Programming with Swing

Introduction

Swing or otherwise known as the Java Foundation Classes, is a group of GUI classes very similar to the AWT (Abstract Window Toolkit). What makes it different however is that while every AWT component has it's native counterpart, Swing components don't and are totally made up of Java code. This is what makes AWT component a "heavyweight" and a Swing/JFC component "lightweight". If you've ever tried to create a Windows program you know how hard it can be to make a nice looking window without having messy and long code. Swing provides for almost everything you'd want for your windows. So having talked a little about Swing, let's get coding!

JFrame and JPanel

JFrame

Remember to import these three packages when working with Swing:

javax.swing.*  
java.awt.*      
java.awt.event.*

A JFrame object is the physical window that you'll be working with in the Swing API. Making a window appear on the screen is easy enough. Here's the code:

JFrame frame = new JFrame("Title Bar Text");        
frame.setSize(256,256);                            
frame.pack();                                      
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.show();                                      

I think that code is relatively simple, all it does is create a new JFrame with a size of 256x256, tell Java to exit when the JFrame is closed and shows the window.

JPanel

You probably want to get started putting components on your brand-new form. Maybe you've even figured out how to and you've discovered that is not what you bargained for. Well, what you don't know is that you need to add a JPanel to the JFrame's contentpane before adding components to the JPanel. Here's what the code looks like:

JFrame frame = new JFrame("Title Bar Text");
JPanel panel = new JPanel();
frame.setSize(256,256);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.getContentPane().add(panel);
frame.pack();
frame.show();

Now we are getting somewhere... You probably have already ran this code, so you've discovered that it doesn't look any different than the last example, and that's because JPanels are just containers for components, the different types of panes are added to the JFrame's contentpane and then components are added to the JPanel. Other than Layout objects which I tell you about later, that's all there is for your basic JPanel.

JButton and JLabel

JButton

Here's where the fun comes in, yep, we are going to add a component, a JButton. You know what a button is right? One of those funky little rectangles that you click on and they go down and then pop back up again. Enough talk, you're probably crapping your-self in anticipation, so here we go!

JFrame frame = new JFrame("Title Bar Text");        
JPanel panel = new JPanel();                        
JButton button1 = new JButton("Button Text");      
frame.setSize(256,256);                            
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.getContentPane().add(panel);                  
panel.add(button1);                                
frame.pack();                                      
frame.show();                                      

And there it is ladies and gentleman, two count 'em 2 new lines of code to create and add a JButton to our JPanel. Notice that when we added the panel, we want to add it to the JFrame's contentpane and when we want to add the JButton, we just plain add() it to the JPanel. This isn't exactly rocket science, if rocket science is what you want, go here.

JLabel

Alright, now that you know how to add components to the JPanel, I'm just going to tell you how to use the constructor method. I goes like this:

JLabel label1 = new JLabel("Label Text");

If you don't understand that, then you shouldn't be reading this, go read up on classes and constructor methods. After you create the JLabel, just add it to the JPanel like we did for JButton.

Component Layout

You may wonder what purpose having to add components to a JPanel serves. JPanels provide the means to layout your components. You choose the layout manager in the constructor for the JPanel. The most common Layouts are:

  • FlowLayout
  • BorderLayout
  • BoxLayout
  • GridLayout
  • GridBagLayout

The following does not describe the full use of all the layout managers and should not be considered the final word on layout managers. I only describe the first
two which are the easiest to use.

The code needed to use a layout manager is like so:

JPanel pane = new JPanel( new BorderLayout() );

and then to add a control to the JPanel:

pane.add(button1, BorderLayout.CENTER);  // possibilities are: NORTH, EAST, WEST, CENTER, SOUTH

And that is how to use BorderLayout. To use FlowLayout is easy as it is the default and you have been using it without even realizing it when we were learning to add components. To learn how to use the other layout managers go get the great book: Java Foundation Classes In A Nutshell.

MDI Applications

MDI apps or Multiple Document Interface is a common thing to see in todays programs. For instance in Microsoft Word, every new document you make is shown as a small window inside the big one. That is MDI. To use MDI in a Swing program you must use a JDesktopPane in place of the usual JPanel on your JFrame. To make the actual small windows use JInternalFrame(s). A JInternalFrame is used just like a JFrame with a few differences of how you create it. Other than creating the JInternalFrame, you still add a JPanel to its contentpane and add components to the JPanel. How 'bout we take a look at the code to add the JDesktopPane to the JFrame?

Here's the code:

JFrame frame = new JFrame("MDI App With Swing");
JDesktopPane desktop = new JDesktopPane();
frame.setSize(560,560);
frame.getContentPane().add(desktop);
frame.pack();
frame.show();

That's easy enough, how 'bout adding that JInternalFrame ? Here it is:

