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 CardLayoutPanel() {
    setLayout(card);//  w ww  . ja  v  a2  s  .c o m
    JButton button = new JButton("Press 1");
    button.addActionListener(this);
    card.addLayoutComponent(button, "1");
    add(button);

    button = new JButton("Press 2");
    button.addActionListener(this);
    card.addLayoutComponent(button, "2");
    add(button);

    button = new JButton("Press 3");
    button.addActionListener(this);
    card.addLayoutComponent(button, "3");
    add(button);

    card.show(this, "2");
}

From source file:Main.java

public Main() {
    childPanel1.setBackground(Color.red);
    childPanel1.setPreferredSize(new Dimension(300, 40));
    childPanel2.setBackground(Color.blue);
    childPanel2.setPreferredSize(new Dimension(300, 40));
    childPanel3.setBackground(Color.yellow);
    childPanel3.setPreferredSize(new Dimension(300, 40));

    JButton myButton = new JButton("Add Component ");
    myButton.addActionListener(e -> {
        add(childPanel2, BorderLayout.CENTER);
        pack();//ww w .  j  av a 2  s.  co  m
    });
    setLocation(10, 200);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    add(childPanel3, BorderLayout.CENTER);
    add(myButton, BorderLayout.SOUTH);
    pack();
    setVisible(true);
}

From source file:MyDialog.java

public MyDialog(JFrame parent) {
    super(parent, "My dialog", true);
    Container cp = getContentPane();
    cp.setLayout(new FlowLayout());
    cp.add(new JLabel("Here is my dialog"));
    JButton ok = new JButton("OK");
    ok.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            dispose(); // Closes the dialog
        }//from  w ww. j a  va 2s  .c o  m
    });
    cp.add(ok);
    setSize(150, 125);
}

From source file:Main.java

public Main(final Frame owner) {
    super(owner);
    JPanel pnl = new JPanel(new BorderLayout());
    pnl.add(new JLabel("Click outside this dialog in the parent frame to close it"), BorderLayout.NORTH);
    JButton btn = new JButton("Click Me");
    btn.addActionListener(e -> JOptionPane.showMessageDialog(Main.this, "New Child Window"));
    pnl.add(btn, BorderLayout.CENTER);
    this.setContentPane(pnl);
    this.pack();//www  . jav a 2 s  .  c om
    this.setLocationRelativeTo(owner);
    this.setAlwaysOnTop(true);
    this.addWindowFocusListener(new WindowFocusListener() {
        public void windowGainedFocus(WindowEvent e) {
        }

        public void windowLostFocus(WindowEvent e) {
            if (SwingUtilities.isDescendingFrom(e.getOppositeWindow(), Main.this)) {
                return;
            }
            Main.this.setVisible(false);
        }
    });
}

From source file:Test.java

public Test() {
    this.setBounds(100, 100, 200, 200);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setType(Type.UTILITY);
    this.setLayout(new FlowLayout());
    JButton exitButton = new JButton("Exit");
    this.add(exitButton);
    exitButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            System.exit(0);//from ww  w.  j  a v a 2s .  c  o m
        }
    });
}

From source file:Test.java

public Test() {
    this.setSize(200, 100);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setLayout(new FlowLayout());

    JColorChooser.showDialog(this, null, Color.blue);
    JButton printDialogButton = new JButton("Print Dialog");
    printDialogButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            final PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
            attributes.add(DialogTypeSelection.COMMON);
            PrinterJob printJob = PrinterJob.getPrinterJob();
            printJob.printDialog(attributes);

        }//  w  w w . j a  v  a 2 s  . c  o  m
    });
    this.add(printDialogButton);
}

From source file:Main.java

private JButton createGridButton(final int row, final int col) {
    final JButton b = new JButton("r" + row + ",c" + col);
    b.addActionListener(e -> {
        JButton gb = Main.this.getGridButton(row, col);
        System.out.println("r" + row + ",c" + col + " " + (b == gb) + " " + (b.equals(gb)));
    });/*from  ww  w . j ava 2s .  c o m*/
    return b;
}

From source file:MainClass.java

public MainClass() {
    Container cp = getContentPane();
    JButton crasher = new JButton("Crash");
    cp.add(crasher);//from   w  w  w .j  a  v a  2  s. c  o  m
    crasher.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            throw new RuntimeException("You asked for it");
        }
    });
    Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
        public void uncaughtException(Thread t, Throwable ex) {
            System.out.println("You crashed thread " + t.getName());
            System.out.println("Exception was: " + ex.toString());
        }
    });
    pack();
}

From source file:Main.java

public Main() {
    JPanel jp = new JPanel();
    jp.setLayout(new BorderLayout());
    final JTabbedPane tb = new JTabbedPane();
    JButton btn = new JButton("push me !!!");
    btn.addActionListener(e -> {
        tb.setEnabledAt(1, true);/*from   w  w  w.  j av a2 s  . c o m*/
        tb.setEnabledAt(2, true);
        tb.setEnabledAt(3, true);
        tb.setEnabledAt(4, true);
    });
    JPanel pnl = new JPanel();
    pnl.add(btn);
    tb.add("Tab1", pnl);
    tb.add("Tab2", new JTextArea(10, 20));
    tb.add("Tab3", new JTextArea(10, 20));
    tb.add("Tab4", new JTextArea(10, 20));
    tb.add("Tab5", new JTextArea(10, 20));
    jp.add(tb, BorderLayout.CENTER);
    tb.setEnabledAt(1, false);
    tb.setEnabledAt(2, false);
    tb.setEnabledAt(3, false);
    tb.setEnabledAt(4, false);
    JFrame frame = new JFrame();
    frame.setLayout(new BorderLayout());
    frame.add(jp, BorderLayout.CENTER);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public void launchDialog() {
    JButton findButton = new JButton("Find");
    findButton.addActionListener(e -> {
        int start = text.getText().indexOf("is");
        int end = start + "is".length();
        if (start != -1) {
            text.requestFocus();// w  w  w.j  a  va 2  s . c o  m
            text.select(start, end);
        }
    });
    JButton cancelButton = new JButton("Cancel");

    JOptionPane optionPane = new JOptionPane("Do you understand?", JOptionPane.QUESTION_MESSAGE,
            JOptionPane.YES_NO_OPTION, null, new Object[] { findButton, cancelButton });

    JDialog dialog = new JDialog(frame, "Click a button", false);
    cancelButton.addActionListener(e -> dialog.setVisible(false));
    dialog.setContentPane(optionPane);
    dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
    dialog.setLocation(100, 100);
    dialog.pack();
    dialog.setVisible(true);
}