1. About JButton
In swing, we can create a push button using the JButton class. We can watch for the click event on it using the ActionListener. When the user clicks the button, it produces the ActionEvent, and the event will go to the registered Listener.
2. About the Example
The below screenshot shows the Example which we will create here:
It is a simple example which has only two components in it. One is JButton and the other one JLabel. When user clicks the Flip Mode button, the Label changes its caption from Read Mode to Write Mode. Upon clicking the button again, the label restores its original caption Read Mode. So during each click, the Label will alternate between the text Read Mode & Write Mode. Let us proceed with the coding:
3. Create JButton
The below code creates the Button and JLabel.
1 2 3 4 5 6 7 8 9 10 11 12 |
//Sample 01: Set Size and Position setBounds(100, 100, 500, 300); Container ControlHost = getContentPane(); ControlHost.setLayout(new FlowLayout()); //Sample 02: Create Swing Button JButton btn = new JButton("Flip Mode"); JLabel lbl = new JLabel("Read Mode"); //Sample 03: Add the Controls to Content Pane ControlHost.add(lbl); ControlHost.add(btn); |
In line 7, we are creating the Jbutton. In the constructor, we passed a string which will appear as the button text. Next, we added this button to the JFrame Window. Note, the FlowLayout Manager manages the window. Also note the order in which we are adding the components. The same order the component flow occurs. Means Layout Manager will display the label before the button display.
4. Handle actionPerformed – Anonymous Inner Class
The Anonymous Inner class is the modern way of handling the Java Events. Here, in the below code, we create such a class for our JButton class. In this inner class, we handle provide the actionPerformed handler function. At line 38, we check the text of the current Label. Based on the outcome, we flip the label’s text. Note, when we use Anonymous Inner class for handling the component events, we no need to check the event originator. Because the inner class is a specific to the component.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
//Sample 04: Handle the Action Event //Adv 1: No need to check the Event Source //Adv 2: No need to store Lbl as class Instance btn.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { if (lbl.getText().equalsIgnoreCase("Read Mode") == true ) { lbl.setText("Write Mode"); } else { lbl.setText("Read Mode"); } } } ); |
5. Watch JButton Example as Youtube Video
6. Code Reference
6.1 MainEntry.java
1 2 3 4 5 6 7 8 |
package tube.coding.examples; public class MainEntry { public static void main(String[] args) { //Sample 07: Create Instance of JFrameDemo JButtonExample frame = new JButtonExample("JButton Example"); frame.setVisible(true); } } |
6.2 JButtonExample.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
package tube.coding.examples; import java.awt.Container; import java.awt.FlowLayout; import java.awt.HeadlessException; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; public class JButtonExample extends JFrame { public JButtonExample(String title) throws HeadlessException { super(title); //Sample 01: Set Size and Position setBounds(100, 100, 500, 300); Container ControlHost = getContentPane(); ControlHost.setLayout(new FlowLayout()); //Sample 02: Create Swing Button JButton btn = new JButton("Flip Mode"); JLabel lbl = new JLabel("Read Mode"); //Sample 03: Add the Controls to Content Pane ControlHost.add(lbl); ControlHost.add(btn); //Sample 04: Handle the Action Event //Adv 1: No need to check the Event Source //Adv 2: No need to store Lbl as class Instance btn.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { if (lbl.getText().equalsIgnoreCase("Read Mode") == true ) { lbl.setText("Write Mode"); } else { lbl.setText("Read Mode"); } } } ); } } |
Categories: Swing
Tags: ActionEvent, actionPerformed, JButton