Session 4: 8 April: Chapter 8: Working with Swing
Go over finalize - didn't get to it last week.
Swing: what is it?
AWT
Java 1.0 had something called the Abstract Windowing Toolkit or AWT for developing Graphical User Interfaces.
AWT still exists and is contained in the package java.awt. Swing is an extension of AWT. AWT is not used much any more because it is quite limited.
AWT required native objects called "peer objects" written in C or C++ to handle the visual part of the interface.
This ensured that the GUI would have the look and feel of the underlying OS.
But it forced a kind of "lowest common denominator" approach to design.
Swing
In Java 2 (JDK 1.2) Sun introduced Swing.
Swing is in the package javax.swing.
"javax" means "java extension"
I don't know where the "swing" comes from. (extra credit for anyone who finds out)
Swing does not use peer objects, the UI is entirely written in Java.
To achieve appropriate look and feel for the OS, you can use a "look and feel class". There is a Macintosh, Windows and XWindows L&F.
But the default is called metal and is probably most similar to Windows.
Swing offers more flexibility and features than AWT. Most new Java client development uses Swing.
Deploying Swing applets/applications
Applets
Since Java 2 is not supported by most browsers, you cannot assume that the client already has it.
To deploy a Swing applet, the user must install the Java plug-in which has all the support for Swing.
Increases the size and difficulty of your applet deployment.
Applications
To deploy a Swing application (not applet), your best bet is Java Web Start which launches the app from a web page.
Creating an application
Most commonly will extend a JFrame or JWindow.
JWindow is a stripped-down window without title bar, maximize etc.
e.g., about box or splash screen
JFrame is a window with title bar, minimize, maximize and close buttons.
<4.1>
public class Lookup extends javax.swing.JFrame {
//...
}
A simple empty frame application, from book:
<4.2>
import javax.swing.JFrame
public class SimpleFrame extends JFrame {
public SimpleFrame() {
super("Frame Title");
setSize(300, 100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] arguments) {
SimpleFrame sf = new SimpleFrame();
}
}
A simple window application, from book:
<4.3>
import javax.swing.JWindow;
public class SimpleWindow extends JWindow {
public SimpleWindow() {
super();
setBounds(250, 225, 300, 150);
}
public static void main(String[] arguments) {
SimpleWindow sw = new SimpleWindow();
sw.setVisible(true);
for (int i = 0; i < 10000; i++)
System.out.print(i + " ");
sw.setVisible(false);
System.exit(0);
}
}
Components & Containers
Components
A component is an object having a graphical representation that can be displayed on the screen and that can interact with the user. (java.awt.Component documentation)
Every component type has its own class. To create a component, you make an object of that class (instantiate) then place on a container.
Example: buttons
<4.4>
JButton play = new JButton("Play");
JButton rewind = new JButton("Rewind");
Containers
A component that can contain other AWT components. (java.awt.Container doc)
Example: JPanel
<4.5>
JButton quit = new JButton("Quit");
JPanel panel = new JPanel();
panel.add(quit);
Some Swing containers use panes which requires a different way of adding the components.
Includes JFrame, JWindow, JDialog and JApplet-- all the top-level containers.
You need to use a content pane, as in this book example:
<4.6>
import javax.swing.*;
public class Buttons extends JFrame {
JButton abort = new JButton("Abort");
JButton retry = new JButton("Retry");
JButton fail = new JButton("Fail");
public Buttons() {
super("Buttons");
setSize(80, 140);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel pane = new JPanel();
pane.add(abort);
pane.add(retry);
pane.add(fail);
setContentPane(pane);
}
public static void main(String[] arguments) {
Buttons rb = new Buttons();
rb.show();
}
}
Here is another way to do it:
<4.7>
import javax.swing.*;
public class Buttons extends JFrame {
JButton abort = new JButton("Abort");
JButton retry = new JButton("Retry");
JButton fail = new JButton("Fail");
public Buttons() {
super("Buttons");
setSize(80, 140);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//add to the already existing content pane
getContentPane().add(abort);
getContentPane().add(retry);
getContentPane().add(fail);
}
public static void main(String[] arguments) {
Buttons rb = new Buttons();
rb.show();
}
Adding to an Applet
Don't need a main() method, since the init() and start() methods are automatically called from the browser.
main() is automatically called when you launch an application from the command line.
Don't need a setSize() because this was specified in the HTML.
<4.8>
import javax.swing.*;
public class ButtonApplet extends JApplet {
JButton abort = new JButton("Abort");
JButton retry = new JButton("Retry");
JButton fail = new JButton("Fail");
public void init() {
JPanel pane = new JPanel();
pane.add(abort);
pane.add(retry);
pane.add(fail);
setContentPane(pane);
}
}
Working with Components
Common methods
setVisible(boolean) makes any component visible or invisible.
setEnabled(boolean) can disable a component. It doesn't make it invisible but does make it un-usable.
set/getText() for text components and set/getValue() for numeric components.
ImageIcons
Can get the image from a .gif or other graphics file.
Read into an ImageIcon object.
Book example:
<4.9>
import javax.swing.*;
public class Icons extends JFrame {
JButton[] buttons = new JButton[24];
public Icons() {
super("Icons");
setSize(335, 318);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel pane = new JPanel();
ImageIcon icon = new ImageIcon("3dman.gif");
for (int i = 0; i < 24; i++) {
buttons[i] = new JButton(icon);
pane.add(buttons[i]);
}
setContentPane(pane);
show();
}
public static void main(String[] arguments) {
Icons ike = new Icons();
}
}
Labels & Text Fields
Labels
Not editable
<4.10>
JLabel tinker = new JLabel("Tinker", SwingConstants.LEFT);
JLabel evers = new JLabel("Evers");
Text Fields
Editable
<4.11>
JTextField name = new JTextField(30);
JTextField name = new JTextField("Tomaso Alberto", 30);
to get a user-entered value, use getText().
For password masking, use JPasswordField
Book example: a user input form
<4.12>
import javax.swing.*;
public class Form extends javax.swing.JFrame {
JTextField username = new JTextField(15);
JPasswordField password = new JPasswordField(15);
JTextArea comments = new JTextArea(4, 15);
public Form() {
super("Feedback Form");
setSize(260, 160);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel pane = new JPanel();
JLabel usernameLabel = new JLabel("Username: ");
JLabel passwordLabel = new JLabel("Password: ");
JLabel commentsLabel = new JLabel("Comments:");
comments.setLineWrap(true);
comments.setWrapStyleWord(true);
pane.add(usernameLabel);
pane.add(username);
pane.add(passwordLabel);
pane.add(password);
pane.add(commentsLabel);
pane.add(comments);
setContentPane(pane);
show();
}
public static void main(String[] arguments) {
Form input = new Form();
}
}
Check Boxes & Radio Buttons
Check boxes are boolean true/false input
Radio buttons are for choosing among several alternatives
Use a nongraphical ButtonGroup object to group them together as one logical unit.
book example:
<4.13>
import javax.swing.*;
public class ChooseTeam extends JFrame {
JRadioButton[] teams = new JRadioButton[4];
public ChooseTeam() {
super("Choose Team");
setSize(140, 190);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
teams[0] = new JRadioButton("Colorado");
teams[1] = new JRadioButton("Dallas", true);
teams[2] = new JRadioButton("New Jersey");
teams[3] = new JRadioButton("Philadelphia");
JPanel pane = new JPanel();
ButtonGroup group = new ButtonGroup();
for (int i = 0; i < teams.length; i++) {
group.add(teams[i]);
pane.add(teams[i]);
}
setContentPane(pane);
show();
}
public static void main(String[] arguments) {
ChooseTeam ct = new ChooseTeam();
}
}
Drop-down Lists & Combo Boxes
Use the same component, JComboBox for both
For a combo box, the component remains visible on the page. For a drop-down list, the component only becomes visible when needed.
book example:
<4.14>
import javax.swing.*;
public class Expiration extends JFrame {
JComboBox monthBox = new JComboBox();
JComboBox yearBox = new JComboBox();
public Expiration() {
super("Expiration Date");
setSize(220, 90);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel pane = new JPanel();
JLabel exp = new JLabel("Expiration Date:");
pane.add(exp);
for (int i = 1; i < 13; i++)
monthBox.addItem("" + i);
for (int i = 2000; i < 2010; i++)
yearBox.addItem("" + i);
pane.add(monthBox);
pane.add(yearBox);
setContentPane(pane);
show();
}
public static void main(String[] arguments) {
Expiration ct = new Expiration();
}
}