Chapter 3 GUI in Java
Chapter 3 GUI in Java
Chapter 3 GUI in Java
2/Swing
• Second Edition
• Allow much power full computer graphics.
• Use prefix “J” to differentiate it from awt
components and containers
o To import packages containing swing component and container
import javax.swing.*;
Example JFrame,JDialog,JPanel,JApplet,JButton…..
2
AWT vs Swing
AWT is Java’s original set of classes for building
GUIs
Uses peer components of the OS; heavyweight
components. Each heavyweight component has a
peer (from package java.awt.peer) that is
responsible for the interactions between the
component and the local platform that display and
manipulate the component.
Not truly portable: looks different and lays out
inconsistently on different Oss
Due to OS’s underlying display management system
Trees (JTree)
minimize
maximize
close
Naming convention
It is common to name the content pane content or content
Pane.
Focus
To select a JTextField so that user typing occurs in that
field, you must give the JTextField focus. For example, the
nameField JTextField can be given focus with:
nameField.requestFocus();
Components Description
JButton This is a standard button which can have text,
icon, or both.
Listener: addActionListener(...)
Constructors
The usual constructor is JSlider s = new JSlider(orientation, min,
max, initial);
orientation: JSlider.HORIZONTAL or JSlider.VERTICAL
min: The minimum value.
max: The maximum value.
initial: The initial value.
Example:JSlider sl = new JSlider(JSlider.HORIZONTAL, 0, 20, 10);
frame.setJMenuBar(mbar);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
74 Advanced Programming with Java Chapter 1 02/21/2024
Dialogs
There are several ways to build dialog boxes:
JOptionPane - Simple Dialogs This is a very easy way
(one statement) to build simple dialogs. Usually this,
JFileChooser, and maybe JColorChooser are the only
dialog classes that you need.
JFileChooser This will create and control a standard file
chooser dialog.
javax.swing.JColorChooser You can call one static method
(Color.showDialog(. . .)) to display a dialog that lets the user
choose a color, or you can add listeners to make a more
complicated dialog interaction, or you can use this class to
create a color chooser pane.
javax.swing.Jdialog This can be used for building dialogs
that are too complicated for JOptionPane.
75 Advanced Programming with Java Chapter 1 02/21/2024
Dialogs(cont)
Dialogs are attached to window (frame)
Every dialog is attached to a window (frame). When the
window in iconified, the dialog will automatically
disappear, and it will automatically reappear when the
window is deiconified. When the window is destroyed, the
dialog will disappear.
Dialogs are usually modal
When a dialog is active, input to other parts of the
graphical user interface will be blocked. This kind of
dialog is called a modal dialog. If you want a dialog which
is modeless (allows interaction with other windows), you
must use the JDialog class.
Use null for the component parameter if you don't have a window
The dialog box will be centered over the component given in the first
parameter. Typically you would give the window over which it should
be centered. If your program doesn't have a window, you may simply
write null, in which case the dialog box will be centered on the
screen.
question
information
warning
error
JFrame JFrame
JPanel
containers
JPanel
JButton
JButton JLabel
JLabel
99 02/21/2024
Advanced Programming with Java Chapter 1
Cont’d
add(btnfive);
con.gridwidth = gridbagconstraints.remainder;
la.setconstraints(btnfive, con);
con.weighty = 0;
add(btnsix);
con.gridwidth = gridbagconstraints.remainser;
la.setconstraints(btnsix, con);
add(btnseven); add(btneight); add(btnnine);
con.gridwidth = 1;
con.weighty = 2.0;
la.setconstraints(btnseven, con);
la.setconstraints(btneight, con);
la.setconstraints(btnnine, con);
add(btnten);
con.gridwidth = gridbagconstraints.remainder;
la.setconstraints(btnten, con);
100 Advanced Programming with Java Chapter 1 02/21/2024
t t wo
f P a r
E n d o r t
x t P a
Ne t
E v e n
When you define a user interface, you will usually have some way to
get user input. For example, buttons, menus, sliders, mouse
clicks, ... all generate events when the user does something with
them.
Events can be
Keystrokes
mouse clicks, or Mouse moving over the component etc
103 Advanced Programming with Java Chapter 1 02/21/2024
Events Handling
In the event handling process, there are three important players
a) Event Source
b) Event Listener/Handler
c) Event Object
myGreetingField.setText("Guten Tag");
}
111 Advanced Programming with Java Chapter 1 02/21/2024
}
Inner-class Listeners(cont)
Cont…
119 Advanced Programming with Java Chapter 1 02/21/2024
Top-level Listeners(cont)
public void actionPerformed(ActionEvent a){
String whichButton = a.getActionCommand();
JOptionPane.showMessageDialog(null, "You clicked! & the button is :" +
whichButton);
}
public static void main(String[] a){
NewClass n = new NewClass();
}
}//end of class