Richard's J2SE Day 9 Working with Swing

 

(P233)
Swing = a set of classes
Metal = user-interface style

Creating an Application (P234)
import javax.swing.*;
Before components can be displayed, they must be added to a container.

Creating and Interface (P235)
One way to create = to make the interface a subclas of JFrame
A few things to do in the constructor method of the class:

JFrame class has two constructors: JFrame() and JFrame(String)

SetSize(int,int) or SetSize(Dimension) to set up a frame's size
Or fill the frame with omponents and call the pack() method

Make frames visible by calling the show() method or setVisible(boolean)
to hide a frame, hide() method or setVisible(false)

Default position is in the upper lefe corner of the computer desktop, to specify a different location, call the setBounds(int, int, int, int) => first 2 arguments: upper left corner + width and height

The normal behavior when a frame is closed is for the application to keep running. To change this, call setDefaultCloseOperation() method with one of four static variables

Developing a Framework (P237)
(P238) SimpleFrame.java

If you have not displayed the frame when it was constructed, you could call sf.setVisible(true) in the main() method to display the frame represented by sf.

(P239) SimpleWindow.java

Closing a Window (P240)
The window must be monitored to see whether the user has done anything to close it, such as clicking a title bar close button. It requires the use of event-handling classes (Day 12).
All event-handling classes belong to the java.awt.event package.

To monitor a window in a user interface, must do 3 things:

A window can be monitored by an object that implements the WindowListener interface.
A class must include all the methods in the interface. Classes that support WindowListener must have the following seven methods.

Another class in the java.awt.event package, WindowAdapter, implements this interface with seven empty methods that do nothing. By creating a subclass of WindowAdapter, you can override methods pertaining to the user interaction events that you want to deal with.
(P241) ExitWindow.java for other objects to associate it by calling addWindowListener() method
(P242) ExitFrame.java to use ExitWindow object previsouly created

Creating a Component (P242)
container classes = JFrame, JWindow
event-handling classes = WindowAdapter
interface component class = JButton

A Swing button can feture a text label, a graphical icon or a combination of both...
Constructor methods:

Adding Components to a Container (P243)
Add a component to a container, add(Component) method
Ordinarily, components are added to the container's content pane.
1. Create a panel
2. Add components to the panel using its add(Component) method
3. Call setContentPane(Container) with the pane as an argument

(P244) Buttons.java => Frame > Pane > Buttons

 

Working with Components (P245)
Swing offers two dozens different user interface components. All have common superclass, javax.swing.JComponent.

Image Icons (P245)
Swing supports ImageIcon objects on buttons and other components in which a label can be provided.
P246 Icons.java

Labels (P247)
User component that contains text, an icon, or both.
Constructors:

Text Fields (P248)
User can enter and modify text through the keyboard = JTextField Class
Constructors:

setEditable(boolean), isEditable(), setText(String), getText(), getSelectedText()
JPasswordField class = a subclass of JTextField

Text Areas (P249)
more than one line of input = JTextArea class
Constructors:

getText(), getSelectedText(), setText(String), append(String), insert(String, int), setLineWrap(boolean), setWrapStyleWord(boolean)

(P250) Form.java

Scrolling Panes (P251)
Text areas do not include scrollbars.
Swing introduces a new container that can be used to hold any component that can be scrolled: JScrollPane.
Constructors:

Static class variables of the ScrollPaneConstants interface

Scrollbars (P251)
components that enable a part of a large display to be selected for viewing. often used on text areas.
Some Swing components have built in scrollbar functionality, including scrolling panes and lists. You can also create a scrollbar by itself.
Constructors:

Check Boxes and Radio Buttons (P252)
2 possible values: selected or not selected
JCheckBox and JRadioButton classes

Constructors:

JRadioButton class has constructors with the same arguments and functionality.
To organise several radio buttons into a group, allowing only one to be selected, create a ButtonGroup class object:

(P254) ChooseTeam.java

Drop-Down Lists and Combo Boxes (P255)
Swing class JComboBox can be used to create drop-down lists and combo boxes
Drop-down lists = a single item to be picked
Combo boxes = drop-down lists with a extra feature: a text field

Create a drop-down list

If the componet's setEditable() method is called with true as an argument, it becomes a combo box.
JComboBox() class methods:

(P256) Expiration.java

(P260) Exercise
VCRControls.java