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(Main.class.getSimpleName());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final JButton button = new JButton("Click me to open dialog");
    button.addActionListener(e -> {
        Window parentWindow = SwingUtilities.windowForComponent(button);
        JDialog dialog = new JDialog(parentWindow);
        dialog.setLocationRelativeTo(button);
        dialog.setModal(true);/* w ww . jav a  2  s  .  c om*/
        dialog.add(new JLabel("A dialog"));
        dialog.pack();
        dialog.setVisible(true);
    });
    frame.add(button);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String args[]) {
    DefaultTableModel model;/*from   w  w w. ja  v a 2 s.  co m*/
    JTable t = new JTable(model = new DefaultTableModel(0, 1));
    for (int i = 0; i < 10; i++) {
        model.addRow(new Object[] { i });
    }
    JButton removeSelected = new JButton("remove");
    removeSelected.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            int[] selectedRows = t.getSelectedRows();
            for (int i = selectedRows.length - 1; i >= 0; i--) {
                model.removeRow(selectedRows[i]);
                ;
            }
        }
    });
    JFrame f = new JFrame();
    f.add(new JScrollPane(t));
    f.add(removeSelected, BorderLayout.SOUTH);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.pack();
    f.setVisible(true);
}

From source file:Main.java

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

    JTextPane codearea = new JTextPane();
    JScrollPane scroll;//  w ww.  j  a  v a  2  s  .co m
    scroll = new JScrollPane(codearea, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
            ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    scroll.setPreferredSize(new Dimension(300, 300));

    JPanel panel = new JPanel(new BorderLayout());
    panel.add(scroll, BorderLayout.CENTER);
    JButton comp = new JButton("Print text");
    comp.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println(codearea.getText());
        }
    });
    panel.add(comp, BorderLayout.SOUTH);

    frame.getContentPane().add(panel);
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);

}

From source file:Main.java

public static void main(String[] args) {
    JTextPane jTextPane = new JTextPane();
    JFrame frame = new JFrame("Test");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.pack();/*  www . j a va 2s.  c  o  m*/
    frame.setVisible(true);

    JButton btnGetSelectedText = new JButton("Get selected text");
    btnGetSelectedText.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println(jTextPane.getSelectedText());
        }
    });
    frame.getContentPane().add(jTextPane, BorderLayout.NORTH);
    frame.getContentPane().add(btnGetSelectedText, BorderLayout.SOUTH);
}

From source file:Main.java

public static void main(String[] args) {
    JPanel panel = new JPanel(new GridBagLayout());
    for (int i = 0; i < 25; i++) {
        JTextField field = new JTextField("Field " + i, 20);
        GridBagConstraints constraints = new GridBagConstraints();
        constraints.gridy = i;//from  w ww  .  j  av  a 2  s  .  c om
        panel.add(field, constraints);
    }
    JScrollPane scrollPane = new JScrollPane(panel);
    JButton removeButton = new JButton("Remove Field");
    removeButton.addActionListener(e -> {
        if (panel.getComponentCount() >= 1) {
            panel.remove(panel.getComponentCount() - 1);
            scrollPane.revalidate();
            scrollPane.repaint();
        }
    });
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setSize(640, 480);
    f.setLocation(200, 200);
    f.getContentPane().add(scrollPane);
    f.getContentPane().add(removeButton, BorderLayout.SOUTH);
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    final JFrame parent1 = new JFrame("Parent Frame 1");
    parent1.setLayout(new FlowLayout());
    parent1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JButton button = new JButton("Application modal dialog");
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JDialog dialog = new JDialog(parent1, "Application-Modal Dialog",
                    Dialog.ModalityType.APPLICATION_MODAL);
            dialog.setBounds(200, 150, 200, 150);
            dialog.setVisible(true);/*from  w  w w .j av  a 2s. com*/
        }
    });
    parent1.add(button);
    parent1.setBounds(100, 100, 200, 150);
    parent1.setVisible(true);

    JFrame parent2 = new JFrame("Parent Frame 2");
    parent2.setBounds(500, 100, 200, 150);
    parent2.setVisible(true);
}

From source file:ButtonPopupSample.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);//from w ww  .j  a  va2s  .co m
    frame.setSize(350, 250);
    frame.setVisible(true);
}

From source file:MultiHighlight.java

public static void main(String args[]) {
    JFrame frame = new JFrame("MultiHighlight");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTextArea area = new JTextArea(5, 20);
    area.setText("ww\nw.java2s.c\nom");
    frame.getContentPane().add(new JScrollPane(area), BorderLayout.CENTER);

    JButton b = new JButton("Highlight All Vowels");
    b.addActionListener(new MultiHighlight(area, "aeiouAEIOU"));
    frame.getContentPane().add(b, BorderLayout.SOUTH);
    frame.pack();/*  w  w  w .  j  a v  a 2  s  .  co  m*/
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    JButton button = new JButton("Start Animation");
    button.addActionListener(new Main(frame));
    frame.getContentPane().add(button);/*from  ww  w  .j ava 2s.co  m*/
    frame.setSize(300, 300);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    final JLabel valueLabel = new JLabel(String.valueOf(value));
    JButton decButton = new JButton("-");
    decButton.addActionListener(e -> valueLabel.setText(String.valueOf(--value)));

    JButton incButton = new JButton("+");
    incButton.addActionListener(e -> valueLabel.setText(String.valueOf(++value)));

    JPanel panel = new JPanel();
    panel.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.weightx = 1;//  w w  w. j a v  a  2s . c om
    c.gridx = 0;
    c.gridy = 0;
    panel.add(decButton, c);
    c.gridx = 1;
    panel.add(valueLabel, c);
    c.gridx = 2;
    panel.add(incButton, c);

    c.gridy = 1;
    int w = 32;
    for (c.gridx = 0; c.gridx < 3; c.gridx++) {
        panel.add(Box.createHorizontalStrut(w), c);
    }

    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.add(panel);
    frame.pack();
    frame.setVisible(true);
}