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() {

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

From source file:Main.java

public Main() {
    super();//from  ww w.  j a v a2s.  co m
    setSize(300, 100);

    progressBar.setMinimum(minValue);
    progressBar.setMaximum(maxValue);
    progressBar.setStringPainted(true);
    JButton start = new JButton("Start");

    start.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Thread runner = new Thread() {
                public void run() {
                    counter = minValue;
                    while (counter <= maxValue) {
                        Runnable runme = new Runnable() {
                            public void run() {
                                progressBar.setValue(counter);
                            }
                        };

                        SwingUtilities.invokeLater(runme);
                        counter++;
                        try {
                            Thread.sleep(100);
                        } catch (Exception ex) {
                        }
                    }
                }
            };
            runner.start();
        }
    });

    getContentPane().add(progressBar, BorderLayout.CENTER);
    getContentPane().add(start, BorderLayout.WEST);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
}

From source file:AboutDialog.java

public AboutDialog() {
    setTitle("About");
    setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));

    add(Box.createRigidArea(new Dimension(0, 10)));

    JLabel name = new JLabel("Notes");
    name.setAlignmentX(0.5f);/*from  w w w .j  a va2  s  . c  o m*/
    add(name);

    add(Box.createRigidArea(new Dimension(0, 100)));

    JButton close = new JButton("Close");
    close.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            dispose();
        }
    });

    close.setAlignmentX(0.5f);
    add(close);
    setModalityType(ModalityType.APPLICATION_MODAL);
    setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    setSize(300, 200);
}

From source file:Main.java

public Main() {
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    Container pane = getContentPane();
    GroupLayout gl = new GroupLayout(pane);
    pane.setLayout(gl);//w w  w  .j  a  v  a  2  s .  c o m

    gl.setAutoCreateGaps(true);
    gl.setAutoCreateContainerGaps(true);

    JButton btn = new JButton("Switch");
    btn.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            for (int i = 1; i < 9; i++) {
                labels[i].setVisible(!labels[i].isVisible());
            }
        }
    });

    gl.setHonorsVisibility(false);

    SequentialGroup seqGroup = gl.createSequentialGroup();
    gl.setHorizontalGroup(seqGroup);
    seqGroup.addComponent(btn);
    seqGroup.addPreferredGap(ComponentPlacement.UNRELATED, 10, 10);
    for (int i = 0; i < 10; i++) {
        seqGroup.addComponent(labels[i]);
        seqGroup.addPreferredGap(ComponentPlacement.UNRELATED, 10, 10);
    }
    ParallelGroup parGroup = gl.createParallelGroup();
    gl.setVerticalGroup(parGroup);
    parGroup.addComponent(btn);

    for (int i = 0; i < 10; i++) {
        parGroup.addComponent(labels[i]);
    }
    pack();
}

From source file:InnerClass.java

public InnerClass() {
    JButton close = new JButton("Close");
    ButtonListener listener = new ButtonListener();
    close.addActionListener(listener);

    JFrame f = new JFrame();
    f.add(close);//from   w w w  . j  a v a  2 s .  c  om

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

From source file:Main.java

public Main() throws HeadlessException {
    setSize(200, 200);/*w  w  w  .ja v  a  2s .c o  m*/
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JButton button1 = new JButton("Message dialog");
    button1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageDialog((Component) e.getSource(), "Thank you!");
        }
    });

    JButton button2 = new JButton("Input dialog");
    button2.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String text = JOptionPane.showInputDialog((Component) e.getSource(), "What is your name?");
            if (text != null && !text.equals("")) {
                JOptionPane.showMessageDialog((Component) e.getSource(), "Hello " + text);
            }
        }
    });

    JButton button3 = new JButton("Yes no dialog");
    button3.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            int result = JOptionPane.showConfirmDialog((Component) e.getSource(), "Close this application?");
            if (result == JOptionPane.YES_OPTION) {
                System.exit(0);
            } else if (result == JOptionPane.NO_OPTION) {
                System.out.println("Do nothing");
            }
        }
    });

    setLayout(new FlowLayout(FlowLayout.CENTER));
    getContentPane().add(button1);
    getContentPane().add(button2);
    getContentPane().add(button3);
}

From source file:Main.java

public Main() {
    super(BoxLayout.Y_AXIS);
    DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
    for (int i = 0; i < 14; i++) {
        DefaultMutableTreeNode node = new DefaultMutableTreeNode("Root" + i);
        node.add(new DefaultMutableTreeNode("Child" + i));
        root.add(node);/*  w w w . j  a v a 2s  .c o  m*/
    }
    CustomTree tree = new CustomTree(root);
    tree.setRootVisible(false);
    JScrollPane pane = new JScrollPane(tree);
    add(pane);
    JButton button = new JButton("Expand");
    button.addActionListener(e -> tree.expandSelectedPaths());
    add(button);
}

From source file:Main.java

public Main() {
    setSize(500, 500);//  w ww .  j av  a  2s. co  m
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    setLayout(new GridLayout(0, 1));
    JPanel buttonPanel = new JPanel();

    JButton addButton = new JButton("add");
    addButton.addActionListener(e -> addElementToArrayList());
    JButton removeButton = new JButton("remove");
    removeButton.addActionListener(e -> removeElementFromArrayList());

    tabbedPane = new JTabbedPane();
    tabbedPaneMouseListener = (new MouseAdapter() {

        public void mouseClicked(MouseEvent e) {
            if (SwingUtilities.isLeftMouseButton(e)) {
                if (e.getClickCount() == 1) {
                    System.out.println("Do Something");
                }
            }
        }
    });
    tabbedPane.addMouseListener(tabbedPaneMouseListener);

    buttonPanel.add(addButton);
    buttonPanel.add(removeButton);
    add(buttonPanel);
    this.add(tabbedPane);
}

From source file:Main.java

public Main() throws HeadlessException {
    setSize(300, 300);//w  w w .  j  av a2  s  .com
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER));

    JButton disable = new JButton("DISABLE");
    disable.setToolTipText("disabled.");
    disable.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            ToolTipManager.sharedInstance().setEnabled(false);
        }
    });
    JButton enable = new JButton("ENABLE");
    enable.setToolTipText("enabled.");
    enable.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            ToolTipManager.sharedInstance().setEnabled(true);
        }
    });
    getContentPane().add(enable);
    getContentPane().add(disable);
}

From source file:Main.java

public Main() {
    this.setUndecorated(true);

    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setSize(200, 200);

    Ellipse2D.Double ellipse = new Ellipse2D.Double(0, 0, 200, 100);
    Rectangle2D.Double rect = new Rectangle2D.Double(0, 100, 200, 200);

    Path2D path = new Path2D.Double();
    path.append(rect, true);/* w  ww . jav a2  s .co m*/
    path.append(ellipse, true);
    this.setShape(path);
    JButton closeButton = new JButton("Close");
    this.add(closeButton, BorderLayout.SOUTH);
    closeButton.addActionListener(e -> System.exit(0));
}