Lesson 5: 15 April: Chapters 9-10
Chapter 9: Building a Swing Interface
Setting Look & Feel
Swing has an abstract class called javax.swing.LookAndFeel which defines how the UI is supposed to look.
The default LNF is called metal and probably looks most like Windows.
There are also LNFs for Windows, Motif (a UNIX XWindows style), and Mac.
The windows JDK ships with metal, Windows and Motif but not Mac.
Example: ChooseTeamLNF.java
Standard Dialogs
A dialog is a popup window used to convey a message to the user or get input. Dialogs are generally modal, that is, they don't return focus until the user has dismissed the dialog.
For a complex dialog, you need to do it the hard way: subclass a JFrame, lay out components, write event handlers.
But for a simple dialog, Swing provides the JOptionPane class. Shows four type of dialog:
ConfirmDialog
for confirming a user decision
InputDialog
prompts for text input
MessageDialog
displays some message
OptionDialog
combines the previous
To create a dialog there is usually a simple version and a more flexible version, using method overloads.
Confirm Dialogs:
Book example: Confirm.java
Input Dialog
Example: Input.java
The book has examples of the other types of standard dialog.
Sliders
A slider is another way of getting input from the user for numeric values.
Implemented in the JSlider class.
Book example: Slider.java
In the example, we don't get any output from the slider. That's done through event handling which we will talk about next week.
Scroll Panes
Scroll bars are those familiar devices on the right or bottom of a window, allowing you to scroll a document beyond its visible limits.
In Swing any component can have scroll bars added to it. This is done through the JScrollPane container object.
You add the component into a JScrollPane, then add the JScrollPane to the layout in place of the original object.
Example: ChooseTeamScrollbar.java
Progress Bars
Used to tell the user what's going on in some long operation, such as a file download or database query.
Implemented through the JProgressBar class.
Book example: Progress.java
Chapter 10: Arranging Components on a User Interface
Some programming environments (such as Visual Basic) allow you to place components at particular coordinate locations.
That's fine for applications that only need to run on one platform. But that doesn't work too well for Java.
When the same application runs on different platforms, or using different Look & Feel, those nicely placed components might not look so good.
Instead Java uses objects called Layout Managers that arrange the components at runtime.
FlowLayout
The default is FlowLayout which arranges everything left to right, then wraps to the next row, like text on a page.
Show ChooseTeam example again with radio buttons.
BorderLayout
I will show one other type of layout manager: BorderLayout. This has north, south, east, west, and center regions.
Show Border.java
For some other types of layout manager, please refer to your books.