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 Main() {
    this.setSize(400, 100);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setLayout(new FlowLayout(FlowLayout.CENTER));
    JButton button = new JButton("Change Frame Color");
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Component component = (Component) e.getSource();
            JFrame frame = (JFrame) SwingUtilities.getRoot(component);
            frame.setBackground(Color.RED);
        }/*from  www.  j  a va2s.  c o  m*/
    });
    this.getContentPane().add(button);
}

From source file:Main.java

public Main() {
    this.setSize(400, 100);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setLayout(new FlowLayout(FlowLayout.CENTER));
    JButton button = new JButton("Change Frame Color");
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Component component = (Component) e.getSource();
            JFrame frame = (JFrame) SwingUtilities.getRoot(component);
            frame.getContentPane().setBackground(Color.RED);
        }// w  w  w.j  av a 2s . c o  m
    });
    this.getContentPane().add(button);
}

From source file:Main.java

public Main() {
    JComboBox<String> cb = new JComboBox<>(new String[] { "a", "m", "b", "c", "v" });
    cb.setSelectedIndex(-1);//from ww  w . j ava  2s .c  om

    JButton button = new JButton("Print Selection");
    button.addActionListener(e -> {
        if (cb.getSelectedIndex() != -1)
            System.out.println(cb.getSelectedItem());
        else
            System.out.println("Not selected");
    });

    add(cb);
    add(button);
}

From source file:Test.java

public Test() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    d.getContentPane().add(new JLabel("Click the OK button"), BorderLayout.CENTER);
    JButton closeIt = new JButton("OK");
    closeIt.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.out.println("Closing dialog");
            d.dispose();/*from  ww w  .  j av a 2s  . c  o  m*/
        }
    });
    d.getContentPane().add(closeIt, BorderLayout.SOUTH);
    d.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    d.pack();

    getContentPane().add(new JLabel("Placeholder label"));
    pack();
    setSize(200, 200);
    setVisible(true);
    d.setVisible(true);
}

From source file:MultipleListeners.java

public MultipleListeners() {
    JPanel panel = new JPanel();
    JButton add = new JButton("+");
    add.addActionListener(new ButtonListener1());
    add.addActionListener(new ButtonListener2());

    panel.add(add);//www . j  a  v  a2s.  c o  m
    panel.add(spinner);
    JFrame f = new JFrame();
    f.add(panel);
    f.add(statusbar, BorderLayout.SOUTH);

    f.setSize(300, 200);
    f.setLocationRelativeTo(null);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
}

From source file:Main.java

Main() {
    setSize(500, 300);/*from   w  w w . ja  va  2 s.  c om*/
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new BorderLayout());
    JScrollPane pane = new JScrollPane();
    txtMain = new JTextArea();
    pane.setViewportView(txtMain);
    this.add(pane, BorderLayout.CENTER);

    JButton btnAddText = new JButton("Add Text");
    btnAddText.addActionListener(e -> {
        txtMain.setText(txtMain.getText()
                + "\nLorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi sagittis id nibh vel rhoncus. ");
        String text = txtMain.getText();
        txtMain.setCaretPosition(text != null ? text.length() : 0);
    });
    add(btnAddText, BorderLayout.SOUTH);
    setVisible(true);
}

From source file:Main.java

private JPanel getPanel() {
    JPanel panel = new JPanel();
    JLabel label = new JLabel("Java Technology Dive Log");
    ImageIcon image = null;//from   w  ww  .ja  v a 2s  . c  o  m
    try {
        image = new ImageIcon(ImageIO.read(new URL("http://www.java2s.com/style/download.png")));
    } catch (Exception mue) {
        mue.printStackTrace();
    }
    label.setIcon(image);

    JButton button = new JButton("EXIT");
    button.addActionListener(e -> dialog.dispose());

    panel.add(label);
    panel.add(button);

    return panel;
}

From source file:Main.java

public Main(JFrame frame) {
    super(BoxLayout.Y_AXIS);
    this.frame = frame;
    add(text);/* w  ww  .ja v  a  2  s. c o  m*/
    JButton button = new JButton("Click Me");
    button.addActionListener(e -> launchDialog());
    add(button);
}

From source file:Main.java

Main() {
    getRootPane().setGlassPane(new JComponent() {
        public void paintComponent(Graphics g) {
            g.setColor(new Color(0, 0, 0, 100));
            g.fillRect(0, 0, getWidth(), getHeight());
            super.paintComponent(g);
        }/*from   w  w  w. j  ava 2  s  .  co m*/
    });
    JButton popDialog = new JButton("Block Frame");
    popDialog.addActionListener(e -> {
        getRootPane().getGlassPane().setVisible(true);
        JOptionPane.showMessageDialog(Main.this, "Shady!");
        getRootPane().getGlassPane().setVisible(false);
    });
    setContentPane(popDialog);
    pack();
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    setSize(350, 180);
}

From source file:ColorPicker.java

public ColorPicker() {
    super("JColorChooser Test Frame");
    setSize(200, 100);//from   w  ww .j av a2  s  . com
    final Container contentPane = getContentPane();
    final JButton go = new JButton("Show JColorChooser");
    go.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Color c;
            c = JColorChooser.showDialog(((Component) e.getSource()).getParent(), "Demo", Color.blue);
            contentPane.setBackground(c);
        }
    });
    contentPane.add(go, BorderLayout.SOUTH);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
}