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(String[] args) {
    JFrame frame = new JFrame("JFrame");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container contentPane = frame.getContentPane();

    // Add a close button
    JButton closeButton = new JButton("Close");
    contentPane.add(closeButton);// w  w  w .  j  av  a2  s  . c  o  m

    closeButton.addActionListener(e -> System.exit(0));

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

From source file:SimpleToolbar.java

public static void main(String[] args) {
    JFrame f = new JFrame();
    JMenuBar menubar = new JMenuBar();
    JMenu file = new JMenu("File");
    menubar.add(file);/*from  w ww.  ja v a  2 s.c o  m*/
    f.setJMenuBar(menubar);

    JToolBar toolbar = new JToolBar();
    ImageIcon icon = new ImageIcon("exit.png");
    JButton exit = new JButton(icon);
    toolbar.add(exit);
    exit.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            System.exit(0);
        }
    });

    f.add(toolbar, BorderLayout.NORTH);
    f.setSize(300, 200);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    JFileChooser fc = new JFileChooser();
    fc.setMultiSelectionEnabled(true);//from  ww w. j a  v  a 2s  . c o  m
    fc.setCurrentDirectory(new File("C:\\tmp"));
    JButton btn1 = new JButton("Show Dialog");
    btn1.addActionListener(e -> fc.showDialog(frame, "Choose"));
    JButton btn2 = new JButton("Show Open Dialog");
    btn2.addActionListener(e -> {
        int retVal = fc.showOpenDialog(frame);
        if (retVal == JFileChooser.APPROVE_OPTION) {
            File[] selectedfiles = fc.getSelectedFiles();
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < selectedfiles.length; i++) {
                sb.append(selectedfiles[i].getName());
                sb.append("\n");
            }
            JOptionPane.showMessageDialog(frame, sb.toString());
        }
    });
    JButton btn3 = new JButton("Show Save Dialog");
    btn3.addActionListener(e -> fc.showSaveDialog(frame));
    Container pane = frame.getContentPane();
    pane.setLayout(new GridLayout(3, 1, 10, 10));
    pane.add(btn1);
    pane.add(btn2);
    pane.add(btn3);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    JButton button = new JButton("Show Input Dialog Box");
    button.addActionListener(e -> {
        JTextField xField = new JTextField(5);
        JTextField yField = new JTextField(5);
        JPanel myPanel = new JPanel();
        myPanel.add(new JLabel("x:"));
        myPanel.add(xField);/*from ww w .  j  a va 2  s  .c om*/
        myPanel.add(Box.createHorizontalStrut(15));
        myPanel.add(new JLabel("y:"));
        myPanel.add(yField);

        int result = JOptionPane.showConfirmDialog(null, myPanel, "Please Enter X and Y Values",
                JOptionPane.OK_CANCEL_OPTION);
        if (result == JOptionPane.OK_OPTION) {
            System.out.println("x value: " + xField.getText());
            System.out.println("y value: " + yField.getText());
        }
    });
    JPanel panel = new JPanel();
    panel.add(button);
    frame.add(panel);
    frame.setSize(400, 400);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}

From source file:Main.java

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

    JButton button = new JButton("Bring me to top after 3 seconds");
    button.addActionListener(e -> triggerBringToFront(f, 3000));
    f.getContentPane().add(button);/*from w  ww .j  ava2 s.  co m*/

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

From source file:Main.java

public static void main(String[] args) {
    final JFrame frame = new JFrame();
    frame.setUndecorated(true);/*from  ww w.  ja v a  2s . com*/
    JButton button = new JButton("Close Me");
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    });

    frame.addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
            point.x = e.getX();
            point.y = e.getY();
        }
    });
    frame.addMouseMotionListener(new MouseMotionAdapter() {
        public void mouseDragged(MouseEvent e) {
            Point p = frame.getLocation();
            frame.setLocation(p.x + e.getX() - point.x, p.y + e.getY() - point.y);
        }
    });

    frame.setSize(300, 300);
    frame.setLocation(200, 200);
    frame.setLayout(new BorderLayout());

    frame.getContentPane().add(button, BorderLayout.NORTH);
    frame.getContentPane().add(new JLabel("Drag Me", JLabel.CENTER), BorderLayout.CENTER);
    frame.setVisible(true);
}

From source file:Main.java

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

    JButton loadButton = new JButton("Display Image");
    loadButton.addActionListener(ev -> {
        JFileChooser fc = new JFileChooser(System.getProperty("user.home"));
        fc.addChoosableFileFilter(//from w  ww .  j av a 2s  .co  m
                new FileNameExtensionFilter("Image files", new String[] { "png", "jpg", "jpeg", "gif" }));
        if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
            try {
                Image image = ImageIO.read(fc.getSelectedFile());
                if (image != null) {
                    JPanel panel = new JPanel(new BorderLayout(10, 10));
                    panel.add(new JLabel(fc.getSelectedFile().toString()), BorderLayout.NORTH);
                    panel.add(new JLabel(new ImageIcon(image)));
                    JOptionPane.showMessageDialog(frame, panel);
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    });

    frame.add(loadButton);
    frame.pack();
    frame.setLocationByPlatform(true);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame("JFrame");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container contentPane = frame.getContentPane();

    // Add a close button
    JButton closeButton = new JButton("Close");
    contentPane.add(closeButton);/*www.j a va  2  s. c o  m*/

    closeButton.addActionListener(e -> {
        System.out.println(e.getActionCommand());
        System.exit(0);
    });

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

From source file:MainClass.java

public static void main(final String args[]) {
    JFrame frame = new JFrame("Button Popup Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    ActionListener actionListener = new ShowPopupActionListener(frame);

    JButton start = new JButton("Pick Me for Popup");
    start.addActionListener(actionListener);
    frame.add(start);//  w  w w.  j  av a2 s  .  c o m

    frame.setSize(350, 250);
    frame.setVisible(true);
}

From source file:MyActionListener.java

public static void main(String args[]) {
    JFrame frame = new JFrame();

    JButton button = new JButton("Click and holding alt, control, and shift key");
    button.addActionListener(new MyActionListener());
    frame.add(button);//from  ww  w .jav a 2s . c o m
    frame.pack();
    frame.setVisible(true);
}