GUI in JAVA
GUI in JAVA
Java
Graphical User Interface
In Java, GUI-based programs are
implemented by using classes from the
javax.swing and java.awt packages.
import javax.swing.*;
public JFrameSubclass () {
setTitle("My First Subclass");
setSize(FRAME_WIDTH, FRAME_HEIGHT);
setLocation(FRAME_X_ORIGIN, FRAME_Y_ORIGIN);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
Code
import javax.swing.*; Create a subclass that
inherits JFrame class
public JFrameSubclass () {
setTitle("My First Subclass");
setSize(FRAME_WIDTH, FRAME_HEIGHT);
setLocation(FRAME_X_ORIGIN, FRAME_Y_ORIGIN);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
Code
Constant declarations
import javax.swing.*;
public JFrameSubclass () {
setTitle("My First Subclass");
setSize(FRAME_WIDTH, FRAME_HEIGHT);
setLocation(FRAME_X_ORIGIN, FRAME_Y_ORIGIN);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
Code
Constructor for this
import javax.swing.*; JFrameSubclass class
public JFrameSubclass () {
setTitle("My First Subclass");
setSize(FRAME_WIDTH, FRAME_HEIGHT);
setLocation(FRAME_X_ORIGIN,
FRAME_Y_ORIGIN);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
Using methods from JFrame
class
setTitle methods:
setTitle
public void setTitle(String title)
Sets the title for this frame to the specified string.
Example:
setTitle("My First Subclass”);
Using methods in JFrame
setSize(int, int)
public void setSize(int width, int height)
Resizes this component so that it has width and
height.
Example:
setSize(FRAME_WIDTH,
FRAME_HEIGHT);
Using methods in JFrame
setLocation
public void setLocation(int x, int y)
Moves this component to a new location. The top-left
corner of the new location is specified by the x and y
parameters in the coordinate space of this
component's parent.
Example: setLocation(FRAME_X_ORIGIN,
FRAME_Y_ORIGIN);
Using methods in JFrame
setDefaultCloseOperation:
Example:
setDefaultCloseOperation(EXIT_ON_CLOSE);
Using public constants in
JFrame
EXIT_ON_CLOSE
public static final int EXIT_ON_CLOSE
The Content Pane of a Frame
The content pane is where we put GUI objects such as
buttons, labels, scroll bars, and others.
We access the content pane by calling the frame’s
getContentPane method.
This
Thisgray
grayarea
areaisisthe
the
content pane of this
content pane of this
frame.
frame.
Changing the Background Color
Here's how we can change the background
color of a content pane to blue:
Container contentPane = getContentPane();
contentPane.setBackground(Color.BLUE);
Adding event-handling
Example
Create a button
Create a dumb GUI first
Add a module to handle event later
Create a dumb GUI
Code
import javax.swing.*;
import java.awt.*; Packages included for
event and GUI objects
import java.awt.event.*;
contentPane.setLayout(null);
contentPane.setBackground(Color.white);
Constructor (continue)
okButton = new JButton("OK");
okButton.setBounds(70,125,BUTTON_WIDTH,BUTTON_HEIGHT);
contentPane.add(okButton);
}
}
Event Handling
An action involving a GUI object, such as clicking a
button, is called an event.
The mechanism to process events is called event
handling.
The event-handling model of Java is based on the
concept known as the delegation-based event model.
With this model, event handling is implemented by
two types of objects:
event source objects
notify
JButton Handler
register
and so forth
addActionListener
JButton button = new JButton("OK");
ButtonHandler handler = new ButtonHandler( );
button.addActionListener(handler);
Review
A Java interface is different from a class
because it includes only
to specify the behavior and does not
include data members or
B. Implementation
A. Method declarations
of a method
Review
A Java interface is different from a class
because it includes only A
B. Implementation
A. Method declarations
of a method
Code for event-handling
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ButtonHandler implements ActionListener {
public ButtonHandler() { }
public void actionPerformed(ActionEvent event){
JButton clickedButton=(JButton) event.getSource();
JRootPane rootPane = clickedButton.getRootPane();
JFrame frame =(JFrame) rootPane.getParent();
Textfield
Code – GUI handler
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class GuiHandler implements ActionListener {
public GuiHandler() { }
public void actionPerformed(ActionEvent event){
}
}
}
Code - TextFrame
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
contentPane.setLayout(null);
contentPane.setBackground(Color.white);
cancelButton.setBounds(160,125,BUTTON_WIDTH,BUTTON_HEIGHT);
contentPane.add(cancelButton);
cancelButton.addActionListener(handler);
okButton.addActionListener(handler);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
The Java Interface
A Java interface includes only constants and abstract
methods.
An abstract method has only the method header, or
prototype. There is no method body. You cannot
create an instance of a Java interface.
A Java interface specifies a behavior.
A class implements an interface by providing the
method body to the abstract methods stated in the
interface.
Any class can implement the interface.
ActionListener Interface
When we call the addActionListener method of an event
source, we must pass an instance of a class that
implements the ActionListener interface.
The ActionListener interface includes one method named
actionPerformed.
A class that implements the ActionListener interface must
therefore provide the method body of actionPerformed.
Since actionPerformed is the method that will be called
when an action event is generated, this is the place
where we put a code we want to be executed in
response to the generated events.
Container as Event Listener
Instead of defining a separate event listener such as
ButtonHandler, it is much more common to have an object
that contains the event sources be a listener.
Example: We make this frame a listener of the
action events of the buttons it contains.
event listener
event source
GUI Classes for Handling Text
The Swing GUI classes JLabel, JTextField, and
JTextArea deal with text.
A JLabel object displays uneditable text (or image).
A JTextField object allows the user to enter a single
line of text.
A JTextArea object allows the user to enter multiple
lines of text. It can also be used for displaying
multiple lines of uneditable text.
JTextField
We use a JTextField object to accept a single line
to text from a user. An action event is generated
when the user presses the ENTER key.
The getText method of JTextField is used to
retrieve the text that the user entered.
JTextField input = new JTextField( );
input.addActionListener(eventListener);
contentPane.add(input);
JLabel
We use a JLabel object to display a
label.
A label can be a text or an image.
When
JLabel
creating an image label, we pass
textLabel = new JLabel("Please enter your name");
contentPane.add(textLabel);
ImageIcon object instead of a string.
JLabel imgLabel = new JLabel(new ImageIcon("cat.gif"));
contentPane.add(imgLabel);
JPasswordField
allows the editing of a single line of text
where the view indicates something was
typed, but does not show the original
characters.
JPasswordField passwordLine = new
JPasswordField();
passwordLine.setBounds(90,110,130,25);
contentPane.add(passwordLine);
Example – Let’s make this frame
JTextArea
We use a JTextArea object to display or allow the user to
enter multiple lines of text.
The setText method assigns the text to a JTextArea,
replacing the current content.
The append method appends the text to the current text.
JTextArea textArea
= new JTextArea( ); Hello
the lost world
. . .
textArea.setText("Hello\n");
textArea.append("the lost ");
textArea.append("world");
JTextArea
Let’s modify our example to add
this
Adding Scroll Bars to
JTextArea
By default a JTextArea does not have
any scroll bars. To add scroll bars, we
place a JTextArea in a JScrollPane
object.
JTextArea textArea = new JTextArea();
. . .
JScrollPane scrollText = new JScrollPane(textArea);
. . .
contentPane.add(scrollText);
TextArea with Scroll Bars
Placing GUI Objects on a Frame
BorderLayout
GridLayout
FlowLayout
In using this layout, GUI component share placed in
left-to-right order.
When the component does not fit on the same
ItemListener
Passed to method itemStateChanged
JCheckBox boldJCheckBox;
JCheckBox italicJCheckBox;
JMenu
JMenuItem
separator
Sequence for Creating Menus
1. Create a JMenuBar object and attach it
to a frame.
2. Create a JMenu object.
3. Create JMenuItem objects and add
them to the JMenu object.
4. Attach the JMenu object to the
JMenuBar object.
Handling Mouse Events
Mouse events include such user interactions as
moving the mouse