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[]) {
    SpinnerNumberModel model = new SpinnerNumberModel(0.0, -1000.0, 1000.0, 0.1);
    JSpinner s = new JSpinner(model);
    JSpinner.NumberEditor editor = new JSpinner.NumberEditor(s);
    s.setEditor(editor);/*from w  w w . ja  va 2s.c  o  m*/
    JTextField stepText = new JTextField(10);
    JButton bStepSet = new JButton("Set Step");
    bStepSet.addActionListener(e -> {
        Double val = Double.parseDouble(stepText.getText().trim());
        model.setStepSize(val);
    });
    JFrame f = new JFrame();
    Container c = f.getContentPane();
    c.add(s);
    JPanel southPanel = new JPanel();
    southPanel.add(stepText);
    southPanel.add(bStepSet);
    c.add(southPanel, BorderLayout.SOUTH);
    f.pack();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400, 400);/*from www .  j  a v a2  s.  c om*/

    JDialog dialog = new JDialog(frame, "Dialog", true);

    JPanel mainGui = new JPanel(new BorderLayout());
    mainGui.setBorder(new EmptyBorder(20, 20, 20, 20));
    mainGui.add(new JLabel("Contents go here"), BorderLayout.CENTER);

    JPanel buttonPanel = new JPanel(new FlowLayout());
    mainGui.add(buttonPanel, BorderLayout.SOUTH);

    JButton close = new JButton("Close");
    close.addActionListener(e -> dialog.setVisible(false));

    buttonPanel.add(close);

    frame.setVisible(true);

    dialog.setContentPane(mainGui);
    dialog.pack();
    dialog.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame jframe = new JFrame();
    jframe.setSize(500, 200);//from www. j  ava2s. co m
    jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTextPane jTextPane = new JTextPane();
    jTextPane.setEditorKit(new HTMLEditorKit());
    JButton btn = new JButton("Print");
    btn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            try {
                jTextPane.setContentType("text/html");
                boolean done = jTextPane.print();
                if (done) {
                    System.out.println("Printing is done");
                } else {
                    System.out.println("Error while printing");
                }
            } catch (Exception pex) {
                pex.printStackTrace();
            }
        }
    });
    jframe.add(btn, BorderLayout.SOUTH);
    jframe.add(jTextPane);
    jframe.setVisible(true);
}

From source file:ActionFocusMover.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Focus Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    ActionListener actionListener = new ActionFocusMover();
    frame.setLayout(new GridLayout(3, 3));
    for (int i = 1; i < 10; i++) {
        JButton button = new JButton(Integer.toString(i));
        button.addActionListener(actionListener);
        if ((i % 2) != 0) {
            button.setFocusable(false);/*from w  ww . j  av a 2 s .  co m*/
        }
        frame.add(button);
    }
    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:JScrollPaneToTopAction.java

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

    JLabel label = new JLabel("Label");
    label.setPreferredSize(new Dimension(1000, 1000));
    JScrollPane jScrollPane = new JScrollPane(label);

    JButton bn = new JButton("Move");

    bn.addActionListener(new JScrollPaneToTopAction(jScrollPane));

    frame.add(bn, BorderLayout.SOUTH);
    frame.add(jScrollPane, BorderLayout.CENTER);
    frame.setSize(400, 150);// w  w  w.  j  a  v  a 2  s.c  om
    frame.setVisible(true);
}

From source file:Main.java

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

    JTextArea textArea = new JTextArea();
    f.add(new JScrollPane(textArea), BorderLayout.CENTER);

    Timer timer = new Timer(1000, new ActionListener() {
        @Override//from  w  w  w. ja  v a 2  s.  com
        public void actionPerformed(ActionEvent e) {
            textArea.append("bla");
        }
    });
    timer.setRepeats(true);
    timer.start();
    JButton button = new JButton("Click me");
    button.addActionListener(e -> {
        System.out.println("Before option pane");
        JOptionPane.showMessageDialog(f, "A message dialog");
        System.out.println("After option pane");
    });
    f.add(button, BorderLayout.SOUTH);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.pack();
    f.setVisible(true);
}

From source file:ButtonListener.java

public static void main(String[] args) {
    JPanel panel = new JPanel();

    JButton close = new JButton("Close");
    close.addActionListener(new ButtonListener());

    JButton open = new JButton("Open");
    open.addActionListener(new ButtonListener());

    JButton find = new JButton("Find");
    find.addActionListener(new ButtonListener());

    JButton save = new JButton("Save");
    save.addActionListener(new ButtonListener());

    panel.add(close);//  www  . j  a va2  s .  c o  m
    panel.add(open);
    panel.add(find);
    panel.add(save);
    JFrame f = new JFrame();
    f.add(panel);
    f.setSize(400, 300);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    final JTextPane pane = new JTextPane();
    pane.setText("Some text");

    JButton buttonButton = new JButton("Insert label");
    buttonButton.addActionListener(e -> {
        JLabel label = new JLabel("label");
        label.setAlignmentY(0.85f);//w w  w.j  ava  2  s  .c  om
        pane.insertComponent(label);

    });

    JPanel panel = new JPanel(new BorderLayout());
    panel.add(buttonButton, BorderLayout.SOUTH);
    panel.add(pane, BorderLayout.CENTER);

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(panel);
    frame.setSize(400, 200);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    Object[][] rowData = { { "Hello", "World" } };
    Object[] columnNames = { "A", "B" };

    JTable table;//  ww  w  .  j a  va2  s  .c o  m
    DefaultTableModel model;

    model = new DefaultTableModel(rowData, columnNames);
    table = new JTable();
    table.setModel(model);
    JButton add = new JButton("Add");

    add.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            model.addRow(rowData[0]);
        }
    });
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container c = f.getContentPane();
    c.setLayout(new BorderLayout());
    c.add(add, BorderLayout.SOUTH);
    c.add(new JScrollPane(table), BorderLayout.CENTER);

    f.pack();

    f.setLocationRelativeTo(null);
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    final JTextPane tp = new JTextPane();
    JButton withFocus = new JButton("Select with focus");
    withFocus.addActionListener(e -> {
        tp.selectAll();/*www . j a  v  a2  s.com*/
        tp.requestFocus();
    });
    JButton withOutFocus = new JButton("Select without focus");
    withFocus.addActionListener(e -> tp.selectAll());

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new BorderLayout());
    frame.add(new JScrollPane(tp));
    JPanel panel = new JPanel();
    panel.add(withFocus);
    panel.add(withOutFocus);
    frame.add(panel, BorderLayout.SOUTH);
    frame.pack();
    frame.setVisible(true);
}