Using Swing

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 tools, such as Eclipse, to help you piece together your user interface. In comparison, the preferred GUI editor for PHP-GTK, Glade, lacks quite a few features - it is good, and free, but Eclipse is much more powerful.


 

Want to learn PHP 7?

Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!

If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!

Next chapter: The future of PHP and Java >>

Previous chapter: Your own classes

Jump to:

 

Home: Table of Contents

Copyright ©2015 Paul Hudson. Follow me: @twostraws.