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:TextForm.java

public static void main(String[] args) {
    String[] labels = { "First Name", "Middle Initial", "Last Name", "Age" };
    char[] mnemonics = { 'F', 'M', 'L', 'A' };
    int[] widths = { 15, 1, 15, 3 };
    String[] descs = { "First Name", "Middle Initial", "Last Name", "Age" };

    final TextForm form = new TextForm(labels, mnemonics, widths, descs);

    JButton submit = new JButton("Submit Form");

    submit.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.out.println(form.getText(0) + " " + form.getText(1) + ". " + form.getText(2) + ", age "
                    + form.getText(3));/* www  . ja v  a2s.  c om*/
        }
    });

    JFrame f = new JFrame("Text Form Example");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(form, BorderLayout.NORTH);
    JPanel p = new JPanel();
    p.add(submit);
    f.getContentPane().add(p, BorderLayout.SOUTH);
    f.pack();
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JDialog dialog;/*from   w  ww. jav  a 2  s .co  m*/
    JList jlist;
    ActionListener otherListener = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("current");
        }
    };
    JButton okButton = new JButton("OK");
    okButton.addActionListener(e -> close(true));
    JButton cancelButton = new JButton("Cancel");
    cancelButton.addActionListener(e -> close(false));
    jlist = new JList(new String[] { "A", "B", "C", "D", "E", "F", "G" });
    jlist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    jlist.setVisibleRowCount(5);
    JScrollPane scroll = new JScrollPane(jlist);
    JPanel buttonsPanel = new JPanel(new FlowLayout());
    buttonsPanel.add(okButton);
    buttonsPanel.add(cancelButton);
    JPanel content = new JPanel(new BorderLayout());
    content.add(scroll, BorderLayout.CENTER);
    content.add(buttonsPanel, BorderLayout.SOUTH);
    dialog = new JDialog((Frame) null, true);
    dialog.setContentPane(content);
    dialog.pack();
    dialog.getRootPane().registerKeyboardAction(otherListener, KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0),
            JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

    dialog.getRootPane().registerKeyboardAction(otherListener, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
            JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

    dialog.getRootPane().getInputMap().put(KeyStroke.getKeyStroke("SPACE"), "doSomething");
    dialog.getRootPane().getActionMap().put("doSomething", new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    });
    dialog.setVisible(true);
}

From source file:RegexTable.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Regexing JTable");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Object rows[][] = { { "A", "About", 44.36 }, { "B", "Boy", 44.84 }, { "C", "Cat", 463.63 },
            { "D", "Day", 27.14 }, { "E", "Eat", 44.57 }, { "F", "Fail", 23.15 }, { "G", "Good", 4.40 },
            { "H", "Hot", 24.96 }, { "I", "Ivey", 5.45 }, { "J", "Jack", 49.54 }, { "K", "Kids", 280.00 } };
    String columns[] = { "Symbol", "Name", "Price" };
    TableModel model = new DefaultTableModel(rows, columns) {
        public Class getColumnClass(int column) {
            Class returnValue;/*from www  .ja va 2  s  .  co  m*/
            if ((column >= 0) && (column < getColumnCount())) {
                returnValue = getValueAt(0, column).getClass();
            } else {
                returnValue = Object.class;
            }
            return returnValue;
        }
    };

    final JTable table = new JTable(model);
    final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model);
    table.setRowSorter(sorter);
    JScrollPane pane = new JScrollPane(table);
    frame.add(pane, BorderLayout.CENTER);

    JPanel panel = new JPanel(new BorderLayout());
    JLabel label = new JLabel("Filter");
    panel.add(label, BorderLayout.WEST);
    final JTextField filterText = new JTextField("A");
    panel.add(filterText, BorderLayout.CENTER);
    frame.add(panel, BorderLayout.NORTH);
    JButton button = new JButton("Filter");
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String text = filterText.getText();
            if (text.length() == 0) {
                sorter.setRowFilter(null);
            } else {
                sorter.setRowFilter(RowFilter.regexFilter(text));
            }
        }
    });
    frame.add(button, BorderLayout.SOUTH);
    frame.setSize(300, 250);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    int TIME_VISIBLE = 3000;
    JFrame frame1 = new JFrame();
    frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame1.setSize(100, 100);/*  w  w  w.  j a va 2 s.co m*/
    frame1.setLocation(100, 100);

    JButton button = new JButton("My Button");
    frame1.getContentPane().add(button);

    button.addActionListener(e -> {
        JOptionPane pane = new JOptionPane("Message", JOptionPane.INFORMATION_MESSAGE);
        JDialog dialog = pane.createDialog(null, "Title");
        dialog.setModal(false);
        dialog.setVisible(true);

        new Timer(TIME_VISIBLE, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dialog.setVisible(false);
            }
        }).start();
    });

    frame1.setVisible(true);

}

