Running and testing JavaBeans Once you have written a class using the Visual Editor for Java, you may wish to run and test it.. Visual Age for Java was able to generate a default impleme
Trang 1Once the template is used, the cursor will be positioned inside the actionPerformed method Add the following statement to this method:
getJTextFieldCustomerSelection().setText(getFirstName());
This statement calls the getFirstName method and writes the return value to the jTextFieldCustomerSelection bean
The getJButtonGetFirstName method is completed now The bold code is the new action listener code that has been added (Figure 14-45)
Figure 14-45 getJButtonGetFirstName method
By now, we have finished designing our sample GUI, including an event handler, and are able to run and test it
Running and testing JavaBeans
Once you have written a class using the Visual Editor for Java, you may wish to run and test it If the class has a main method, you can use the menu option Run -> Java Application
In this section we show how to run the sample as a JavaBean and as a Java application Additional information about running a Java class is provided in the section “Running your programs” on page 103
However, when you write a JavaBean using the Visual Editor, it will not typically
private javax.swing.JButton getJButtonGetFirstName() {
if (jButtonGetFirstName == null) { jButtonGetFirstName = new javax.swing.JButton();
jButtonGetFirstName.setText("Get First Name");
jButtonGetFirstName
.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { getJTextFieldCustomerSelection().setText(getFirstName());
} });
} return jButtonGetFirstName;
}
Trang 2By comparison, in VisualAge for Java, you could not test a JavaBean, and you could only execute JavaBeans through the main method Visual Age for Java was able to generate a default implementation of the main method to help test the JavaBean
Running the sample GUI as a JavaBean
To run the sample GUI, simply click the drop-down arrow of the Run icon in the toolbar and select Run The Launch Configurations dialog opens and you select
Java Bean / Applet from the Launch Configurations list (Figure 14-46)
Figure 14-46 Create a launch configuration for the JavaBean Click New to create a new launch configuration for the class CustomerGUI
If the class you are testing extends java.awt.Applet or javax.swing.JApplet, you can go to the Applet Parameters tab of the launch configuration to specify its parameters It will be launched inside an applet viewer
Other tab views let you specify the JRE, program, and VM arguments, classpath information, and additional settings Besides using the JavaBean launcher to run your JavaBeans, you can also select Debug -> Debug As -> Java Bean to run JavaBeans using the debugger
Figure 14-47 shows the new launch configuration with the JavaBean tab Leave the default values and click Run
Trang 3Figure 14-47 Run the JavaBean by using its launch configuration The GUI should be launched now, and Figure 14-48 shows what the launched sample GUI looks like
Figure 14-48 Launched sample GUI with the default look-and-feel
Trang 4look-and-feel, Swing initializes the cross-plattform Java look-and-feel, formaly knows as Metal
If you change the launch configuration and launch the sample GUI with the Windows look-and-feel, our GUI looks as shown in Figure 14-49 on page 507
Figure 14-49 Launched sample GUI with the Windows look-and-feel
Running the sample GUI as a Java application
You can also run the sample GUI as a Java application To do this you have to add code to the main method
Figure 14-50 shows the main method including the additional code As we use a JPanel, we have to put this panel first in a frame, to make it runnable as a Java application
Finally, we have to set the frame object visible, and our sample GUI is now also runnable as a Java application
Figure 14-50 Main method of the sample GUI
public static void main(String[] args) { JFrame frame = new JFrame("Sample GUI");
CustomerGUI customerGUI = new CustomerGUI();
frame.setSize(330, 150);
frame.getContentPane().add(customerGUI);
// close the frame properly instead of just hiding it frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
Trang 5To run the GUI as an application, select the CustomerGUI class and Run -> Run As -> Java Application This adds a second launch configuration under Java Application
Note that we set the vertical size of the frame to 150, not big enough to hold all the last names In this case the scroll bar appears so that we can scroll through the names (Figure 14-51)
Figure 14-51 Reduce sample GUI window size
Testing the sample GUI
When we launch our sample GUI, we can play around by selecting elements from the list and clicking the button to return the first name
You can also resize the GUI and see the GUI ‘s behavior If you for example reduce the size of the GUI as shown in Figure 14-51, the implemented scroll pane of the list becomes visible Also,the beans align automatically
Another difference with respect to the Visual Composition Editor from Visual Age for Java is that the new Visual Editor directly reads and writes source code and has no additional metadata files associated with a visual class
Running the sample outside of Application Developer
To run the sample outside of Application Developer, select the CustomerGUI class and Export -> File system Export the project to the hard drive, for example C:\
To run the sample, go to the ItsoProGuideGui directory that is created by exporting and issue either of these commands:
java -cp ".;c:\sqllib\java\db2java.zip" itso.gui.CustomerGUI java itso.gui.CustomerGUI
JScrollPane bean
Trang 6In this chapter we introduced you to the new Visual Editor We created a new GUI and demonstrated how to add JavaBeans to the GUI based on Swing
components
We also implemented event handling code to the GUI and showed how to run and test such a visual bean
Trang 8Part 3 Testing and
debugging applications
Part 3 describes how to test and debug your applications This part of the book also introduces Application Developer’s server tools feature and demonstrates how to test components by using JUnit
Part 3
Trang 10Chapter 15. Servers and server
configurations
Application Developer provides support for testing, debugging, profiling, and deploying your Enterprise applications to built-in, local, and remote test environments
To run an enterprise application or Web application in Application Developer, it must be published (deployed) to the server This is achieved by installing the EAR project for the application into an application server The server can then be started and the application can be tested in a Web browser or by using the universal test client for EJBs and Web services
This chapter describes how to use the Server perspective to set up and use servers and server configurations to test applications
15