Example usage for javax.swing JButton addActionListener

List of usage examples for javax.swing JButton addActionListener

Introduction

In this page you can find the example usage for javax.swing JButton addActionListener.

Prototype

public void addActionListener(ActionListener l) 

Source Link

Document

Adds an ActionListener to the button.

Usage

From source file:Main.java

public static void main(final String args[]) {
    JButton button = new JButton("Test");

    button.addActionListener(e -> System.out.println("Button pressed"));

    JOptionPane.showMessageDialog(null, button);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    JButton button = new JButton("Click me");
    button.addActionListener(e -> JOptionPane.showMessageDialog(frame, "Hello World!"));
    frame.getContentPane().add(button);/*  ww w  .  ja v  a2 s  . co m*/
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    final JFrame frame = new JFrame();
    frame.setUndecorated(true);/*from ww w  .  java  2 s  . co  m*/
    JButton button = new JButton("Minimize");
    button.addActionListener(e -> frame.setState(Frame.ICONIFIED));
    frame.add(button);
    frame.pack();
    frame.setVisible(true);
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}

From source file:Main.java

public static void main(final String args[]) {
    JButton button = new JButton("Test");

    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.out.println("Button pressed");
        }//from  w  ww.  ja  v  a2 s .  co  m
    });

    JOptionPane.showMessageDialog(null, button);
}

From source file:Main.java

public static void main(final String args[]) {
    JButton button = new JButton("Test");

    button.addActionListener(new ClickListener());
    JOptionPane.showMessageDialog(null, button);

    button.doClick();//  w w w  .j  a va 2 s . com
}

From source file:MyActionListener.java

public static void main(String[] args) {
    JFrame.setDefaultLookAndFeelDecorated(true);
    JDialog.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("ActionListener Test 1");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JButton button = new JButton("Register");
    button.addActionListener(new MyActionListener());
    frame.getContentPane().add(button);//from  w  w  w. j  av  a  2 s  . c om
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JComboBox<String> comboBox = new JComboBox<>(new String[] { "Something", "Stuff", "Beep" });
    JButton add = new JButton("Add item");
    add.addActionListener(new ActionListener() {

        @Override/*from   w ww.j a va2 s. c o m*/
        public void actionPerformed(ActionEvent e) {
            comboBox.addItem("Item");
        }
    });
    frame.add(comboBox);
    frame.add(add, BorderLayout.SOUTH);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JDesktopPane dp = new JDesktopPane();
    JInternalFrame inf = new JInternalFrame("Help", true, true, true, true);
    inf.setSize(200, 200);/*ww  w. jav a 2 s.  c o m*/
    inf.setVisible(true);
    dp.add(inf);

    JButton btn = new JButton("Click");
    btn.addActionListener(e -> JOptionPane.showInternalInputDialog(inf, "Hit me"));
    inf.add(btn);

    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setLayout(new BorderLayout());
    f.add(dp);
    f.setSize(400, 400);
    f.setVisible(true);
}

From source file:ActionListenerTest2.java

public static void main(String[] args) {
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JButton button = new JButton("Select File");
    button.addActionListener(new MyActionListener());
    frame.add(button);//from   ww w .  ja  v a 2  s. c o m

    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JPanel panel = new JPanel(new BorderLayout());
    final JTree tree = new JTree();
    panel.add(new JScrollPane(tree));
    JButton btn = new JButton("Press Me");
    btn.addActionListener(et -> {
        for (Enumeration e = ((TreeNode) tree.getModel().getRoot()).children(); e.hasMoreElements();) {
            TreeNode tn = (TreeNode) e.nextElement();
            tree.expandPath(new TreePath(((DefaultTreeModel) tree.getModel()).getPathToRoot(tn)));
        }/*from w  w  w  .ja  va 2  s.  c o m*/
    });
    panel.add(btn, BorderLayout.SOUTH);
    JFrame frame = new JFrame("");
    frame.getContentPane().add(panel);
    frame.setSize(300, 300);
    frame.setLocation(100, 100);
    frame.pack();
    frame.show();
}