Database Reference
In-Depth Information
will use for all our user interface screens. Because we want to center all our GUI screens, we will place
the method in Login , as shown in Listing 12-3, and make the method both public and static—anyone
can call it without referring to an instance of Login . It is part of the template and is code we do not need
to duplicate for each screen. We hand whatever item we want to have centered to this method, and the
method adjusts the item's location.
Listing 12-3. GUI Center() Method
public static void center(Component item) {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = item.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
item.setLocation ((screenSize.width - frameSize.width)/2,
(screenSize.height - frameSize.height)/2);
}
The code for the center() method is often generated automatically by the IDE for GUI applications
(this version is from JDeveloper). I've just moved the code from a standard GUI main() method into this
static method. Listing 12-3 shows the code.
JAVABEANS
I have mentioned JavaBeans, but this is our first introduction to them. JavaBeans are Java classes that
have a required set of methods and interface. In particular, a JavaBean is a GUI object that has getter and
setter methods for each property of the user interface. In this way, a JavaBean can be provided to an IDE,
and the IDE automatically knows how to present it on screen and how to provide access to its properties.
Each piece of text, input box and button that we have in our GUI application is a JavaBean.
Each IDE handles JavaBeans a little differently, placing the property settings code in a bit different location.
Often the IDE will flag this code with a comment like, “This code is automatically generated, do not modify
it.” On the contrary, you generated the code when you drug or painted the component on the GUI
application screen in the IDE, and you can edit the code. However, if you modify the code in unexpected
ways, the IDE may no longer be able to present it to you on the screen. Perhaps a better wording for the
flag comment would be, “This code lets the IDE display your components, please don't modify or add code
that the IDE can't understand.”
In JDeveloper, one of the free Java IDEs from Oracle (the other being NetBeans), when you develop GUI
applications, most of the code for managing the user interface is placed in a method named jbInit() .
Because we find all the parameter settings there initially, it is tempting to add our modifications to the UI in
that method also. However, I have found that it is better to generate another method (what I will call
ojsInit() ) and to code our modifications to the UI in that method instead. We change the standard
 
 
Search WWH ::




Custom Search