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.ColorChooserDemo2.java

public ColorChooserDemo2() {
    super(new BorderLayout());

    //Set up banner to use as custom preview panel
    banner = new JLabel("Welcome to the Tutorial Zone!", JLabel.CENTER);
    banner.setForeground(Color.yellow);
    banner.setBackground(Color.blue);
    banner.setOpaque(true);/*  w w w  . ja va2 s.  c o m*/
    banner.setFont(new Font("SansSerif", Font.BOLD, 24));
    banner.setPreferredSize(new Dimension(100, 65));

    JPanel bannerPanel = new JPanel(new BorderLayout());
    bannerPanel.add(banner, BorderLayout.CENTER);
    bannerPanel.setBorder(BorderFactory.createTitledBorder("Banner"));

    //Set up color chooser for setting background color
    JPanel panel = new JPanel(); //use FlowLayout
    JButton bcc = new JButton("Show Color Chooser...");
    bcc.addActionListener(this);
    panel.add(bcc);
    panel.setBorder(BorderFactory.createTitledBorder("Choose Background Color"));

    //Set up color chooser for setting text color
    tcc = new JColorChooser();
    tcc.getSelectionModel().addChangeListener(this);
    tcc.setBorder(BorderFactory.createTitledBorder("Choose Text Color"));

    //Remove the preview panel
    tcc.setPreviewPanel(new JPanel());

    //Override the chooser panels with our own
    AbstractColorChooserPanel panels[] = { new CrayonPanel() };
    tcc.setChooserPanels(panels);
    tcc.setColor(banner.getForeground());

    add(bannerPanel, BorderLayout.PAGE_START);
    add(panel, BorderLayout.CENTER);
    add(tcc, BorderLayout.PAGE_END);
}

From source file:userinterface.graph.GraphOptions.java

/** Creates a new instance of MultiGraphOptions */
public GraphOptions(GUIPlugin plugin, JPanel theModel, JFrame gui, String title) {
    super(gui, title);

    gop = new GraphOptionsPanel(plugin, gui, theModel);

    gop.setPreferredSize(new Dimension(400, 650));

    this.getContentPane().add(gop);

    JPanel p = new JPanel(new FlowLayout(FlowLayout.RIGHT));
    this.getContentPane().add(p, BorderLayout.SOUTH);

    this.getContentPane().setSize(400, 650);

    JButton jb = new JButton("Close");

    jb.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            gop.stopEditors();// w w w  .j av a2  s . c  o  m
            setVisible(false);
        }
    });

    jb.addFocusListener(new FocusListener() {
        /**
         * Invoked when a component gains the keyboard focus.
         */
        public void focusGained(FocusEvent e) {
        }

        /**
         * Invoked when a component loses the keyboard focus.
         */
        public void focusLost(FocusEvent e) {
            //gop.stopEditors();
        }

    });

    p.add(jb);

    pack();
    setLocationRelativeTo(getParent()); // centre
    //show();
    setDefaultCloseOperation(javax.swing.JFrame.DISPOSE_ON_CLOSE);
}

From source file:Sender.java

public void init() {
    //Execute a job on the event-dispatching thread; creating this applet's GUI.
    try {//from  www.ja  v a  2  s.co  m
        final ActionListener al = this;
        SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
                JButton btn = new JButton("Click To Increment Counter");
                add(btn);
                btn.addActionListener(al);
            }
        });
    } catch (Exception e) {
        System.err.println("createGUI didn't complete successfully");
    }
}

From source file:Flipper.java

private JButton makeButton(String caption) {
    JButton b = new JButton(caption);
    b.setActionCommand(caption);//from   ww  w.  j  a v  a 2  s .c  o m
    b.addActionListener(this);
    getContentPane().add(b, constraints);
    return b;
}

From source file:RevalidateExample.java

public RevalidateExample() {
    super("Revalidation Demo");
    setSize(300, 150);/*from   w  ww . ja  v a 2  s .c  om*/
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    Font font = new Font("Dialog", Font.PLAIN, 10);
    final JButton b = new JButton("Add");
    b.setFont(font);

    Container c = getContentPane();
    c.setLayout(new FlowLayout());
    c.add(b);

    b.addActionListener(new ActionListener() {
        // Increase the size of the button's font each time it's clicked

        int size = 20;

        public void actionPerformed(ActionEvent ev) {
            b.setFont(new Font("Dialog", Font.PLAIN, ++size));
            b.revalidate(); // invalidates the button & validates its root pane
        }
    });
}

From source file:geneon.intellij.plugin.jenkins.ui.EditServerDialog.java

@Nullable
@Override// w w w  .  jav a  2  s . co  m
protected JComponent createCenterPanel() {
    nameTextField.setMinimumSize(new Dimension(300, 10));
    urlTextField.setMinimumSize(new Dimension(300, 10));

    JButton testButton = new JButton("Test");
    testButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            testSettings();
        }
    });

    JPanel panel = new JPanel(new GridBagLayout());
    JLabel nameLabel = new JLabel("Server name:");
    panel.add(nameLabel, new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.WEST,
            GridBagConstraints.NONE, new Insets(0, 0, 5, 10), 0, 0));
    panel.add(nameTextField, new GridBagConstraints(1, 0, 1, 1, 1, 0, GridBagConstraints.CENTER,
            GridBagConstraints.HORIZONTAL, new Insets(0, 0, 5, 0), 0, 0));
    JLabel urlLabel = new JLabel("URL:");
    panel.add(urlLabel, new GridBagConstraints(0, 1, 1, 1, 0, 0, GridBagConstraints.WEST,
            GridBagConstraints.NONE, new Insets(0, 0, 5, 10), 0, 0));
    panel.add(urlTextField, new GridBagConstraints(1, 1, 1, 1, 1, 0, GridBagConstraints.CENTER,
            GridBagConstraints.HORIZONTAL, new Insets(0, 0, 5, 0), 0, 0));
    panel.add(testButton, new GridBagConstraints(0, 2, 2, 1, 1, 0, GridBagConstraints.WEST,
            GridBagConstraints.NONE, new Insets(0, 0, 5, 0), 0, 0));

    return panel;
}

