/ Published in: Java
This code snippet shows how to handle multiple JButtons with a single ActionListener.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JButton; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; Main main = new Main(); } public Main(){ button1.setText("Button 1"); button1.addActionListener(this); button2.setText("Button 2"); button2.addActionListener(this); panel.add(button1); panel.add(button2); frame.getContentPane().add(panel); frame.setSize(200,200); frame.setVisible(true); } switch(e.getActionCommand()){ case "Button 1": break; case "Button 2": break; } } }
URL: http://www.snippetcentral.net/java/multiple-jbuttons-with-a-single-actionlistener.html