Username:
Password:
Java 6 Tutorials

Swing Components / Main Window

Subtitles of the Movie

This movie is about the fundamental structure of writing the mainline class of a Swing application. Swing is a collection of displayable components that you can hook together to create a graphical user interface. The main window of a Swing application has some special considerations. This example program displays an empty main window. It's a Swing mainline program and you can tell that because it extends the JFrame class. Here with these three lines of code, the size of the window is set to 200 pixels high by 300 pixels wide. The location of the upper left-hand corner of the window is set to 250 pixels from the left and 150 pixels from the top. All screen measurement in Java are made in pixels. The actual physical size is determined by the resolution setting of the monitor. The window is put on display by the last call, the one to set visible, setting it to true. You can always make a window disappear again by setting visible to false. Notice this code. The call to the method add window listener. If you're not familiar with anonymous classes, this code will look a little odd to you. It would have been possible to declare a reference to the window adapter to find a class overriding the window closing method, create an object of that class, put it in the reference and pass the reference to add window listener. This would achieve the same thing that's being done here by declaring the class and creating an instance of it in the same spot. This is sort of a shorthand technique for doing the thing. If you don't do this, the user can close the window, but the application won't know anything about it. It'll just keep running, but it won't have a window. With this little piece of code in place, your application shuts down when the window closes. Here's how it works. The window appears at the specified location on the screen and doing this not only closes the window, but it also shuts down the application. A second common way to shut down a program is to have a menu selection named something like close or quit. This example has a file menu with a button labeled exit and that menu button will cause the program to cease execution. To construct a menu at the top of a JFrame window, a JMenuBar object is constructed. This is the bar of pull-down menus that appears across the top of the window. This example only has one pull-down menu. The tradition is that the left-most pull-down menu be named file. With a call to add this new pull-down menu is added as the first menu on the bar. A new JMenuItem is constructed. It's labeled exit. This class implements the action listener interface so it's an action listener and again being used to receive action events from the exit button on the menu. The menu item which is the exit button is added to the file menu with a call to add and finally the menu bar itself is assigned to be the menu bar for this JFrame object. Here at the very bottom you can see the action perform method that will be called whenever the exit button is selected. A test is made to determine which button has been selected. Now, in this simple example, there's only one button so a call is made to system exit, just like in the previous example. This shuts down the program. Here's how it works. Selecting the exit button on the menu closes the application. The third common way to shut down a program is to use a regular push button. It works the same way a menu does by issuing an action event. A JButton is constructed to be the exit button. This class is added to the list of action listeners for this button. You can actually have a number of action listeners respond to the same button, but you almost always only need one. The button is then added to the window for display. The action performed method is exactly the same as it was for the menu example. It checks to see whether the selected button was an exit button and if so, it calls system exit. Here you can see the exit button in the middle of the screen and you can see how it works. Now, what you want is a program that can be closed in any of the three different ways. Here's a skeleton mainline program that can be used as a starting point for building an application. At least that's what I do with it. There's nothing much new here. It's just a matter of throwing all the code from the previous examples into one class. It all works together ok. The code that defines the menu and the new window layout are separated into separate methods. That's because in a regular application, these two have a tendency to get quite large and I like to have things organized so I can find them. As you can see, this program has both a push button and a menu. Either of them can be used to close the window. Also, the upper, right-hand corner can be used. Let me mention a couple of things that you might want to consider. This program always halts with a direct call to system exit. This function call halts the JVM, so that would kill any of the threads or any other programs that you may have running. That may or may not be what you want. You may want to write your own termination method and use it instead. And you can simply let the window close. Then have the mainline of the program return. Killing the JVM is usually ok, but you may want to get out and leave it under certain circumstances. You may want your own termination method to save information to disk before you shut down. Or you could have an exclusive lock on some resource and need to release it. Or you may want to ask the user if he knows what he's doing. Anyway, once you've got your program so you can make it start and stop, it's simply a matter of adding features until it does everything else you want it to do. Stay tuned because we've got some more Swing stuff coming up. Things like dialog boxes and different types of buttons. Also, I'm going to show you a nifty way to layout a window the way you'd like to see it.

Tutorial Information

Course: Java 6
Author: Arthur Griffith
SKU: 33858
ISBN: 1-934743-59-3
Release Date: 2008-02-29
Duration: 7 hrs / 92 lessons
Work Files: Yes
Captions: Available on CD and Online University
Compatibility: Vista/XP/2000, OS X, Linux
QuickTime 7, Flash 8

VTC Sign up & Benefits

  • Unlimited Access
  • 81,350 Video Tutorials (20,800 free)
  • Video Available as Flash or QuickTime
  • Over 782 Courses
  • $30 for One Month Access
  • Multi-User Discounts Available