JButton ActionListener - potatoscript/JavaSwing GitHub Wiki
🔘 JButton & ActionListener
1️⃣ Introduction to JButton
A JButton is a clickable button in Java Swing. It allows users to trigger actions when clicked.
Why Use JButton?
✔️ Provides a simple interactive component
✔️ Supports event handling with ActionListener
✔️ Can be customized (size, icon, text, color, etc.)
2️⃣ Creating a Simple JButton
Example: Adding a Button to JFrame
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);
}
}
Explanation
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).
3️⃣ Handling Button Clicks with ActionListener
Example: Adding ActionListener to JButton
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);
}
}
Explanation
✔️ button.addActionListener(...) → Registers a click event.
✔️ JOptionPane.showMessageDialog(...) → Shows a popup message.
📌 Now, when you click the button, a message appears!
4️⃣ Using Lambda Expression (Simpler Syntax)
Java 8+ allows lambda expressions, making event handling cleaner.
Example: Using a Lambda for ActionListener
button.addActionListener(e -> JOptionPane.showMessageDialog(null, "Button Clicked!"));
✔️ Less code
✔️ Easier to read
✔️ Works only in Java 8+
5️⃣ Handling Multiple Buttons
Example: Multiple Buttons with Different Actions
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);
}
}
Explanation
✔️ btn1 → Shows "Hello!" when clicked.
✔️ btn2 → Shows "Goodbye!" when clicked.
✔️ Multiple buttons can have different actions.
6️⃣ Changing Button Text, Icon, and Style
Example: Customizing a JButton
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);
}
}
Explanation
✔️ 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!
7️⃣ Disabling & Enabling Buttons
Example: Disable Button After Click
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);
}
}
Explanation
✔️ button.setEnabled(false); → Disables the button after clicking.
✔️ setEnabled(true); → Re-enables the button (if needed).
8️⃣ Closing the Application with a Button
Example: Exit Button (System.exit(0))
JButton exitButton = new JButton("Exit");
exitButton.addActionListener(e -> System.exit(0));
✔️ Closes the app when clicked.
✔️ System.exit(0); → Terminates the program.
🔥 Summary
✅ 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.