Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Java GUI Components

This document presents a Java code example that implements various GUI components including Checkboxes, Toggle Buttons, Radio Buttons, ListView, ComboBox, ScrollPane, and TextArea. It provides detailed explanations for each component and how they are added to a JFrame, utilizing FlowLayout for arrangement. The code follows the Allman style for better readability and structure.

Uploaded by

agdanishr
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Java GUI Components

This document presents a Java code example that implements various GUI components including Checkboxes, Toggle Buttons, Radio Buttons, ListView, ComboBox, ScrollPane, and TextArea. It provides detailed explanations for each component and how they are added to a JFrame, utilizing FlowLayout for arrangement. The code follows the Allman style for better readability and structure.

Uploaded by

agdanishr
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

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.

Title: Java GUI Components Implementation with Checkboxes, Toggle Buttons,


Radio Buttons, ListView, ComboBox, ScrollPane, and Text Control

Code:

java

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class GUIExample


{
public static void main(String[] args)
{
// Create the frame for the GUI
JFrame frame = new JFrame("GUI Components Example");

// Set the layout manager


frame.setLayout(new FlowLayout());

// Set default close operation


frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// Create and add a Checkbox


JCheckBox checkBox = new JCheckBox("Accept Terms and Conditions");
frame.add(checkBox);

// Create and add a Toggle Button


JToggleButton toggleButton = new JToggleButton("Toggle Me");
frame.add(toggleButton);

// Create and add Radio Buttons


JRadioButton radioButton1 = new JRadioButton("Option 1");
JRadioButton radioButton2 = new JRadioButton("Option 2");

// 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);

// Create and add a ListView


String[] listItems = {"Item 1", "Item 2", "Item 3", "Item 4"};
JList<String> listView = new JList<>(listItems);
JScrollPane listScrollPane = new JScrollPane(listView);
frame.add(listScrollPane);

// Create and add a ComboBox


String[] comboBoxItems = {"Choice A", "Choice B", "Choice C"};
JComboBox<String> comboBox = new JComboBox<>(comboBoxItems);
frame.add(comboBox);

// Create and add a ScrollPane with TextArea for Text Control


Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 1/3
JTextArea textArea = new JTextArea(5, 20);
JScrollPane scrollPane = new JScrollPane(textArea);
frame.add(scrollPane);

// Set frame size and visibility


frame.setSize(400, 400);
frame.setVisible(true);
}
}

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

You might also like