Swing Controls

Swing Controls Swing provides a set of controls (also known as components) to create interactive user interfaces. These controls are part of the javax.swing package and can be easily added to containers like JPanel and JFrame. Common Swing Controls Control Description JButton Button that triggers an action when clicked. JLabel Displays a short string or an image. JTextField A single-line text input field. JTextArea A multi-line text input field. JCheckBox A checkbox for binary choices (checked/unchecked). JRadioButton A radio button for selecting one option from a group. JComboBox A dropdown menu for selecting from a list of items. JList A list that allows selecting one or more items. Example: Using JButton and JLabel JFrame frame = new JFrame("Swing Controls"); JButton button = new JButton("Click Me"); JLabel label = new JLabel("Hello, World!"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { label.setText("Button Clicked!"); } }); frame.add(button, BorderLayout.NORTH); frame.add(label, BorderLayout.CENTER); frame.setSize(300, 200); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); Example: JCheckBox JCheckBox checkBox = new JCheckBox("Agree to terms"); checkBox.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { if (e.getStateChange() == ItemEvent.SELECTED) { System.out.println("Checked"); } else { System.out.println("Unchecked"); } } }); Example: JComboBox String[] items = { "Java", "Python", "C++", "JavaScript" }; JComboBox<String> comboBox = new JComboBox<>(items); comboBox.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String selectedItem = (String) comboBox.getSelectedItem(); System.out.println("Selected: " + selectedItem); } }); 🔗 Related Notes Swing Event Handling Event Listeners Swing

May 8, 2025 · 2 min · Rohan

Event Classes and Listener Interfaces

Event Classes and Listener Interfaces Java Swing uses event classes to encapsulate details about events and listener interfaces to respond to those events. This is the basis of Swing’s event delegation model. Event Classes Event classes are found in java.awt.event and javax.swing.event packages. Each class corresponds to a type of user action. Event Class Description ActionEvent Generated by buttons, menus, etc. MouseEvent Mouse actions like click, press KeyEvent Keyboard input WindowEvent Window state changes ItemEvent Checkbox, radio button changes public void actionPerformed(ActionEvent e) { String command = e.getActionCommand(); } Listener Interfaces Each event class has a corresponding listener interface. ...

May 8, 2025 · 1 min · Rohan

Event Listeners

Event Listeners Event Listeners are interfaces in Java Swing that define methods for responding to user actions or events. Components generate events, and listeners handle them. Commonly Used Listener Interfaces Listener Interface Description Method Signature ActionListener For button clicks and similar actions void actionPerformed(ActionEvent e) MouseListener Mouse click, enter, exit, etc. void mouseClicked(MouseEvent e) (and others) KeyListener Keyboard key press and release void keyPressed(KeyEvent e) (and others) WindowListener Window open, close, minimize, etc. void windowClosing(WindowEvent e) (and others) Example: ActionListener button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("Action performed!"); } }); Example: MouseListener label.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { System.out.println("Mouse clicked!"); } }); MouseAdapter is an abstract adapter class that lets you override only the needed methods. ...

May 8, 2025 · 1 min · Rohan

Swing Event Handling

Swing Event Handling Event Handling in Swing is based on the delegation event model, where an event source (component) notifies one or more listeners when an event occurs. Key Concepts Event Source: The component that generates an event (e.g., JButton) Event Listener: An interface that receives and processes the event (e.g., ActionListener) Event Object: Carries information about the event (e.g., ActionEvent) Example: Handling Button Click import javax.swing.*; import java.awt.event.*; public class EventExample { public static void main(String[] args) { JFrame frame = new JFrame("Event Example"); JButton button = new JButton("Click Me"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("Button clicked!"); } }); frame.add(button); frame.setSize(200, 150); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } Lambda Version (Java 8+) button.addActionListener(e -> System.out.println("Clicked!")); Event Flow User interacts with a component (e.g., clicks a button). Component generates an event object. Event object is passed to the registered listener’s method. 🔗 Related Notes Event Listeners Event Classes and Listener Interfaces Swing Controls

May 8, 2025 · 1 min · Rohan

Layout Managers

Layout Managers Layout Managers in Java Swing control the positioning and sizing of components within a container. They help in creating consistent GUI designs that adjust gracefully to different screen sizes. Common Layout Managers 1. FlowLayout (default for JPanel) Places components in a row, wraps to the next row when full. setLayout(new FlowLayout()); 2. BorderLayout (default for JFrame) Divides the container into five regions: NORTH, SOUTH, EAST, WEST, CENTER. setLayout(new BorderLayout()); add(new JButton("North"), BorderLayout.NORTH); 3. GridLayout Places components in a grid with equal-sized cells. ...

May 8, 2025 · 1 min · Rohan

Components and Containers

Components and Containers In Swing, Components are the visual elements like buttons, labels, and text fields, while Containers hold and organize these components. Components Components are instances of classes that inherit from java.awt.Component. Common examples include: JButton JLabel JTextField JCheckBox JComboBox JButton button = new JButton("Click Me"); JLabel label = new JLabel("Hello"); Containers Containers are components that can hold other components (including other containers). Examples: JFrame – Top-level window JPanel – Generic lightweight container JDialog – Dialog window JPanel panel = new JPanel(); panel.add(button); frame.add(panel); Nesting Components You can nest components within containers to create structured layouts. ...

May 8, 2025 · 1 min · Rohan

Swing

Swing Swing is a part of Java Foundation Classes (JFC) used to create Graphical User Interfaces (GUIs). It builds on AWT (Abstract Window Toolkit) but provides a richer set of components and is more flexible. Key Features Lightweight Components (do not rely on native OS peers) Pluggable Look-and-Feel MVC Architecture Highly customizable and extensible Basic Structure import javax.swing.*; public class HelloSwing { public static void main(String[] args) { JFrame frame = new JFrame("Swing Example"); JButton button = new JButton("Click Me!"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(300, 200); frame.add(button); frame.setVisible(true); } } Core Components JFrame – Top-level window JButton – Push button JLabel – Display text JTextField – Single-line input JPanel – Generic container Benefits of Swing Cross-platform consistency Rich component library Backed by event-driven programming 🔗 Related Notes Components and Containers Layout Managers Swing Event Handling

May 8, 2025 · 1 min · Rohan