-
Notifications
You must be signed in to change notification settings - Fork 0
JButton ActionListener
A JButton is a clickable button in Java Swing. It allows users to trigger actions when clicked.
✔️ Provides a simple interactive component
✔️ Supports event handling with ActionListener
✔️ Can be customized (size, icon, text, color, etc.)
import javax.swing.*;
public class JButtonExample {
public static void main(String[] args) {
// Create frame
JFrame frame = new JFrame("JButton Example");
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create button
JButton button = new JButton("Click Me");
// Add button to frame
frame.add(button);
// Make frame visible
frame.setVisible(true);
}
}
-
JButton button = new JButton("Click Me");
→ Creates a button with text. -
frame.add(button);
→ Adds the button to the frame. - Clicking the button does nothing yet (we need an event listener).
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ButtonClickExample {
public static void main(String[] args) {
JFrame frame = new JFrame("ActionListener Example");
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button = new JButton("Click Me");
// Add ActionListener to the button
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "Button Clicked!");
}
});
frame.add(button);
frame.setVisible(true);
}
}
✔️ button.addActionListener(...)
→ Registers a click event.
✔️ JOptionPane.showMessageDialog(...)
→ Shows a popup message.
📌 Now, when you click the button, a message appears!
Java 8+ allows lambda expressions, making event handling cleaner.
button.addActionListener(e -> JOptionPane.showMessageDialog(null, "Button Clicked!"));
✔️ Less code
✔️ Easier to read
✔️ Works only in Java 8+
import javax.swing.*;
import java.awt.event.ActionListener;
public class MultipleButtonsExample {
public static void main(String[] args) {
JFrame frame = new JFrame("Multiple Buttons");
frame.setSize(400, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
JButton btn1 = new JButton("Say Hello");
JButton btn2 = new JButton("Say Goodbye");
// Add event handlers
btn1.addActionListener(e -> JOptionPane.showMessageDialog(null, "Hello!"));
btn2.addActionListener(e -> JOptionPane.showMessageDialog(null, "Goodbye!"));
// Add buttons to panel
panel.add(btn1);
panel.add(btn2);
frame.add(panel);
frame.setVisible(true);
}
}
✔️ btn1
→ Shows "Hello!" when clicked.
✔️ btn2
→ Shows "Goodbye!" when clicked.
✔️ Multiple buttons can have different actions.
import javax.swing.*;
import java.awt.*;
public class StyledButtonExample {
public static void main(String[] args) {
JFrame frame = new JFrame("Styled Button");
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button = new JButton("Click Me");
button.setFont(new Font("Arial", Font.BOLD, 16));
button.setBackground(Color.BLUE);
button.setForeground(Color.WHITE);
frame.add(button);
frame.setVisible(true);
}
}
✔️ setFont(new Font("Arial", Font.BOLD, 16));
→ Sets font style & size.
✔️ setBackground(Color.BLUE);
→ Sets background color.
✔️ setForeground(Color.WHITE);
→ Sets text color.
📌 You can fully customize buttons in Swing!
import javax.swing.*;
public class DisableButtonExample {
public static void main(String[] args) {
JFrame frame = new JFrame("Disable Button Example");
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button = new JButton("Click Me");
button.addActionListener(e -> {
button.setEnabled(false); // Disable the button
JOptionPane.showMessageDialog(null, "Button Disabled!");
});
frame.add(button);
frame.setVisible(true);
}
}
✔️ button.setEnabled(false);
→ Disables the button after clicking.
✔️ setEnabled(true);
→ Re-enables the button (if needed).
JButton exitButton = new JButton("Exit");
exitButton.addActionListener(e -> System.exit(0));
✔️ Closes the app when clicked.
✔️ System.exit(0);
→ Terminates the program.
✅ JButton
creates interactive buttons.
✅ ActionListener
handles button clicks.
✅ Use lambda expressions for simpler event handling.
✅ Customize buttons with text, color, and font.
✅ Disable/Enable buttons as needed.