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() {
    made_list.setModel(new DefaultListModel());
    for (String element : elements) {
        ((DefaultListModel) made_list.getModel()).addElement(element);
    }// ww w  .  j  a va 2 s .  co m

    JButton removeItemBtn = new JButton("Remove Item");
    removeItemBtn.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            removeActionPerformed(e);
        }
    });

    JPanel panel = new JPanel();
    panel.add(new JScrollPane(made_list));
    panel.add(removeItemBtn);

    JOptionPane.showMessageDialog(null, panel);
}

From source file:Main.java

public Main() {
    JPanel parentPanel = new JPanel();
    parentPanel.setLayout(new BorderLayout(10, 10));

    JPanel childPanel1 = new JPanel();
    childPanel1.setBackground(Color.red);
    childPanel1.setPreferredSize(new Dimension(300, 40));

    JPanel childPanel2 = new JPanel();
    childPanel2.setBackground(Color.blue);
    childPanel2.setPreferredSize(new Dimension(800, 600));

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

From source file:Main.java

public Main() {
    setSize(300, 300);/* ww  w  .  j a  v a 2  s.co  m*/
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    JPanel controlPane = new JPanel(new GridLayout(2, 1));
    controlPane.setOpaque(false);
    controlPane.add(new JLabel("Please wait..."));
    controlPane.add(waiter);
    glass.setOpaque(false);
    glass.add(padding);
    glass.add(new JLabel());
    glass.add(controlPane);
    glass.add(new JLabel());
    glass.add(new JLabel());
    glass.setSize(new Dimension(300, 300));

    setGlassPane(glass);

    JButton startB = new JButton("Start!");

    startB.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent A) {
            glass.setVisible(true);
            padding.requestFocus();
        }
    });
    Container contentPane = getContentPane();
    contentPane.add(startB, BorderLayout.SOUTH);
}

From source file:Main.java

private void addComponentsToPane() {

    JButton button = new JButton("Clear");
    button.addActionListener(this);

    typingArea = new JTextField(20);
    typingArea.addKeyListener(this);

    getContentPane().add(typingArea, BorderLayout.PAGE_START);
    getContentPane().add(button, BorderLayout.PAGE_END);
}

From source file:Test.java

public ApplicationWindow() {
    this.setBounds(100, 100, 200, 200);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setType(Type.NORMAL);

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

public Main() {
    comboBox.addItem("One");
    comboBox.addItem("Two");
    comboBox.addItem("Three");
    JButton button = new JButton("Show Selected");
    button.addActionListener(e -> System.out.println("Selected item: " + comboBox.getSelectedItem()));
    JButton button1 = new JButton("Append Items");
    button1.addActionListener(e -> appendCbItem());
    JButton button2 = new JButton("Reduce Items");
    button2.addActionListener(e -> reduceCbItem());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new GridLayout(4, 1));
    frame.add(comboBox);//from  w w  w  . j a  v  a 2 s . co  m
    frame.add(button);
    frame.add(button1);
    frame.add(button2);
    frame.pack();
    frame.setVisible(true);
    selectFirstItem();
}

From source file:MulticastEvent.java

public MulticastEvent() {
    JButton newButton = new JButton("New");
    add(newButton);//from ww w.ja  va  2s . c om
    newButton.addActionListener(this);

    closeAllButton = new JButton("Close all");
    add(closeAllButton);
}

From source file:Main.java

Main(String title) {
    super(title);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel p = new JPanel();

    JButton jb = new JButton("Open ...");
    jb.addActionListener(this);
    p.add(jb);/*from   w  ww .j  a va  2  s. com*/

    jb = new JButton("Save ...");
    jb.addActionListener(this);
    p.add(jb);

    getContentPane().add(p);

    setSize(200, 65);
    setVisible(true);
}

From source file:MainClass.java

MainClass(String title) {
    super(title);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel p = new JPanel();

    JButton jb = new JButton("Open ...");
    jb.addActionListener(this);
    p.add(jb);//w w  w.j a v  a  2 s.co m

    jb = new JButton("Save ...");
    jb.addActionListener(this);
    p.add(jb);

    getContentPane().add(p);

    setSize(200, 65);
    setVisible(true);
}

From source file:MainClass.java

public MainClass() {

    JButton jb = new JButton(new MyIcon());
    jb.setActionCommand("France");
    jb.addActionListener(this);
    add(jb);/*  w  w  w . j av a  2  s  .com*/
}