From source file:SwingSuspendResume.java

public SwingSuspendResume() {
    symbolTF = new JTextField();
    symbolTF.setEditable(false);//from   w  ww.ja v  a  2 s. c  o  m
    symbolTF.setFont(new Font("Monospaced", Font.BOLD, 26));
    symbolTF.setHorizontalAlignment(JTextField.CENTER);

    final JButton suspendB = new JButton("Suspend");
    final JButton resumeB = new JButton("Resume");

    suspendB.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            suspendNow();
        }
    });

    resumeB.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            resumeNow();
        }
    });

    JPanel innerStackP = new JPanel();
    innerStackP.setLayout(new GridLayout(0, 1, 3, 3));
    innerStackP.add(symbolTF);
    innerStackP.add(suspendB);
    innerStackP.add(resumeB);

    this.setLayout(new FlowLayout(FlowLayout.CENTER));
    this.add(innerStackP);
}

From source file:Main.java

private void addComponentsToPane() {

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

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

    // Uncomment this if you wish to turn off focus
    // traversal. The focus subsystem consumes
    // focus traversal keys, such as Tab and Shift Tab.
    // If you uncomment the following line of code, this
    // disables focus traversal and the Tab events will
    // become available to the key event listener.
    // typingArea.setFocusTraversalKeysEnabled(false);

    displayArea = new JTextArea();
    displayArea.setEditable(false);/*from  ww  w . j  ava  2s  .c  o m*/
    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:TableIt.java

public TableIt() {
    JFrame f = new JFrame();
    TableModel tm = new MyTableModel();
    final JTable table = new JTable(tm);

    TableColumnModel tcm = table.getColumnModel();
    TableColumn column = tcm.getColumn(tcm.getColumnCount() - 1);
    TableCellRenderer renderer = new MyTableCellRenderer();
    column.setCellRenderer(renderer);//from ww  w. ja va  2  s  . c o m

    JButton selectionType = new JButton("Next Type");
    selectionType.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            boolean rowSet = table.getRowSelectionAllowed();
            boolean colSet = table.getColumnSelectionAllowed();
            boolean cellSet = table.getCellSelectionEnabled();

            boolean setRow = !rowSet;
            boolean setCol = rowSet ^ colSet;
            boolean setCell = rowSet & colSet;

            table.setCellSelectionEnabled(setCell);
            table.setColumnSelectionAllowed(setCol);
            table.setRowSelectionAllowed(setRow);
            System.out.println("Row Selection Allowed? " + setRow);
            System.out.println("Column Selection Allowed? " + setCol);
            System.out.println("Cell Selection Enabled? " + setCell);
            table.repaint();
        }
    });
    JButton selectionMode = new JButton("Next Mode");
    selectionMode.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            ListSelectionModel lsm = table.getSelectionModel();
            int mode = lsm.getSelectionMode();
            int nextMode;
            String nextModeString;
            if (mode == ListSelectionModel.SINGLE_SELECTION) {
                nextMode = ListSelectionModel.SINGLE_INTERVAL_SELECTION;
                nextModeString = "Single Interval Selection";
            } else if (mode == ListSelectionModel.SINGLE_INTERVAL_SELECTION) {
                nextMode = ListSelectionModel.MULTIPLE_INTERVAL_SELECTION;
                nextModeString = "Multiple Interval Selection";
            } else {
                nextMode = ListSelectionModel.SINGLE_SELECTION;
                nextModeString = "Single Selection";
            }
            lsm.setSelectionMode(nextMode);
            System.out.println("Selection Mode: " + nextModeString);
            table.repaint();
        }
    });
    JPanel jp = new JPanel();
    jp.add(selectionType);
    jp.add(selectionMode);
    JScrollPane jsp = new JScrollPane(table);
    Container c = f.getContentPane();
    c.add(jsp, BorderLayout.CENTER);
    c.add(jp, BorderLayout.SOUTH);
    f.setSize(300, 250);
    f.show();
}

From source file:de.wusel.partyplayer.gui.dialog.ChangePasswordDialog.java

private void initUI() {
    setLayout(new MigLayout("fill", "[] [grow] []"));

    add(new JLabel("Altes Passwort:"));
    add(oldPasswordField, "grow, span, wrap");
    add(new JLabel("Neues Passwort:"));
    add(newPasswordField, "grow, span, wrap");
    add(new JSeparator(), "newline push, span, grow, aligny top, wrap");
    JPanel buttonPanel = new JPanel(new MigLayout("insets 0 0 0 0, fill"));
    final JButton cancelButton = new JButton("cancel");
    cancelButton.addActionListener(new ActionListener() {

        @Override//  w  w w. j a v  a2 s  . co m
        public void actionPerformed(ActionEvent e) {
            cancel();
        }
    });
    buttonPanel.add(cancelButton, "split 2, tag cancel");
    final JButton okButton = new JButton("ok");
    okButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            ok();
        }
    });

    buttonPanel.add(okButton, "tag ok");
    add(buttonPanel, "span 3, growx");
}