Hudzilla.org - the homepage of Paul Hudson
Contents > Java and COM > Bringing Java into the mix Wish List | Report Bug | About Me ]

14.2.4     Using Swing

This is NOT the latest copy of this book; click here for the latest version.

One of the key advantages to using Java over other cross-platform languages such as C++ is that it has a native windowing toolkit to handling GUI environments. In the early days of Java, this toolkit was called the Abstract Windowing Toolkit (AWT), and provided one set of code to handle various native interfaces. AWT interfaced directly with the native operating system on behalf of the programmer, generating GUI components as a native application would.

The downside to AWT, however, was that it was very slow, and a newer, slimmer, more streamlined GUI toolkit was introduced, called Swing. Swing introduced so-called lightweight components - graphical components that were drawn by Swing rather than by the local OS. This led to a difference between the Swing interface and the native interface, but it also gave Java a big speed boost, because it could handle its GUIs as it wanted to.

Nowadays, Swing is the de facto standard for Java GUIs, and, as it is implemented in a variety of Java classes, we can make use of it using PHP. Using these classes is a little complicated, however, because user interfaces generally require dozens of different objects ("widgets") in order to get a working system. It is very difficult to create Swing user interfaces using PHP if you have no knowledge of how to create Swing user interfaces using Java itself, so I am only going to show you how to get started creating the widgets - you will need to do most of the grunt work of designing a complex GUI yourself.

<?php
    
// create a window frame
    
$frame = new Java('javax.swing.JFrame', 'PHP');

    
// and also a button
    
$button = new Java('javax.swing.JButton', 'Hello, world');

    
// the "content pane" of a frame is where we put other widgets if we want them inside the frame. A call to getcontentpane() returns the pane for a frame
    
$pane = $frame->getcontentpane();

    
// add the button to the frame
        // NOTE: This procedure has been changed in the new 1.5 release of Java ("Tiger").
        // You can still do it this way, but new programs targeted at 1.5 or higher can also use
        // $frame->add() directly, which is less hassle.
    
$pane->add($button);

    
// set to the frame to exit when closed
    
$frame->setdefaultcloseoperation($frame->EXIT_ON_CLOSE);

    
// prepare the frame for viewing
    
$frame->pack();

    
// show the frame
    
$frame->setvisible(true);

    
// create a new thread, then tell it to sleep for 20 seconds - this has the effect of keeping the window open a while
    
$thread = new Java('java.lang.Thread');
    
$thread->sleep(20000);

    
// clean up the frame
    
$frame->dispose();
?>

The code is well commented so it should not present a problem. It is a little painful to have to keep writing javax.swing.blah each time you want to create a new widget, but this is a strict requirement using Java this way.

Utilising a sleeping thread is a simple hack to make the Java window stay open for a period of time. You can either loop the sleep call, or write more complicated code to achieve the same effect.

Using Java to create your GUIs might seem like overkill given that the PHP GTK extension works out faster and easier, and, frankly, it is overkill, but it is a fun way to mix two powerful technologies and learn by doing so.

The key advantage to Java is that it has much more powerful GUI creation tools, such as Borland's JBuilder, to help you piece together your interface. In comparison, the preferred GUI editor for PHP-GTK, Glade, lacks quite a few features - it is good, and free, but JBuilder is much more powerful.







<< 14.2.3 Your own classes   14.2.5 The future of PHP and Java >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
A PHP User - 06 Sep 2008

Not necessary, a chapter that can be left out. Since PHP work is generally for server, would be content with producing a simple Debugging Console for PHP.



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is eight plus three?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow