Professional Documents
Culture Documents
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.
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.
• 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.
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:
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(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(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.
• Sample Program:
Output:
Before entering text and clicking the button: After entering text and clicking the button:
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.