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() {
    setSize(300, 100);/*from w ww . ja v  a2s .com*/

    UIManager.put("ProgressBar.selectionBackground", Color.black);
    UIManager.put("ProgressBar.selectionForeground", Color.white);
    UIManager.put("ProgressBar.foreground", new Color(8, 32, 128));

    progressBar = new JProgressBar();
    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);

    WindowListener wndCloser = new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    };
    addWindowListener(wndCloser);
    setVisible(true);
}

From source file:JProgressBarDemo.java

public JProgressBarDemo() {
    super("JProgressBar Demo");
    setSize(300, 100);/*  w w w  .  j  av a2 s .c  o  m*/

    UIManager.put("ProgressBar.selectionBackground", Color.black);
    UIManager.put("ProgressBar.selectionForeground", Color.white);
    UIManager.put("ProgressBar.foreground", new Color(8, 32, 128));

    progressBar = new JProgressBar();
    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);

    WindowListener wndCloser = new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    };
    addWindowListener(wndCloser);
    setVisible(true);
}

From source file:Branch.java

public void init() {
    Container cp = getContentPane();
    root = new DefaultMutableTreeNode("root");
    tree = new JTree(root);
    // Add it and make it take care of scrolling:
    cp.add(new JScrollPane(tree), BorderLayout.CENTER);
    // Capture the tree's model:
    model = (DefaultTreeModel) tree.getModel();
    JButton test = new JButton("Press me");
    test.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (i < data.length) {
                child = new Branch(data[i++]).node();
                // What's the last one you clicked?
                chosen = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
                if (chosen == null)
                    chosen = root;//from w ww.  ja  va 2 s  .  c  o  m
                // The model will create the appropriate event.
                // In response, the tree will update itself:
                model.insertNodeInto(child, chosen, 0);
                // Puts the new node on the chosen node.
            }
        }
    });
    // Change the button's colors:
    test.setBackground(Color.BLUE);
    test.setForeground(Color.WHITE);
    JPanel p = new JPanel();
    p.add(test);
    cp.add(p, BorderLayout.SOUTH);
}

From source file:SwingThreading.java

public SwingThreading() {
    super("Swing Threading");

    JButton freezer = new JButton("Increment");
    freezer.addActionListener(this);

    counter = new JLabel("0");

    add(freezer, BorderLayout.CENTER);
    add(counter, BorderLayout.SOUTH);

    pack();//  w w  w.  j a  v a2 s . co  m
    setLocationRelativeTo(null);

    setDefaultCloseOperation(EXIT_ON_CLOSE);
}

From source file:ErrorHandlerTest.java

/**
 * A fairly banal GUI, just to show interaction.
 *///w  ww .  jav  a  2 s.  c o m
ErrorHandlerTest() {
    super("GUI");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container cp = getContentPane();
    JButton bx = new JButton("Throw!");
    bx.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            throw new IllegalArgumentException("foo");
        }
    });
    cp.add(bx);
    setBounds(200, 200, 200, 100);
}

From source file:Main.java

public ConfirmDialog(Frame parent) {
    super(parent, true);

    JPanel gui = new JPanel(new BorderLayout(3, 3));
    gui.setBorder(new EmptyBorder(5, 5, 5, 5));
    content = new JPanel(new BorderLayout());
    gui.add(content, BorderLayout.CENTER);
    JPanel buttons = new JPanel(new FlowLayout(4));
    gui.add(buttons, BorderLayout.SOUTH);

    JButton ok = new JButton("OK");
    buttons.add(ok);//  w w  w  .j  a va 2 s .co m
    ok.addActionListener(e -> {
        result = OK_OPTION;
        setVisible(false);
    });

    JButton cancel = new JButton("Cancel");
    buttons.add(cancel);
    cancel.addActionListener(e -> {
        result = CANCEL_OPTION;
        setVisible(false);
    });
    setContentPane(gui);
}

From source file:Main.java

public CardLayoutPanel() {
    setLayout(card);//from   w ww .  j a  v a  2  s .c om
    JButton button;
    for (int i = 1; i <= 6; i++) {
        add(button = new JButton("Press " + i), "Card" + i);
        button.addActionListener(this);
    }
}

From source file:Main.java

public CardLayoutPanel() {
    setLayout(card);//from  w  w  w.  j av a2  s.  c  o m
    JButton button;
    for (int i = 1; i <= 6; i++) {
        add(button = new JButton("Press " + i), "Card" + i);
        button.addActionListener(this);
    }
    System.out.println(card.getLayoutAlignmentX(this));
}

From source file:Main.java

public CardLayoutPanel() {
    setLayout(card);// w ww  . j a  va2 s . c  o m
    JButton button;
    for (int i = 1; i <= 6; i++) {
        add(button = new JButton("Press " + i), "Card" + i);
        button.addActionListener(this);
    }
    System.out.println(card.preferredLayoutSize(this));
}

From source file:Main.java

public CardLayoutPanel() {
    setLayout(card);//from  w w  w.j  a va  2 s .  co  m
    JButton button;
    for (int i = 1; i <= 6; i++) {
        add(button = new JButton("Press " + i), "Card" + i);
        button.addActionListener(this);
    }
    card.last(this);
}