Gui Java
Gui Java
1
Why learn GUIs?
• Learn about event-driven programming techniques
• Practice learning and using a large, complex API
• A chance to see how it is designed and learn from it:
model-view separation
design patterns
refactoring vs. reimplementing an ailing API
• Because GUIs are neat!
2
Java GUI History
• Abstract Windowing Toolkit (AWT): Sun's initial effort to create a
set of cross-platform GUI classes. (JDK 1.0 - 1.1)
Maps general Java code to each operating system's real GUI system.
Problems: Limited to lowest common denominator; clunky to use.
• Swing: A newer GUI library written from the ground up that allows
much more powerful graphics and GUI construction. (JDK 1.2+)
Paints GUI controls itself pixel-by-pixel rather than handing off to OS.
Benefits: Features; compatibility; OO design.
3
GUI terminology
• window: A first-class citizen of the graphical desktop.
Also called a top-level container.
examples: frame, dialog box, applet
4
Components
5
Swing inheritance hierarchy
• Component (AWT)
import java.awt.*;
Window import javax.swing.*;
• Frame
• JFrame (Swing)
• JDialog
Container
• JComponent (Swing)
• JButton JColorChooser JFileChooser
• JComboBox JLabel JList
• JMenuBar JOptionPane JPanel
• JPopupMenu JProgressBar JScrollbar
• JScrollPane JSlider JSpinner
• JSplitPane JTabbedPane JTable
• JToolbar JTree JTextArea
• JTextField ...
6
Component properties
Each has a get (or is) accessor and a set modifier method.
examples: getColor, setFont, setEnabled, isVisible
name type description
background Color background color behind component
border Border border line around component
enabled boolean whether it can be interacted with
focusable boolean whether key text can be typed on it
font Font font used for text in component
foreground Color foreground color of component
height, width int component's current size in pixels
visible boolean whether component can be seen
tooltip text String text shown when hovering mouse
size, minimum / Dimension various sizes, size limits, or desired
maximum / preferred size sizes that the component may take
7
JFrame
a graphical window to hold other components
• public JFrame()
public JFrame(String title)
Creates a frame with an optional title.
8
More JFrame
9
JButton
a clickable region for causing actions to occur
10
GUI example
import java.awt.*; // Where is the other button?
import javax.swing.*;
public class GuiExample1 {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(new Dimension(300, 100));
frame.setTitle("A frame");
JButton button1 = new JButton();
button1.setText("I'm a button.");
button1.setBackground(Color.BLUE);
frame.add(button1);
JButton button2 = new JButton();
button2.setText("Click me!");
button2.setBackground(Color.RED);
frame.add(button2);
frame.setVisible(true);
}
}
11
Sizing and positioning
How does the programmer specify where each component appears,
how big each component should be, and what the component
should do if the window is resized / moved / maximized / etc.?
13
JFrame as container
A JFrame is a container. Containers have these methods:
14
Preferred sizes
• Swing component objects each have a certain size they would "like"
to be: Just large enough to fit their contents (text, icons, etc.).
This is called the preferred size of the component.
15
FlowLayout
public FlowLayout()
myFrame.setLayout(new FlowLayout());
myFrame.add(new JButton("Button 1"));
The default layout for containers other than JFrame (seen later).
16
BorderLayout
public BorderLayout()
myFrame.setLayout(new BorderLayout());
myFrame.add(new JButton("Button 1"), BorderLayout.NORTH);
17
GridLayout
public GridLayout(int rows, int columns)
18
Event Listeners
19
Graphical events
• event: An object that represents a user's interaction with a GUI
component; can be "handled" to create interactive components.
20
Event-driven programming
• event-driven programming: A style of coding where a program's
overall flow of execution is dictated by events.
Rather than a central "main" method that drives execution,
the program loads and waits for user input events.
As each event occurs, the program runs particular code to respond.
The overall flow of what code is executed is determined by the series
of events that occur, not a pre-determined order.
21
Event hierarchy
import java.awt.event.*;
• EventObject • EventListener
AWTEvent (AWT) AWTEventListener
• ActionEvent ActionListener
• TextEvent TextListener
• ComponentEvent ComponentListener
FocusEvent FocusListener
WindowEvent WindowListener
InputEvent
• KeyEvent KeyListener
• MouseEvent MouseListener
22
Action events
• action event: An action that has occurred on a GUI component.
The most common, general event type in Swing. Caused by:
• button or menu clicks,
• check box checking / unchecking,
• pressing Enter in a text field, ...
23
Implementing a listener
public class name implements ActionListener {
public void actionPerformed(ActionEvent event) {
code to handle the event;
}
}
24
Nested classes
• nested class: A class defined inside of another class.
• Usefulness:
Nested classes are hidden from other classes (encapsulated).
Nested objects can access/modify the fields of their outer object.
25
Nested class syntax
// enclosing outer class
public class name {
...
Only the outer class can see the nested class or make objects of it.
Each nested object is associated with the outer object that created it,
so it can access/modify that outer object's methods/fields.
• If necessary, can refer to outer object as OuterClassName.this
26
Static inner classes
// enclosing outer class
public class name {
...
Static inner classes are not associated with a particular outer object.
They cannot see the fields of the enclosing class.
Usefulness: Clients can refer to and instantiate static inner classes:
Outer.Inner name = new Outer.Inner(params);
27
GUI event example
public class MyGUI {
private JFrame frame;
private JButton stutter;
private JTextField textfield;
public MyGUI() {
...
stutter.addActionListener(new StutterListener());
}
...