JFrame frame = new JFrame("MDI App With Swing");
JDesktopPane desktop = new JDesktopPane();
JInternalFrame intframe = new JInternalFrame("Title Text",true,true,true,true);
frame.setSize(560,560);
intframe.setSize(256,256);
frame.getContentPane().add(desktop);
desktop.add(intframe);
intframe.pack();
intframe.show();
frame.pack();
frame.show();

Looking at this code you are probably thinking, "Hey what is those extra trues for when intframe is made?", to answer your question: those trues tell Java that we want the window to be able to be resized, maximized, minimized and closed. So far so good, but now we need to add a JPanel and a JButton to that JInternalFrame. How is this done?

Answer: Just like usual, just add the JPanel to the JInternalFrame's contentpane and then add a JButton to the JPanel. Wanna see the code? yes?

Here it is:

JFrame frame = new JFrame("MDI App With Swing");
JDesktopPane desktop = new JDesktopPane();
JInternalFrame intframe = new JInternalFrame("Title Text",true,true,true,true);
JPanel pane = new JPanel();
JButton button1 = new JButton("Button Text");
frame.setSize(560,560);
intframe.setSize(256,256);
intframe.getContentPane().add(pane);
pane.add(button1);
frame.getContentPane().add(desktop);
desktop.add(intframe);
frame.pack();
frame.show();
intframe.pack();
intframe.show();
                                         

Yep, we're getting into longer and longer code samples now. But, if you look the code is still just as hard as when we started. It hasn't gotten much harder!

GUI Look & Feel

Look And Feel is very important, it defines how components look and act (feel) . For instance, what you've been seeing when you have been putting components on a JPanel is the Java Look And Feel. The JButtons don't look normal, do they? Nope. Just like how Windows program looks different from a Java or Mac program, that's look and feel. A Java program typically can have one of the 5 or so possible LAFs(Look And Feels) :

  • Use the current OS's Look and Feel.
  • Use the Java (a.k.a Metal) Look and Feel.
  • Use the Motif (UNIX) Look and Feel.
  • Use the Windows LAF (only on windows).
  • Use the Mac LAF (only on mac).

To set the look and feel BEFORE constructing any components, use the UIManager class. It contains the setLookAndFeel(String classname) method that (as shown) takes a string that is the class name of the LAF that you want to use for your program.

Here's the code to use the Motif LAF:

Try {
    UIManager.setLookAndFeel("com.sun.java.swing.motif.MotifLookAndFeel");
}
catch(ClassNotFoundException e) {}

Here's the code to use the current OS's LAF:

Try {
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(ClassNotFoundException e) {}

The Windows LAF Class Name is com.sun.java.swing.windows.WindowsLookAndFeel
The Mac LAF Class Name is com.sun.java.swing.mac.MacLookAndFeel
The Java LAF Class Name is com.sun.java.swing.plaf.metal.MetalLookAndFeel
To use the Java LAF (even though it IS the default) use the method UIManager.getCrossPlatformLookAndFeelClassName()

That's it for Look and Feel. If you manage to find other LAFs on the web. They are used in the same way, just find out its class name!

Listening for Action

To Listen for an action like a mouse click or key press you use a Listener class. There are several different types of Listener classes and this tutorial only talks about one of them. To find out about the rest, consult a Swing Reference book like the one suggested earlier. I like to use my Listeners as internal classes, so if you don't like that, TOO BAD!

We will be using ActionListener to learn how to use Listener objects. ActionListener listens for a mouse click over a component. We will use it with a JButton to do something when the JButton is clicked.

Here's the code to get a JFrame with a JPanel and a JButton:

JFrame frame = new JFrame("ActionListener Test");
JPanel panel = new JPanel();
JButton button1 = new JButton("Click Me!");
frame.setSize(256,256);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.getContentPane().add(panel);
panel.add(button1);
frame.pack();
frame.show();

Now we want to add a new ActionListener to JButton button1 :

button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e1) {
// CODE TO RUN WHEN BUTTON CLICKED GOES HERE
}});

 I realize that is probably a little simplistic, but we want to keep things simplistic, don't we? Of course we do! So all this code does is make an internal class of
an ActionListener with it's method actionPerformed(ActionEvent e1) that runs when the ActionListener detects (hears?) an action such as our button being clicked.

That's the basics of Action Listening, for more, go buy a good book. Get off the computer! Go do something, ride your bicycle, or something! A book is better for your eyes anyway than the RF Radiation coming from this screen! whew, there's my rant for this section.

Conclusion

I hope you had fun, Java is a neat language. More people should use it, as it has a large and usefull API and it runs on so many platforms. The interpreter has also gotten quite fast, so speed is no longer a reason for not using it.

I recommend going to http://java.sun.com/ and using the API docs, they are essential!

You might also like...

Comments

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.”