Commonly Used Classes
Such is the power of Java in which you have so many resources on which to draw. The user interface for the program appears in Figure 4-11 and the code to create the display appears in Figure 4-12. The code for the TemperatureConverter example calls the addActionListeners method to designate the program as an action listener for all buttons within it. This strategy of using addActionListeners was described in the preceding chapter.
- Then we can drag and drop the other components in this frame.
- The framework provides default implementations of model interfaces for all of its concrete components.
- In general, only complex components, such as tables, trees and sometimes lists, may require the custom model implementations around the application-specific data structures.
- Because of this, most Swing components have associated models , and the programmers can use various default implementations or provide their own.
The format itself is specified using a string as defined in the DecimalFormat class in java.text. The use of format codes is illustrated in the currency converter program shown in Figure 4-8 later in this chapter. The Java GUI subsystem consists of a separate, automous task execution thread called the «event loop». The result of processing an event may be a manipulation of the bits of color on the screen or it may result in calls to methods in the developer’s code. In Java, the event-dispatch thread is distinct from the main thread of the program .
The main body of the calendar uses a TableLayout manager with seven columns. The rows and columns are also set to overlap by a pixel to ensure that there is only a single-pixel line dividing the individual cells. Finally, each day in the calendar is represented using a VPanel with a Swing border attached. The exciting thing about the program is that it offers some very sophisticated features—GUI-based control, tabular formatting, and internationalization—and still fits in three pages of code.
The format control methods at the end of Figure 4-4 turn out to be relatively important. In the absence of format control, the value of a DoubleField often displays huertasencasas.com so many digits that the number becomes unreadable. The setFormat and getFormat methods eliminate this problem by allowing you to specify the output format.
It is started automatically when a user interface object is created. As a result, every Java GUI program is automatically multithreaded. Next note how the GreeterGUI class is defined as a subclass of JFrame and as implementing the ActionListener interface. GreeterGUI thereby inherits all of the functionality of a JFrame. One of its functions is to serve as an ActionListener for its goButton. The ActionListener interface consists entirely of the actionPerformed() method, which is defined in the program. This method encapsulates the actions that will be taken whenever the user clicks the goButton.