You are on page 1of 6

IT1712

User Interface 1
Swing Components
• A user interface component allows user to interact with a program.
• JAVA’s basic GUI programming involves two (2) packages: Abstract Window Toolkit (AWT) and Swing.
• UI components are also known as controls or widgets.
• To use the Swing components, the package javax.swing is used.
• A container is a type of component that holds other components; this groups them into a single entity.
• Most Commonly Used Swing Components:
Component Description
JFrame A window where other components are displayed
JLabel Holds text that can be displayed
JTextField Allows user to type a single line of text data
JButton Results in an action once clicked
JCheckBox Allows user to select one or more options from a set
JRadioButton Limits user to select only one item
JComboBox Has a display area that shows a default option and a list box that contains
additional options

JFrame
• A JFrame is used to hold other components that will be displayed in the program. It has a title and
size, and its visibility can be configured.
• Constructors of the JFrame Class:
o JFrame() – creates an invisible JFrame without a title.

o JFrame(String title) – constructs an invisible JFrame with a title.

o JFrame(GraphicsConfiguration gc) – creates a JFrame in the specified


GraphicsConfiguration of a screen device with a blank title. The GraphicsConfiguration class
represents the capabilities and modes of graphics devices such as monitor or printer.
o JFrame(String title, GraphicsConfiguration gc) – constructs a JFrame with a
title and the specified GraphicsConfiguration of a screen.
• Methods of JFrame:
Method Description
void setTitle(String) Sets the title of the JFrame
void setSize(int, int) Sets the width and height of the JFrame in pixels (picture
elements that make up the image on the computer
monitor)
void setSize(Dimension) Sets the width and height of the JFrame using a
Dimension class object
String getTitle() Returns the title of the JFrame
void setResizable(boolean) If set to true, the JFrame is resizable
If set to false, the JFrame is not resizable
boolean isResizable() Indicates whether the JFrame is resizable by returning
either true or false
void setVisible(boolean) If set to true, the JFrame is visible
If set to false, the JFrame is not visible
void setBounds(int, int, The first two arguments set the horizontal and vertical
int, int) positions of the JFrame while the final two (2) set its width
and height.

07 Handout 1 *Property of STI


Page 1 of 6
IT1712

Sample Program:

Output:

JLabel
• JLabel is a component used for displaying text.
• A JLabel (just like other components) is added to the JFrame using the add() method.
• Constructors of the JLabel Class:
o JLabel() – constructs a JLabel without a title and image.

o JLabel(String text) – creates a JLabel with the specified text.

o JLabel(Icon image) – creates a JLabel with the specified image.

o JLabel(String text, int horizontalAlignment) – constructs a JLabel with the


specified text and horizontal alignment.
o JLabel(Icon image, int horizontalAlignment) – constructs a JLabel with the
specified image and horizontal alignment.

o JLabel(String text, Icon icon, int horizontalAlignment) – constructs a


JLabel with the specified text, image, and horizontal alignment.

• The text in a JLabel can be modified using the setText() method and can be retrieved using the
getText() method.
• The JLabel and other components can be omitted from the JFrame through the remove() method.
To add or remove a component for a container, one of these methods must be called: invalidate(),
validate(), and repaint().
• The setFont() method of the Font class (included in java.awt) allows you to change the typeface,
style, and size of a font.

07 Handout 1 *Property of STI


Page 2 of 6
IT1712

• Arguments of the Font Class:


o Typeface – represents the font face. Examples are Arial, Century, and Times New Roman.
o Style – applies an attribute to displayed text and is one of three (3) values: Font.PLAIN,
Font.BOLD, or Font.ITALIC.
o Point size – an integer that represents about 1/72 of an inch. A printed text is commonly 12
points while a headline might be 30 points.

Layout Manager
• A layout manager controls the positioning of the components.
• FlowLayout is the basic layout manager. This places components in a row, and when a row is filled,
components are automatically placed into the next row.
Sample Code:

Output:

• How components are positioned in each row of their container can be specified by choosing one (1)
among three (3) constants: FlowLayout.LEFT, FlowLayout.RIGHT, and FlowLayout.CENTER.
Sample Usage:

• Another way of creating a layout manager is:

07 Handout 1 *Property of STI


Page 3 of 6
IT1712

JTextField
• A JTextField allows a user to type a single line of text data (any characters you can enter from the
keyboard).
• Constructors of the JTextField Class:
o JTextField() - constructs an empty JTextField.

o JTextField(int columns) – creates an empty JTextField with a specified number of


columns.

o JTextField(String text) – constructs a new JTextField with an initial text.

o JTextField(String text, int columns) – creates a new JTextField with an initial text
and number of columns.

JButton
• A JButton is a component that the user can click for data input. It is typically partnered with a text
field.
• Constructors of the JButton Class:
o JButton() – creates a JButton without text.

o JButton(String text) – constructs a JButton with text.

o JButton(Icon icon) – creates a JButton with an icon (of type Icon or ImageIcon), without
text.

o JButton(String text, Icon icon) – constructs a JButton with initial text and an icon
(of type Icon or ImageIcon).

o JButton(Action a) – creates a JButton in which properties are taken from the Action
(class) supplied. An Action object is created if there are two (2) or more components that
perform the same function.
• Tool tips are popup windows that help users understand the purpose of a program’s components. A
tool tip appears when the user hovers the mouse pointer over the component.
• To display a tooltip, the setToolTipText() method is used.

Events
• An event occurs when the user performs an action on a component, such as entering text in a
JTextField or clicking a JButton.
• An event-driven program is a program that responds to actions generated by the user. It involves a
source and a listener.
• The source is a component on which an event is generated while the listener is an object that is
interested in an event.
Example: The text entered in the text field can be the source while the button can be the listener.
• To incorporate events in a program, the java.awt.event package is used.

07 Handout 1 *Property of STI


Page 4 of 6
IT1712

• Sample Program:

Output:
Before entering text and clicking the button: After entering text and clicking the button:

07 Handout 1 *Property of STI


Page 5 of 6
IT1712

• Common Event Listeners:


Listener Type of Events Example
ActionListener Action events Button clicks
AdjustmentListener Adjustment events Scroll bar moves
ChangeListener Change events Slider is repositioned
FocusListener Keyboard focus events Text field gains or loses focus
ItemListener Item events Checkbox changes status
KeyListener Keyboard events Text is entered
MouseListener Mouse events Mouse clicks
MouseMotionListener Mouse movement events Mouse rolls
WindowListener Window events Window closes
• Swing Components and their Associated Listener-Registering Methods:
Listener Listener-Registering Method(s)
JButton, JCheckBox, JComboBox, JTextField, addActionListener()
JRadioButton
JScrollBar addAdjustmentListener()
All Swing Components addFocusListener(),
addKeyListener(),
addMouseListener(),
addMouseMotionListener()
JButton, JCheckBox, JComboBox, JRadioButton addItemListener()
All Window and JFrame Components addWindowListener()
JSlider, JCheckBox addChangeListener()

JCheckBox
• A JCheckBox allows users to choose one (1) or more items. It contains a label positioned before a
square.
• Methods of the JCheckBox Class:
Method Description
void setText(String) Sets the text for the JCheckBox
String getText() Returns the JCheckBox text
void setSelected(boolean) Sets the state of the JCheckBox to true for selected or false for
unselected
boolean isSelected() Gets the current state (checked or unchecked) of the JCheckBox
• Constructors of the JCheckBox Class:
o JCheckBox() – creates a JCheckBox without a label.
o JCheckBox(String label) – creates a JCheckBox with a label.
o JCheckBox(String label, boolean state) – creates a JCheckBox (selected or
unselected) with a label.

JComboBox
• A JComboBox is a component that combines two (2) features: a display area showing a default option
and a list box that contains additional, alternate options.
• To add items in a JComboBox, the addItem() method is used.

References:
Baesens, B., Backiel, A. & Broucke, S. (2015). Beginning java programming: The object-oriented approach. Indiana: John
Wiley & Sons, Inc.
Farrell, J. (2014). Java programming, 7th Edition. Boston: Course Technology, Cengage Learning
Savitch, W. (2014). Java: An introduction to problem solving and programming, 7th Edition. California: Pearson Education,
Inc.

07 Handout 1 *Property of STI


Page 6 of 6

You might also like