Java JButton handle action event
import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JTextField; public class Main extends JFrame { JTextField field = new JTextField(); class ButtonListener implements ActionListener { boolean toggle = true; public void actionPerformed(ActionEvent e) { if (toggle) field.setText("true"); else//from w w w . ja v a 2 s .c o m field.setText("false"); toggle = !toggle; } } public Main() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton button = new JButton("Press me"); button.addActionListener(new ButtonListener()); getContentPane().add(button, BorderLayout.SOUTH); getContentPane().add(field, BorderLayout.NORTH); pack(); setVisible(true); } public static void main(String[] args) { Main st = new Main(); } }