Graphical User Interfaces: Programming With Java's Swing API February 4, 2003 CMPS 109 ! Advanced Programming
Graphical User Interfaces: Programming With Java's Swing API February 4, 2003 CMPS 109 ! Advanced Programming
February 4, 2003
CMPS 109 ! Advanced Programming
Priorities
Does your program do what it is supposed to?
If the program doesn’t work, there’s no point to using it.
Examples of Components:
Labels, Buttons, Images, Text Entry boxes, Lists, Trees, Menus, etc.
AWT and Swing
The Abstract Windowing Toolkit !AWT" was
Java’s original GUI toolkit.
It used “native” widgets.
It was difficult to program.
It did not support a wide range of widgets.
class HelloButton {
public static void main( String[] args )
{
JFrame frame = new JFrame( “Hello Button” );
Container pane = frame.getContentPane();
JButton hello = new JButton( “Hello, world!” );
pane.add( hello );
frame.pack();
frame.show();
}
}
What to import
import java.awt.*;
import javax.swing.*;
...
pane.add( hello );
...
Create another Swing component.
In this case, a standard push%button is created with the label “Hello,
World!”.
This button doesn’t do anything when pushed.
frame.pack();
frame.show();
...
MouseListener DocumentListener
MouseMotionListener TableModelListener
WindowListener TreeModelListener
ItemListener
Listening for Events
class HelloButton {
public static void main( String[] args )
{
new HelloButton();
}
HelloButton()
{
JButton hello = new JButton( “Hello, world!” );
hello.addActionListener( new HelloButtonListener() );
JFrame frame = new JFrame( “Hello Button” );
Container pane = frame.getContentPane();
pane.add( hello );
frame.pack();
frame.show();
}
A Sample Listener
private class HelloButtonListener
implements ActionListener
{
public void actionPerformed( ActionEvent e )
{
System.out.println( “The event was: “ + e );
}
}
}
class HelloButton {
public static void main( String[] args )
{
new HelloButton();
}
HelloButton()
{
JButton hello = new JButton( “Hello, world!” );
hello.addActionListener( new HelloButtonListener() );
JFrame frame = new JFrame( “Hello Button” );
frame.addWindowListener( new HelloButtonWindowListener() );
frame.getContentPane().add( hello );
frame.pack();
frame.show();
}
Quitting Example
private class HelloButtonListener
implements ActionListener
{
public void actionPerformed( ActionEvent e )
{
System.out.println( “The event was: “ + e );
}
}
Uses of Borders:
Excellent for debugging layout issues when you have nested JPanels
and resize problems.
Empty borders can be used to create extra spacing around
components where nothing is drawn.
Compound borders can be used to stack borders on top of each
other !for example, an empty area of 12 pixels on all sides, and then
an etched border, or another compound border".
Text Input with Swing
TextFieldExample() {
textField = new JTextField( “Enter text here...” );
echoLabel = new JLabel( “...it will echo here.” );
JFrame frame = new JFrame( “Text Field Example” );
frame.getContentPane().setLayout( new GridLayout( 2, 1 ) );
frame.getContentPane().add( textField );
frame.getContentPane().add( echoLabel );
frame.pack();
frame.show();
}
private class TextFieldListener
implements ActionListener
{
public void actionPerformed( ActionEvent e ) {
echoLabel.setText( textField.getText() );
}
}
JOptionPane
JOptionPane allows you to create simple
dialog boxes:
showConfirmDialog() presents the user with up to three choices: Yes,
No, and Cancel.
showInputDialog() gives the user a question and a text field to
enter input.
showMessageDialog() is great for reporting errors !invalid input"
and simple messages.
MenuBarExample() {
mbeFrame = new JFrame( “Text Field Example” );
mbeFrame.setJMenuBar( makeMenuBar() );
mbeFrame.getContentPane().add( new JLabel( “Other Stuff Here.” ) );
mbeFrame.pack();
mbeFrame.show();
}
private class QuitItemListener
implements ActionListener
{
public void actionPerformed( ActionEvent e ) {
if( JOptionPane.showConfirmDialog( mbeFrame,
“Do you want to quit?”, “Confirm Quit”,
JOptionPane.YES_NO_OPTION ) == JOptionPane.YES_OPTION ) {
System.exit( 0 );
}
}
}
Menu Bar Example
private JMenuBar makeMenuBar() {
JMenuItem quitItem = new JMenuItem( “Quit” );
quitItem.addActionListener( new QuitItemListener() );
JMenu fileMenu = new JMenu( “File” );
fileMenu.add( new JMenuItem( “Item 1” ) );
fileMenu.add( “Item 2” );
fileMenu.addSeparator();
JMenu fontMenu = new JMenu( “Font” );
fontMenu.add( “Times” );
fontMenu.add( “Courier” );
fontMenu.add( “Script” );
JMenu editMenu = new JMenu( “Edit” );
editMenu.add( “Undo” );
editMenu.addSeparator();
editMenu.add( fontMenu );
JMenuBar mbar = new JMenuBar();
mbar.add( fileMenu );
mbar.add( editMenu );
return( mbar );
}
Demonstrations and
Questions