Module 5 - Part 1 Swing Controls
Module 5 - Part 1 Swing Controls
Page 2
Swing Controls
Page 3 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Swing Controls
► ImageIcon
► The ImageIcon component creates an icon sized-image from an image
residing at the source URL.
► Example: ImageIcon homeIcon = new
ImageIcon(“src/images/home.jpg”);
Page 4 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Swing Controls
► Jbutton
► JButton class is used to create a push-button on the UI. The button can
contain some display text or image. It generates an event when clicked
and double-clicked.
► A JButton can be implemented in the application by calling one of its
constructors.
► Example: JButton okBtn = new JButton(“Ok”);
► This constructor returns a button with text Ok on it.
► JButton homeBtn = new JButton(homeIcon);
► It returns a button with a homeIcon on it.
► JButton btn2 = new JButton(homeIcon, “Home”);
► It returns a button with the home icon and text Home.
Page 5 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Swing Controls
► JLabel
► JLabel class is used to render a read-only text label or images on the UI.
It does not generate any event.
► Example: JLabel textLbl = new JLabel(“This is a text label.”);
Page 6 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Swing Controls
► JTextField
► JTextField renders an editable single-line text box.
► A user can input non-formatted text in the box.
► To initialize the text field, call its constructor and pass an optional
integer parameter to it.
► This parameter sets the width of the box measured by the number of
columns.
► It does not limit the number of characters that can be input in the box.
► Example: JTextField txtBox = new JTextField(20);
► It renders a text box of 20 column width.
Page 7 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Swing Controls
► JTextArea
► JTextArea class renders a multi-line text box.
► Similar to the JTextField, a user can input non-formatted text in the field.
The constructor for JTextArea also expects two integer parameters
which define the height and width of the text-area in columns.
► It does not restrict the number of characters that the user can input in
the text-area.
► Example:
► JTextArea txtArea = new JTextArea(“This text is default text for text area.”, 5, 20);
► The above code renders a multi-line text-area of height 5 rows and width 20
columns, with default text initialized in the text-area.
Page 8 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Swing Controls
► JPasswordField
► JPasswordField is a subclass of JTextField class.
► It renders a text-box that masks the user input text with bullet points.
This is used for inserting passwords into the application.
► Example:
► JPasswordField pwdField = new JPasswordField(15);
► var pwdValue = pwdField.getPassword();
► It returns a password field of 15 column width. The getPassword method gets the
value entered by the user.
Page 9 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Swing Controls
► JCheckBox
► JCheckBox renders a check-box with a label. The check-box has two
states – on/off. When selected, the state is on and a small tick is
displayed in the box.
► Example:
► CheckBox chkBox = new JCheckBox(“Show Help”, true);
► It returns a checkbox with the label Show Help. Notice the second
parameter in the constructor. It is a boolean value that indicates the default
state of the check-box. True means the check-box is defaulted to on state.
Page 10 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Swing Controls
► JRadioButton
► JRadioButton is used to render a group of radio buttons in the UI. A user
can select one choice from the group.
► Example:
Page 11 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Swing Controls
► JList
► JList component renders a scrollable list of elements. A user can select a
value or multiple values from the list. This select behavior is defined in
the code by the developer.
► Example
Page 12 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Swing Controls
► JComboBox
► JComboBox class is used to render a dropdown of the list of options.
► Example
► The default selected option can be specified through the setSelectedIndex method.
The above code sets Sydney as the default selected option.
Page 13 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Swing Controls
► JFileChooser
► JFileChooser class renders a file selection utility. This component lets a
user select a file from the local system.
► The above code creates a file chooser dialog and attaches it to the button. The button
click would open the file chooser dialog. The selected file is returned through the
getSelectedFile method.
Page 14 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Swing Layout Managers
Page 15
Swing Layout Managers
► Several AWT and Swing classes provide layout managers for
general use:
► BorderLayout
► BoxLayout
► CardLayout
► FlowLayout
► GridBagLayout
► GridLayout
► GroupLayout
► SpringLayout
Page 16 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Swing Layout Managers - BorderLayout
► A BorderLayout places components in up to five areas: top,
bottom, left, right, and center.
► All extra space is placed in the center area.
► Tool bars that are created using JToolBar must be created within
a BorderLayout container, if you want to be able to drag and
drop the bars away from their starting positions.
Page 17 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Swing Layout Managers - BoxLayout
► The BoxLayout class puts components in a single row or
column. It respects the components' requested maximum sizes
and also lets you align components.
Page 18 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Swing Layout Managers - FlowLayout
► FlowLayout is the default layout manager for every JPanel. It
simply lays out components in a single row, starting a new row if
its container is not sufficiently wide.
Page 19 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Swing Layout Managers - GridBagLayout
► GridBagLayout is a sophisticated, flexible layout manager.
► It aligns components by placing them within a grid of cells,
allowing components to span more than one cell.
► The rows in the grid can have different heights, and grid
columns can have different widths.
Page 20 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Swing Layout Managers - GridLayout
► GridLayout simply makes a bunch of components equal in size
and displays them in the requested number of rows and
columns.
Page 21 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Swing Layout Managers - GroupLayout
► GroupLayout is a layout manager that was developed for use by
GUI builder tools, but it can also be used manually.
► GroupLayout works with the horizontal and vertical layouts
separately.
► The layout is defined for each dimension independently.
Consequently, however, each component needs to be defined
twice in the layout.
Page 22 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Swing Layout Managers - SpringLayout
► SpringLayout is a flexible layout manager designed for use by
GUI builders.
► It lets you specify precise relationships between the edges of
components under its control.
Page 23 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Thank You
Prof. Sarju S
Department of Computer Science and Engineering
St. Joseph’s College of Engineering and Technology, Palai
Page 24