From source file:Main.java

public static void main(String[] args) {
    JFrame f = new JFrame();
    final ConfirmDialog dialog = new ConfirmDialog(f);
    final JTree tree = new JTree();
    tree.setVisibleRowCount(5);//from w w w .  j  a  v a2 s . co m
    final JScrollPane treeScroll = new JScrollPane(tree);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JButton b = new JButton("Choose Tree Item");
    b.addActionListener(e -> {
        int result = dialog.showConfirmDialog(treeScroll, "Choose an item");
        if (result == ConfirmDialog.OK_OPTION) {
            System.out.println(tree.getSelectionPath());
        } else {
            System.out.println("User cancelled");
        }
    });
    JPanel p = new JPanel(new BorderLayout());
    p.add(b);
    p.setBorder(new EmptyBorder(50, 50, 50, 50));
    f.setContentPane(p);
    f.pack();
    f.setLocationByPlatform(true);
    f.setVisible(true);

}

From source file:Main.java

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

    f.setSize(300, 500);//from  www. j ava  2s . c o  m
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel pan = new JPanel(new GridLayout(1, 1));
    XmlJTree myTree = new XmlJTree(null);
    f.add(new JScrollPane(myTree));
    JButton button = new JButton("Choose file");
    button.addActionListener(e -> {
        JFileChooser chooser = new JFileChooser();
        FileNameExtensionFilter filter = new FileNameExtensionFilter("XML file", "xml");
        chooser.setFileFilter(filter);
        int returnVal = chooser.showOpenDialog(null);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            myTree.setPath(chooser.getSelectedFile().getAbsolutePath());
        }
    });
    pan.add(button);
    f.add(pan, BorderLayout.SOUTH);
    f.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();

    JButton closeButton = new JButton("Close");
    contentPane.add(closeButton);//from   ww  w . j  av  a2s .  c  o m

    closeButton.addActionListener(e -> System.exit(0));
    // Add a MouseListener to the JButton
    closeButton.addMouseListener(new MouseListener() {
        @Override
        public void mouseClicked(MouseEvent e) {
        }

        @Override
        public void mousePressed(MouseEvent e) {
        }

        @Override
        public void mouseReleased(MouseEvent e) {
        }

        @Override
        public void mouseEntered(MouseEvent e) {
            closeButton.setText("Mouse has  entered!");
        }

        @Override
        public void mouseExited(MouseEvent e) {
            closeButton.setText("Mouse has  exited!");
        }
    });
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

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

    JMenuBar menuBar = new JMenuBar();
    JMenu menu = new JMenu("File");
    menuBar.add(menu);/*  w ww.jav  a 2  s.  c o m*/
    JMenuItem menuItem = new JMenuItem("Print");
    KeyStroke ctrlP = KeyStroke.getKeyStroke(KeyEvent.VK_P, InputEvent.CTRL_MASK);
    menuItem.setAccelerator(ctrlP);
    menuItem.addActionListener(printAction);
    menu.add(menuItem);

    JButton fileButton = new JButton("About");
    fileButton.setMnemonic(KeyEvent.VK_A);
    fileButton.addActionListener(printAction);

    frame.setJMenuBar(menuBar);

    frame.add(fileButton, BorderLayout.SOUTH);
    frame.setSize(300, 100);
    frame.setVisible(true);
}

From source file:HelloWorldPrinter.java

public static void main(String args[]) {

    UIManager.put("swing.boldMetal", Boolean.FALSE);
    JFrame f = new JFrame("Hello World Printer");
    f.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);/*  w  ww .j  a v  a 2  s . co  m*/
        }
    });
    JButton printButton = new JButton("Print Hello World");
    printButton.addActionListener(new HelloWorldPrinter());
    f.add("Center", printButton);
    f.pack();
    f.setVisible(true);
}

From source file:ScreenDump.java

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

    JMenuBar menuBar = new JMenuBar();
    JMenu menu = new JMenu("File");
    menuBar.add(menu);//  ww  w .  j  a va 2 s  . com
    JMenuItem menuItem = new JMenuItem("Print");
    KeyStroke ctrlP = KeyStroke.getKeyStroke(KeyEvent.VK_P, InputEvent.CTRL_MASK);
    menuItem.setAccelerator(ctrlP);
    menuItem.addActionListener(printAction);
    menu.add(menuItem);

    JButton fileButton = new JButton("About");
    fileButton.setMnemonic(KeyEvent.VK_A);
    fileButton.addActionListener(printAction);

    frame.setJMenuBar(menuBar);

    Container contentPane = frame.getContentPane();
    contentPane.add(fileButton, BorderLayout.SOUTH);
    frame.setSize(300, 100);
    frame.setVisible(true);
}