This tutorial shows you how to build a Single Document Interface (SDI) application with NetBeans. It presumes that you know what an SDI user interface is and that you want to build one with NetBeans.
You can find more about SDI interfaces here: additional SDI information.
In this tutorial you will build a NetBeans project with a main window and two child windows that display traffic camera pictures that update several times per minute. The traffic cam images are from the Minnesota Department of Transportation Web site. You can see that site here: MDOT Traffic Cameras .
If you try to run the application you won't see much, but try running it anyway. To compile and run the application click on the Run menu then click on Run Main Project. (Or click on the tool bar button for running the main project.)
You should see a dialog box telling you that there is no main application class to compile and run. Recall that earlier you told NetBeans not to create a main class. SDIAppGui.SDIAppMainWindow should be highlighted as a suggested class to create and compile. Click the OK button. NetBeans will chug away compiling the application and in a moment a blank window will display. That's the application thus far. Close the window.
Figure 3. Creating a JFrame Form
What you put on your main application window is up to you, but for this tutorial you will need to put a few things on it to open other windows.
All you have at this point is a JFrame with nothing on it. You will add a JPanel onto the JFrame to have a container to hold stuff. You will also add a menu to invoke the secondary or child windows that you will create shortly.
Before you start adding components to the JFrame you might want to make the main application window open in the center of the screen and give the application a title in the main window title bar. To center the main application window do the following:
Your application will now open in the center of the screen. Run the application to test it.
To give your application a title, right click on the JFrame again and re-open its properties dialog box. Click the properties tab. For the title property type: Traffic Cameras. Close the dialog box and rerun the application. You should see the title in the window's title bar when you run the application.
Rename the JFrame to: frameMain. To do this, reopen the frame's properties dialog box. Click the Properties tab and scroll down to the name property. Type frameMain for the name property. Close the dialog box.
Now add Swing components to the JFrame. The NetBeans IDE will have to be displaying the Design Editor window as shown in Figure 2. The Palette window is in the upper right of the IDE. You can add components by dragging them to the JFrame, or by clicking on a component in the palette, then clicking on the JFrame.
Add a menu to the main application window:
Add a JPanel:
Add a border to the JPanel:
Now that you have your main application window you can create the child windows. For this tutorial you will create two windows. In a real application you would likely have numerous child windows, but for this tutorial two child windows will suffice to demonstrate an SDI user interface.
Your child windows will be JDialog components. You will be adding two JDialog components to your main application to create the child windows. Go to the NetBeans Design Editor window.
Repeat steps 1 through 6 above for the second child window, but in step 4 name the window cam2Win, and in step 6 change the title to Camera 2.
The JLabel components are containers for the traffic camera images.
Your child windows are ready to be used by the main application window. Now you will add Java code to open the child windows and to display the traffic images.
Rather than typing the code in the steps below, copy the code from this Web page and paste it into your application source at the appropriate places.
import java.awt.Image; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.net.MalformedURLException; import java.net.URL; import javax.swing.AbstractAction; import javax.swing.Action; import javax.swing.ImageIcon; import javax.swing.Timer;
On the main menu you need to add code to quit the application and code to open the child windows. Click on the design button so that the design editor window shows.
private void menuMainQuitMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
}
private void menuMainCam1MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
}
cam1Win.setBounds(0,0,400,400);
cam1Win.setVisible(true);
The first statement sets the location of the child window to the top left of the screen with a width and height of 400 pixels. The second statement displays the window.
private void menuMainCam2MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
}
cam2Win.setBounds(400,0,400,400);
cam2Win.setVisible(true);
Note that this second window has a setBounds statement that positions it 400 pixels from the left so that it does not overlap the first window.
Now the main application menu items have code to quit the application and to open the child windows. Run the program and click on the menus to see that they work. Notice that with the child windows open, when you close the main application window, the child windows also close.
private void firstChildWinWindowActivated(java.awt.event.WindowEvent evt) {
// TODO add your handling code here:
}
Action updateImage1 = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
URL u = null;
lblImage1.setIcon(null);
try {
u = new URL("...");
Image img = Toolkit.getDefaultToolkit().createImage(u);
ImageIcon pic = new ImageIcon(img);
lblImage1.setIcon(pic);
}
catch (MalformedURLException mue){
mue.printStackTrace();
}
}
};
new Timer(7000, updateImage1).start();
This code retrieves a highway camera image from the MNDOT web site and creates an image object for it. Then the code assigns the image to the JLabel (lblImage1.setIcon(pic)) icon property. An action even is wrapped around the image retrieval so that a Swing timer can repeatedly call the code to refresh the image. The last statement in this code, new Timer(7000, updateImage1).start();, starts a Swing timer that refreshes the image about four times a minute.
private void secondChildWinWindowActivated(java.awt.event.WindowEvent evt) {
// TODO add your handling code here:
}
Action updateImage2 = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
//System.out.println("object doesn't exist" );
URL u = null;
lblImage2.setIcon(null);
try {
u = new URL("...");
Image img = Toolkit.getDefaultToolkit().createImage(u);
ImageIcon pic = new ImageIcon(img);
lblImage2.setIcon(pic);
}
catch (MalformedURLException mue){
mue.printStackTrace();
}
}
};
new Timer(7000, updateImage2).start();
This code is slightly different from that in the first child window. The action event name, the image URL camera number, and the JLabel name that contains the image are different. The MNDOT traffic cameras do go down occasionally, so if, when you run the application, a child window does not display an image within a couple minutes, go to the MNDOT site and choose a camera. Then use that camera number in place of the 624 or 848 in the above code.
That's it. You now have an SDI application that does something (Figure 8). A couple notes before you run the application. When you run the application it will take a minute for the images to show in the child windows. It takes a minute or so for the timers to start, get the images from the URL and display the image. Also, because you have a timer or two running, when you try to close the application, it will not respond immediately.
Figure 8
This tutorial showed you how to create a Java SDI application using NetBeans. You saw how to create a NetBeans project and an application window, and how to create child windows that are invoked from the main application window.
| Figure1.jpg | ![]() |
174145 bytes |
| Figure2.jpg | ![]() |
262852 bytes |
| Figure3.jpg | ![]() |
238702 bytes |
| Figure4.jpg | ![]() |
165604 bytes |
| Figure5.jpg | ![]() |
146247 bytes |
| Figure6.jpg | ![]() |
61915 bytes |
| Figure7.jpg | ![]() |
47884 bytes |
| Figure8.jpg | ![]() |
261957 bytes |