0% found this document useful (0 votes)
18 views

Lecture 13

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Lecture 13

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Faculty of Information Technology - Programming 2

Lecture 13
Graphical User Interface

Spring 2024 61FIT3PR2 – Programming 2 1


Introduction
• A graphical interface application is an application
that uses a graphical interface to communicate with
the user
• Import and display data from graphical components

Spring 2024 61FIT3PR2 – Programming 2 2


Java's support for GUI programming (1)
• Two APIs: AWT & Swing
• Abstract Window Toolkit (AWT):

(older) introduced since JDK 1.0
• decouple display component specifications from their
implementations
• heavy-weight: uses a native peer class for each
display component
• the peer classes are fixed for given platform has a
modular interaction model (JDK 1.1)

Spring 2024 61FIT3PR2 – Programming 2 3


Java's support for GUI programming (2)
• Swing:

introduced since JDK 1.2
improved from AWT
• light-weight: Java's implementation of the display (no
need for peer classes)
• look & feel of the display components can be
customised for a given platform
• has enhanced display components
extends AWT 1.1's interaction model

Spring 2024 61FIT3PR2 – Programming 2 4


Basic components of GUI
• Window Title
• Application window.

Container
Control
• Container Control
• Contains one or more controls
• Control

Container
Control Control

• TextField, Radio, CheckBox, Control Control


Buttons, Tables…
• Note:
• An application can have many
window.
• A container can contain multiple
controls or containers other
Spring 2024 61FIT3PR2 – Programming 2 5
Window
• In Swing, JFrame is used to create windows
• Related components
• Icon
• Title
• Minimize, maximize, and turn off windows

Spring 2024 61FIT3PR2 – Programming 2 6


Create a window object
• Directly using the JFrame class:
JFrame w = new JFrame();
JFrame w1 = new JFrame("First Window
Class");
• Indirectly via a sub-class of JFrame:
public class FirstWindow extends Jframe
{..}
Jframe w = new FirstWindow();

Spring 2024 61FIT3PR2 – Programming 2 7


Example
// Create a JFrame instance
JFrame frame = new JFrame();

// Set the title of the frame


frame.setTitle("Window Demo");

// Set the icon of the frame


ImageIcon icon = new ImageIcon("path/to/icon.png");
// Replace with your icon path
frame.setIconImage(icon.getImage());

// Set the size of the frame


frame.setSize(400, 300);

// Set the default close operation


frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Spring 2024 61FIT3PR2 – Programming 2 8


Container
• In Swing there are many types of Containers.
In this lecture we will only learn about JPanel
• JPanel is a container used to group other
controls or containers

Spring 2024 61FIT3PR2 – Programming 2 9


Control
• Controls are interface elements used to receive
data from the user or sender that presents data
to the user
• Commonly used Controls
• JLabel - Label
• JTextField – Input field
• JButton – Push button
• JCheckBox – Checkbox
• JRadioButton – Selection
• JComboBox – Selection box
• JTable – Table
• JOptionPane – Dialog box
• …
Spring 2024 61FIT3PR2 – Programming 2 10
JLabel
• JLabel is the control used to create labels.
• The most important attribute of Jlabel is Text
• When naming you need to start with lbl.

• lblUserName
• lblPassword

Spring 2024 61FIT3PR2 – Programming 2 11


JLabel

Spring 2024 61FIT3PR2 – Programming 2 12


JTextField, JTextArea and JPasswordField
• JTextField is used to create input
boxes. The two commonly used
properties are Text and Name
• Text: value of input box
• Name: name of the input box, should
start with txt
• For example:
➢ txtUsername
➢ txtPassword
• JTextArea is only different from
JTextField in that it allows it to
contain multi-line text
• JPasswordField does not allow the
content of the Text to be seen
Spring 2024 61FIT3PR2 – Programming 2 13
JTextField, JTextArea and JPasswordField

Spring 2024 61FIT3PR2 – Programming 2 14


JButton
• JButton is used to create push buttons. Commonly used
attributes
• Text: button label
• Name: name of the button, should start with btn
• For example: btnLogin

Spring 2024 61FIT3PR2 – Programming 2 15


JCheckBox
• JCheckBox is used to create checkboxes. Commonly
used attributes
• Text: Attached label
• Selected: Status
• Name: Name, should start with chk

Spring 2024 61FIT3PR2 – Programming 2 16


JCheckBox

Spring 2024 61FIT3PR2 – Programming 2 17


JRadioButton, ButtonGroup
• JRadioButton is used to create select items. Commonly
used attributes
• Text: Attached label
• Selected: Status
• Name: Name, should start with rdo
• ButtonGroup: the group to which the radio belongs. In
each group you can only select one radio.

Spring 2024 61FIT3PR2 – Programming 2 18


JRadioButton, ButtonGroup

Spring 2024 61FIT3PR2 – Programming 2 19


JComboBox
• JComboBox is used to create a drop-down list and only
allows selecting 1. Frequently used properties.
• Model: contains a list of data
• SelectedIndex: selected item location
• SelectedItem: selected item data
• Name: Name, should start with cbo ( cbNames)

Spring 2024 61FIT3PR2 – Programming 2 20


JComboBox

Spring 2024 61FIT3PR2 – Programming 2 21


JTable, DefaultTableModel
• JTable is used to represent tabular data. Commonly used
attributes
• Model: table data
• SelectionMode: mode for selecting rows
• RowHeight: height of each row
• Name: Name, should start with tbl ( tblBooks)

Spring 2024 61FIT3PR2 – Programming 2 22


JTable, DefaultTableModel

Spring 2024 61FIT3PR2 – Programming 2 23


JTable, DefaultTableModel

Spring 2024 61FIT3PR2 – Programming 2 24


Summary
• Gui Introduction
• Basic components of GUI
• Container
• JLabel
• JTextField, JTextArea and JPasswordField
• Jbutton
• JCheckBox
• JRadioButton, ButtonGroup
• JComboBox
• JTable, DefaultTableModel

Spring 2024 61FIT3PR2 – Programming 2 25

You might also like