Java GUI Components
Java GUI Components
Here's a simple and detailed Java code that implements various GUI components using Allman style.
This will include CheckBox, ToggleButton, RadioButton, ListView, ComboBox, ScrollPane, and
TextControl, with explanations for each part of the code.
Code:
java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
// Group the radio buttons together in a ButtonGroup (to allow only one selection)
ButtonGroup group = new ButtonGroup();
group.add(radioButton1);
group.add(radioButton2);
frame.add(radioButton1);
frame.add(radioButton2);
Detailed Explanation:
1. JCheckBox (CheckBox):
A checkbox is used to allow users to select one or more options from a set.
`JCheckBox checkBox = new JCheckBox("Accept Terms and Conditions");` creates a checkbox
labeled "Accept Terms and Conditions".
It is added to the frame with `frame.add(checkBox)`.
2. JToggleButton (Toggle Button):
A toggle button works like a checkbox, but it has a visual effect of switching between two
states (ON/OFF).
`JToggleButton toggleButton = new JToggleButton("Toggle Me");` creates a toggle button.
It is added to the frame with `frame.add(toggleButton)`.
3. JRadioButton (Radio Button):
Radio buttons allow users to select one option from a list of mutually exclusive choices.
Here, we create two radio buttons (`radioButton1` and `radioButton2`), and group them using
`ButtonGroup` so that only one button can be selected at a time.
`group.add(radioButton1);` and `group.add(radioButton2);` ensure mutual exclusivity.
The radio buttons are then added to the frame with `frame.add(radioButton1)` and
`frame.add(radioButton2)`.
4. JList (ListView):
`JList<String> listView = new JList<>(listItems);` creates a list from an array of items
(`listItems`).
The list is enclosed in a `JScrollPane` to make it scrollable if there are many items.
`frame.add(listScrollPane)` adds the scrollable list to the frame.
5. JComboBox (ComboBox):
A combo box (also known as a drop-down list) allows the user to select from a list of options.
`JComboBox<String> comboBox = new JComboBox<>(comboBoxItems);` creates a combo box with
choices.
It is added to the frame with `frame.add(comboBox)`.
6. JTextArea and JScrollPane (Text Control):
is used for multi-line text input. We define its size as 5 rows and 20 columns.
`JTextArea`
The text area is wrapped in a `JScrollPane` to allow scrolling when the text exceeds the visible
area.
`frame.add(scrollPane)` adds the scrollable text area to the frame.
7. Frame Settings:
`frame.setSize(400, 400);` sets the window size.
`frame.setVisible(true);` makes the frame visible.
Notes:
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 2/3
FlowLayout is used as the layout manager, which arranges components sequentially in a line. Each
component is added to the frame in the order it is created.
Allman Style: In this style, curly braces are placed on their own line. This helps in enhancing
readability by keeping the code structure clear.
Summary:
This Java code implements a GUI window that contains a variety of standard components: Checkboxes,
Toggle Buttons, Radio Buttons, ListView, ComboBox, ScrollPane, and TextArea. Each component is
simple to create and use, providing a straightforward way to interact with users through graphical
elements.
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 3/3