yewkh2010
Slide 1
Learning Outcomes
At the end of this topic, students should be able to:
yewkh2010
Slide 2
yewkh2010
Slide 3
Standard Classes
1. In Java, every object used is instantiated from a certain class.
a. The class could be written by ourselves.
b. The class could be imported from the Java Development Kit.
2. There are many GUI classes in J2SE so that we can just reuse
those classes for instantiating GUI objects to construct interface.
3. Examples:
a. Creating a button:
JButton button = new JButton(OK);
b. Creating a panel:
JPanel panel = new JPanel();
c. Adding button to panel:
panel.add(button);
yewkh2010
Slide 4
java.awt
1. These standard classes are organized under the following packages:
2. java.awt is a package. Contains all of the classes for creating GUI and
for painting graphics and images.
3. The root of all AWT
components is the class
Component.
java.awt
Component
Button, Canvas,
TextComponent, Label, etc..
Container
Window
Frame
Panel
Dialog
Applet
javax.swing
JFrame
yewkh2010
JWindow
JDialog
JApplet
Slide 5
javax.swing
1. AWT provides only basic GUI components.
2. Enhanced GUI components are in a different package:
javax.swing.*
javax. swing
JPanel
JMenuBar
JComponent
AbstractButton
JTextComponent
JLabel
JButton
JTextArea
JTextField
JToggleButton
JCheckBox
yewkh2010
JRadioButton
JPasswordField
Slide 6
awt or swing?
1. Since we have the classes to build GUI in both AWT and
Swing packages, which one do we use?
Answer:
Both.
Jcomponents for most widgets are from the Swing package.
Containers such as JFrame, JWindows
extends classes from AWT.
2. How? Simply import both the packages in our coding.
import java.awt.*;
import javax.swing.*;
yewkh2010
Slide 7
2.
3.
yewkh2010
Slide 8
Demo
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
import java.awt.*;
import javax.swing.*;
public class HelloGUI {
JFrame myFrame;
JLabel myLabel;
Container myPane;
public HelloGUI() {
myFrame = new JFrame("Hello GUI");
myPane = myFrame.getContentPane();
myLabel = new JLabel("Hello Java GUI! Welcome to my life.");
}
public void launchFrame(){
myPane.add(myLabel);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.pack();
myFrame.setVisible(true);
}
public static void main(String[] args) {
HelloGUI myHelloGUIObject = new HelloGUI();
myHelloGUIObject.launchFrame();
}
}
yewkh2010
Slide 9
Slide 10
A SIMPLE EXAMPLE
Create the following GUI:
yewkh2010
Slide 11
UML
MyGUI
-myFrame: JFrame
-myPane: Container
-myPanel: JPanel
-myButton1: JButton
-myButton2: JButton
+MyGUI()
+createAndShowGUI(): void
Instantiate the
objects.
Assemble and
show the objects.
Slide 12
Code Explained
1.
import java.awt.*;
1.
2.
import javax.swing.*;
2.
3.
3.
4.
5.
6.
7.
5.
myPanel.setBackground(Color.YELLOW);
7.
myPanel.add(myButton1);
8.
myPanel.add(myButton2);
9.
public MyGUI() {
10.
11.
myPane = myFrame.getContentPane();
12.
13.
10.
myFrame.pack();
11.
myFrame.setVisible(true);
12.
13.
14.
15.
myDemo.createAndShowGUI();
14.
16.
15.
17.
16.
myPane.add(myPanel);
6.
8.
9.
4.
}
}
yewkh2010
Slide 13
Code Explained
yewkh2010
Slide 14
Applies decorative
2. setDefaultCloseOperationJFrame.EXIT_ON_CLOSE)
3. pack()
4. setVisible(true)
5. add(myButton)
yewkh2010
Slide 15
Containers
1.
2.
yewkh2010
Slide 16
Containers
yewkh2010
Slide 17
Containers
Frame is a basic window
that typically has
decorations: the border, a
title, and buttons for
closing or iconifying the
window.
Pane is the visible area
on the frame where we
place the widgets.
Panel is an intermediate
container which must be
placed in another
container such as frame.
It can hold more than one
widgets.
yewkh2010
Analogy
Panels.
Panels.
yewkh2010
Slide 19
1.
JrootPane object is created when one of the top-level Swing containers:- JFrame,
JinternalFrame, JApplet and JDialog is instantiated.
2.
b.
c.
yewkh2010
Slide 20
getContentPane method
1.
2.
3.
4.
5.
6.
yewkh2010
Slide 21
Panel Example
1. import java.awt.*;
2. import javax.swing.*;
3.
4. public class Demo_Panel {
5.
private JFrame myFrame;
6.
private Container myPane;
7.
private JPanel myPanel1,myPanel2;
8.
9.
public Demo_Panel() {
10.
myFrame = new JFrame("My Window");
11.
myPane = myFrame.getContentPane();
12.
myPanel1 = new JPanel();
13.
myPanel2 = new JPanel();
14.
}
15. //see next page
yewkh2010
Slide 22
Panel Example
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
yewkh2010
Widgets
1.
2.
3.
All the widgets in Java extend the Jcomponent class. Hence, they all share some
common properties:
a.
Border getBorder();
b.
c.
d.
e.
f.
g.
h.
i.
j.
k.
yewkh2010
Slide 24
JLabel
1. JLabel myLabel1 = new JLabel(Hello);
2. panel.add(myLabel1);
3. myLabel1.setName(Login name:);
yewkh2010
Slide 25
JTextField
1.
2.
3.
yewkh2010
JTextField textField;
textField = new JTextField(jTextField1");
panel.add(textField);
Slide 26
JCheckBox
1.
2.
3.
4.
5.
6.
7.
yewkh2010
Slide 27
JRadioButton
1.
2.
3.
4.
5.
6.
7.
yewkh2010
Slide 28
ButtonGroup
1.
2.
3.
4.
5.
6.
7.
yewkh2010
Slide 29
Combo Box
1.
2.
3.
yewkh2010
private String[] s1 =
{"None","Diploma","Bachelor","Master",PhD"};
cb1 = new JComboBox(s1);
cb1.setSelectedItem("None");
Slide 30
Scrollpane
1.
2.
a = new JTextArea();
scrol = new JScrollPane(a);
JTextArea
JScrollPane
yewkh2010
Slide 31
import java.awt.*;
import javax.swing.*;
public class AddMultipleWidgets{
JFrame myFrame;
JButton myButton;
JButton myButton2;
Container myPane;
public AddMultipleWidgets() {
myFrame = new JFrame(Login");
myButton = new JButton(Login");
myButton2 = new JButton(Clear");
myPane = myFrame.getContentPane();
}
public void createAndShowGUI(){
myPane.add(myButton);
myPane.add(myButton2);
myFrame.pack();
myFrame.setVisible(true);
}
yewkh2010
Slide 32
Answer
1.
2.
import java.awt.*;
import javax.swing.*;
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
myPane.add(myPanel);
20.
myPanel.add(myButton);
21.
myPanel.add(myButton2);
JFrame myFrame;
JButton myButton;
JButton myButton2;
Container myPane;
JPanel myPanel;
22.
myFrame.pack();
23.
myFrame.setVisible(true);
public AddMultipleWidgets() {
myFrame = new JFrame(Login");
myButton = new JButton(Login");
myButton2 = new JButton(Clear");
myPane = myFrame.getContentPane();
myPanel = new JPanel();
}
26.
yewkh2010
24.
25.
public static void main(String[] args) {
27.
AddMultipleWidgets Demo
28.
= new AddMultipleWidgets();
29.
Demo. createAndShowGUI();
30.
31.
}
}
Slide 33
Layout Manager
1.
2.
3.
4.
yewkh2010
Slide 34
yewkh2010
Slide 35
BorderLayout
1. BorderLayout is the default layout of the frame. Hence dont have to set
the layout.
2. The frame is divided into 5 regions:- CENTER, PAGE_START,
PAGE_END, LINE_START, and LINE_END.
3. Each region can store only one component. For example, if I try to
add more than one button in any one region, only the last added
button will be shown.
yewkh2010
Slide 36
FlowLayout
1.
2.
3.
Hence have to set the layout explicitly if we want the frame to have flow
layout instead of borderlayout.
4.
Example:
myFrame.setLayout(new FlowLayout());
5.
yewkh2010
Slide 37
GridLayout
1.
GridLayout is not the default layout of the frame. Hence have to set the
layout explicitly.
2.
Syntax:
SetLayout(new GridLayout(int rows, int cols));
3.
Examples:
yewkh2010
Slide 38
GridBagLayout
1. Similar to a grid layout, but provides a wide variety of options
for resizing and positioning the components.
2. For further details, read the J2SE API on GridBagLayout.
yewkh2010
Slide 39
GridBagLayout
button = new JButton("Button 2");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 1;
JButton button;
pane.setLayout(new GridBagLayout());
c.gridy = 0;
pane.add(button, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 2;
c.gridy = 0;
pane.add(button, c);
c.gridx = 0;
c.gridy = 0;
pane.add(button, c);
yewkh2010
Slide 40
GridBagLayout
button = new JButton("5");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 0;
//reset to default
c.anchor = GridBagConstraints.PAGE_END;
//bottom of space
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 40;
c.gridx = 1;
c.weightx = 0.0;
c.gridwidth = 3;
c.gridy = 2;
c.gridx = 0;
pane.add(button, c);
//third row
c.gridy = 1;
pane.add(button, c);
yewkh2010
Slide 41
BoxLayout
import java.awt.Component;
import java.awt.Container;
import javax.swing.BoxLayout;
import javax.swing.JButton;
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
import javax.swing.JFrame;
//Set up the content pane.
public class BoxLayoutDemo {
addComponentsToPane(frame.getContentPane());
frame.pack();
frame.setVisible(true);
launchGUI();
addAButton("5", pane);
}
}
}
button.setAlignmentX(Component.CENTER_ALIGNMENT);
container.add(button);
}
yewkh2010
Slide 42
2.
3.
4.
Example:
JFrame myFrame = new JFrame(My Software);
myFrame.setLayout(null);
myFrame.setSize(400,300);
5.
yewkh2010
Slide 43
Example
yewkh2010
Slide 44
Example
1.
import java.awt.*;
20.
myPane.setBackground(Color.BLUE);
2.
import javax.swing.*;
21.
myPanel.setLayout(null);
22.
myPanel.setSize(300,200);
23.
myPanel.setLocation(50,50);
24.
myPanel.setBackground(Color.GREEN);
3.
4.
JFrame myFrame;
5.
Container myPane;
26.
myButton.setSize(100,30);
6.
JButton myButton;
27.
myButton.setLocation(100,85);
7.
JPanel myPanel;
28.
myPane.add(myPanel);
29.
myPanel.add(myButton);
8.
9.
public AddOneWidget_NullLayout() {
10.
11.
12.
13.
14.
30.
31.
myFrame.setVisible(true);
32.
33.
34.
35.
36.
myDemo.createAndShowGUI();
37.
15.
16.
25.
38.
}
}
17.
myFrame.setLayout(null);
18.
myFrame.setSize(400,330);
19.
yewkh2010
Slide 45
What we have done so far is to build the Java technology GUI in a single class.
Below depicts the expected output and the corresponding UML describing the
design of the class.
MyWindow
-f: JFrame
-pane: Container
-panel: JPanel
-button1: JButton
+MyWindow()
+createAndShowGUI(): void
+public static main(String [] args): void
3.
yewkh2010
Slide 46
A better practice is to separate both widgets and intermediary containers (such as panel)
from the launcher which initializes frame into two separate classes. For example:
a.
b.
MyApp class the launcher class that instantiate the frame and later call getPanel().
Declare attributes for
class.
MyWindow
-f: JFrame
-pane: Container
-panel: JPanel
-button1: JButton
Initiate components.
+MyWindow()
+createAndShowGUI(): void
+public static main(String [] args): void
Assemble components.
Instantiate MyWindow and
launch the program.
MyGUI
MyApp
-panel: Jpanel
-button1: JButton
-f: JFrame
-pane: Container
Instantiate frame.
Call getPanel() method
from MyGUI object.
Launch the frame.
yewkh2010
<<Uses>>
+MyGUI()
+getPanel(): JPanel
Slide 47
import javax.swing.*;
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
public MyGUI()
{
button1 = new JButton(Exit");
panel = new JPanel();
panel.add(button1);
}
JPanel getPanel()
{
return panel;
}
Initiate components.
Then assemble.
yewkh2010
Slide 48
import javax.swing.*;
import java.awt.*;
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
Resides in
Resides in
Initialize JFrame
object with parameter
Demo.
Use JFrame method
to retrieve the
content pane.
Instantiate the other
class MyGUI as
object.
yewkh2010
Slide 49
JOptionPane
1. JOptionPane class is available under the package javax.swing.
2. It is used to provide GUI that solicits user input.
3. There are various methods under the JOptionPane class:
a. showMessageDialog();
b. showConfirmDialog();
c. showInputDialog();
d. showOptionDialog();
4. The following slides are several examples of the usage.
yewkh2010
Slide 50
2.
3.
yewkh2010
Slide 51
yewkh2010
Slide 52
yewkh2010
Slide 53
yewkh2010
Slide 54
What happens if you try to click the Exit button from the
earlier example? Is there any response after we click the
button?
Answer: No. Because we have not yet registered the
listener and handler to the event source!
yewkh2010
Slide 55
CLICK EVENT
2.
JVM
ActionListener
3.
4.
5.
Take action!!!!
Do something!!!
yewkh2010
Slide 56
JAVA
yewkh2010
Then, I must
immediately execute
the handler.
JAVA
Slide 57
Quiz yourself
1. What is an event source? Give a few examples.
2. What is an event? Give a few examples.
3. Why do we add listeners to the components?
4. Who should listen to whose event?
5. Why dont we make the event source automatically listens to the
event instead of using methods of adding the listener explicity?
6. What should the program respond to the event? Take action?
What action? How?
7. What is the handler, really?
yewkh2010
Slide 58
Event Handler
1. The event handler object is instantiated from the handler
class that implements the listener interface.
2. The handler class must contain all overriding method for the
interface, such as public void actionPerformed(ActionEvent event){}
yewkh2010
<<interface>>
Interface01
+method01():void
<<interface>>
ActionListener
+actionPerformed(ActionEvent event):void
MyClass
+method01():void
MyClass
+actionPerformed(ActionEvent event):void
Generic Interface
Implementation
Implementing
ActionListener inteface
Slide 59
Event Handler
Two flavors of implementations:
GUI and handler in the same class.
GUI and handler in separate classes.
<<interface>>
ActionListener
+actionPerformed(ActionEvent event):void
<<interface>>
ActionListener
+actionPerformed(ActionEvent event):void
MyGUIClass
+actionPerformed(ActionEvent event):void
MyActionHandler
+actionPerformed(ActionEvent event):void
<<uses>>
MyGUI
+method1():void
yewkh2010
Slide 60
Event Handling
There can be one or
multiple handlers for
one single event.
yewkh2010
Slide 61
1.
2.
3.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
public MyActiveGUI() {
button1 = new JButton(Exit);
button1.addActionListener(this);
panel = new JPanel();
panel.add(button1);
}
JPanel getPanel() { }
14.
15.
16.
17.
18.
19.
20.
21.
22.
Abide to a contract:
You will be recognized as a
listener.
You will get notification.
But you must implement a
method that
acts upon the notification.
yewkh2010
Slide 62
yewkh2010
Slide 63
import
import
import
import
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
javax.swing.*;
javax.swing.JOptionPane;
java.awt.*;
java.awt.event.*;
public MultipleButtons(){
myFrame = new JFrame("Event Handling
Demo");
myPane = myFrame.getContentPane();
myPanel = new JPanel();
loginButton = new JButton("Login");
loginButton.addActionListener(this);
clearButton = new JButton("Clear");
clearButton.addActionListener(this);
exitButton = new JButton("Exit");
exitButton.addActionListener(this);
}
public void createAndShowGUI(){
myPanel.add(loginButton);
myPanel.add(clearButton);
myPanel.add(exitButton);
myPane.add(myPanel,BorderLayout.PAGE_END);
myFrame.pack();
myFrame.setVisible(true);
}
yewkh2010
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
ae){
Slide 64
getSource() method
1.
Example:
JButton but = (JButton) ae.getSource();
2.
3.
yewkh2010
Slide 65
3.
4.
5.
6.
7.
8.
9.
10.
yewkh2010
Slide 66
2.
This is done by simply implementing more interface and adding more handlers.
Category
Interface
Methods to be implemented
Action
ActionListener
actionPerformed(ActionEvent)
Item
ItemListener
itemStateChanged(ItemEvent)
Mouse
MouseListener
mousePressed(MouseEvent)
mouseReleased(MouseEvent)
mouseEntered(MouseEvent)
mouseExited(MouseEvent)
mouseClicked(MouseEvent)
Mouse motion
MouseMotionListener
mouseDragged(MouseEvent)
mouseMoved(MouseEvent)
Key
KeyListener
keyPressed(KeyEvent)
keyReleased(KeyEvent)
keyTyped(KeyEvent)
Mouse wheel
MouseWheelListener
mouseWheelMoved(MouseWheelEvent e)
Text
TextListener
textValueChanged(TextEvent)
yewkh2010
Slide 67
The above GUI listens to the mouse motion and mouse clicks.
yewkh2010
Slide 68
import javax.swing.*;
25.
2.
import java.awt.*;
26.
3.
import java.awt.event.*;
27.
tf.setText(s);
28.
4.
5.
private JFrame f;
29.
6.
30.
31.
tf.setText(s);
32.
33.
7.
public MultipleBehaviors() {
8.
9.
tf = new JTextField(30);
10.
34.
11.
35.
12.
36.
13.
f.add(label, BorderLayout.NORTH);
37.
14.
f.add(tf, BorderLayout.SOUTH);
38.
15.
f.addMouseMotionListener(this);
16.
f.addMouseListener(this);
39.
17.
f.setSize(300, 200);
40.
18.
f.setVisible(true);
41.
two.launchFrame();
42.
43.
19.
20.
21.
22.
23.
tf.setText(s);
24.
yewkh2010
Slide 69
yewkh2010
Slide 70
Slide 71
Multiple Windows
WindowMain.java
Window0.java
Window1.java
Slide 72
Window0.java
Window0
Slide 73
Window0.java
1.
import javax.swing.*;
2.
import java.awt.*;
3.
import java.awt.event.*;
4.
5.
6.
21.
22.
frame.setVisible(true);
23.
24.
25.
7.
8.
private JPanel
9.
panel;
26.
frame.setVisible(false);
27.
28.
}
}
10.
11.
12.
13.
14.
15.
16.
17.
pane.add(panel);
18.
frame.pack();
19.
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
20.
yewkh2010
Slide 74
Window1.java
Window1
Slide 75
Window1.java
1.
import javax.swing.*;
2.
import java.awt.*;
3.
import java.awt.event.*;
4.
5.
8.
9.
10.
private JPanel
13.
panel;
14.
15.
25.
26.
27.
frame.setVisible(true);
28.
29.
30.
31.
frame.setVisible(false);
32.
33.
}
}
16.
17.
18.
19.
20.
21.
pane.add(panel);
22.
frame.pack();
23.
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
24.
yewkh2010
Slide 76
WindowMain.java
MyGUI_WindowMain <<implements ActionListener>>
-//declare all the required WIDGET fields here
-//declare all the required CONTAINER fields here
~//declare MyGUI_Window0 field here
~//declare MyGUI_Window1 field here
+MyGUI_WindowMain()
+actionPerformed(ActionEvent ae): void
<<static>>+main(String [] args): void
WindowMain.java
1.
2.
3.
4.
5.
private JPanel
6.
7.
Window0 win1;
8.
Window1 win2;
panel;
9.
10.
11.
12.
13.
panel.add(button0);
14.
15.
16.
panel.add(button1);
17.
18.
19.
frame.pack(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
20
21.
22.
yewkh2010
23. }
Create instances of
Window0 and Window1,
but don't show yet!
Slide 78
WindowMain.java
24.
25.
26.
27.
if (jb == button0){
28.
win1.showFrame();
29.
win2.hideFrame();
30.
31.
32.
win2.showFrame();
33.
win1.hideFrame();
34.
35.
Event handling
for both the
buttons.
}
}
36.
37.
38.
39.
wm.showFrame();
40.
41. }
yewkh2010
Slide 79
Summary
1.
2.
3.
4.
5.
6.
7.
yewkh2010
Slide 80
Q&A
yewkh2010
Slide 81
Extra Reading
Read up javadoc on java.awt.Toolkit.
yewkh2010
Slide 82