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:components.SplitPaneDividerDemo.java

private JComponent createControlPanel() {
    JPanel panel = new JPanel();
    JButton reset = new JButton("Reset");
    reset.addActionListener(this);
    panel.add(reset);// w w w  . ja  va 2  s  . c  om
    return panel;
}

From source file:Main.java

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

}

From source file:Main.java

public CardLayoutPanel() {
    setLayout(card);//from   w w w .  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);
    }
    card.setVgap(50);
    card.setHgap(30);
}

From source file:Main.java

public CardLayoutPanel() {
    setLayout(card);/*  ww w  .  j a v a 2 s .  co  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.getHgap());
    System.out.println(card.getVgap());
}

From source file:Main.java

private void addComponentsToPane() {

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

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

    displayArea = new JTextArea();
    displayArea.setEditable(false);/*from w  ww .j  av a  2 s.c  om*/
    JScrollPane scrollPane = new JScrollPane(displayArea);
    scrollPane.setPreferredSize(new Dimension(375, 125));

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

From source file:Main.java

public Main() {
    super(BoxLayout.Y_AXIS);
    glassPane.setOpaque(false);/*w w w  .  j a  va 2s.c om*/
    DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
    for (int i = 0; i < 14000; i++) {
        DefaultMutableTreeNode node = new DefaultMutableTreeNode("Root" + i);
        node.add(new DefaultMutableTreeNode("Child" + i));
        root.add(node);
    }

    final JTree tree = new JTree(root);
    tree.setRootVisible(false);
    final JScrollPane pane = new JScrollPane(tree);
    add(pane);

    JButton button = new JButton("Expand");
    button.addActionListener(e -> {
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                TreePath[] paths = tree.getSelectionPaths();
                if (paths == null || paths.length == 0) {
                    glassPane.setVisible(false);
                    return;
                }
                tree.setSelectionPath(paths[0]);

                for (int i = 0; i < paths.length; i++) {
                    tree.expandPath(paths[i]);
                }
                glassPane.setVisible(false);
            }
        });
        getRootPane().setGlassPane(glassPane);
        glassPane.setVisible(true);
        t.start();
    });
    add(button);
    glassPane.addMouseWheelListener(e -> {
        for (MouseWheelListener mwl : pane.getMouseWheelListeners()) {
            mwl.mouseWheelMoved(e);
        }
    });
}

From source file:SwingThreadingWait.java

public SwingThreadingWait() {
    super("Invoke & Wait");

    JButton freezer = new JButton("Open File");
    freezer.addActionListener(this);

    counter = new JLabel("Time elapsed: 0s");

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

    pack();/* w  w w  .ja v  a2  s.  c  o m*/
    setLocationRelativeTo(null);

    setDefaultCloseOperation(EXIT_ON_CLOSE);
}

From source file:MainClass.java

private void makeGUI() {
    setLayout(new FlowLayout());

    ImageIcon france = new ImageIcon("france.gif");
    JButton jb = new JButton(france);
    jb.setActionCommand("France");
    jb.addActionListener(this);
    add(jb);// ww w.  jav  a2s  .  co  m

    ImageIcon germany = new ImageIcon("germany.gif");
    jb = new JButton(germany);
    jb.setActionCommand("Germany");
    jb.addActionListener(this);
    add(jb);

    ImageIcon italy = new ImageIcon("italy.gif");
    jb = new JButton(italy);
    jb.setActionCommand("Italy");
    jb.addActionListener(this);
    add(jb);

    ImageIcon japan = new ImageIcon("japan.gif");
    jb = new JButton(japan);
    jb.setActionCommand("Japan");
    jb.addActionListener(this);
    add(jb);

    jtf = new JTextField(15);
    add(jtf);
}

From source file:MainClass.java

public CardLayoutPanel() {

    setLayout(card);//from   w  w w. 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);
    }
}

From source file:ListIt.java

public ListIt() {
    JFrame f = new JFrame();
    final PartsListModel pcm = new PartsListModel();
    ListCellRenderer lcr = new MyLabelRenderer();
    JList jl = new JList(pcm);
    jl.setCellRenderer(lcr);/*w  ww . java 2  s. co  m*/
    ListSelectionModel lsm = jl.getSelectionModel();
    lsm.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    jl.addListSelectionListener(new ListSelectionListener() {
        public void valueChanged(ListSelectionEvent e) {
            if (!e.getValueIsAdjusting()) {
                String element[] = (String[]) pcm.getElementAt(e.getFirstIndex());
                System.out.println(element[0] + " : " + element[1] + " : " + element[2]);
            }
        }
    });
    JScrollPane jsp = new JScrollPane(jl);
    JComboBox jc = new JComboBox(pcm);
    jc.setRenderer(lcr);
    JButton jb = new JButton("Add Merchandise");
    jb.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            pcm.addElement(partsList[(int) (Math.random() * partsList.length)]);
        }
    });
    Container c = f.getContentPane();
    c.add(jsp, BorderLayout.NORTH);
    c.add(jc, BorderLayout.CENTER);
    c.add(jb, BorderLayout.SOUTH);
    f.setSize(250, 250);
    f.show();
}