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 static JButton createButton(String text, ActionListener... listeners) {
    JButton result = new JButton(text);

    for (ActionListener listener : listeners) {
        result.addActionListener(listener);
    }/*from w  w w .j  a  v a  2  s .  c  o  m*/

    return result;
}

From source file:Main.java

/**
 * @param okText/*from  w ww  .j  a v  a2  s  . co m*/
 * @param listener
 * @param root 
 * @return newly created JPanel with ok and cancel buttons
 */
public static JPanel createOkPanel(String okText, ActionListener listener, JRootPane root) {
    JPanel panel = new JPanel();
    JButton ok = new JButton(okText);
    panel.add(ok);
    ok.addActionListener(listener);
    ok.setActionCommand(okText);
    if (root != null)
        root.setDefaultButton(ok);
    return panel;
}

From source file:Main.java

public static JButton getButton(String displayText, ActionListener actionListener, String name) {
    JButton button = new JButton(displayText);
    button.setActionCommand(displayText);
    button.addActionListener(actionListener);

    if (name != null && !"".equals(name)) {
        button.setName(name);// ww  w. j  a  v  a2s  .c  om
    }

    return button;
}

From source file:Main.java

static Component newButton(String text, String actionCommand, ActionListener... listeners) {
    JButton button = new JButton(text);
    button.setActionCommand(actionCommand);
    for (ActionListener l : listeners)
        button.addActionListener(l);

    return button;
}

From source file:Main.java

/**
 * Create simple {@link JButton}.// w  w w  .  j  a  v a  2  s .c  o  m
 * 
 * @param caption
 *            caption on the button
 * @param action
 *            action
 * @return {@link JButton}
 */
public static JButton createSimpleTextButton(String caption, Action action) {
    JButton button = decoratedToSimpleButton(new JButton(caption));
    button.addActionListener(action);
    return button;
}

From source file:MainClass.java

public static JPanel demo1() {
    JPanel pan = new JPanel(new BorderLayout());
    pan.setBorder(new TitledBorder("change format midstream"));

    MaskFormatter lowercase = null;
    try {/*from w  w w .  java2s .c  om*/
        lowercase = new MaskFormatter("LLLL");
    } catch (ParseException pe) {
    }
    final JFormattedTextField field = new JFormattedTextField(lowercase);
    field.setValue("lower case");
    pan.add(field, BorderLayout.CENTER);

    final JButton change = new JButton("change format");
    JPanel changePanel = new JPanel();
    changePanel.add(change);
    pan.add(changePanel, BorderLayout.SOUTH);

    change.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            try {
                field.commitEdit();
                MaskFormatter uppercase = new MaskFormatter("UUUU");
                DefaultFormatterFactory factory = new DefaultFormatterFactory(uppercase);
                field.setFormatterFactory(factory);
                change.setEnabled(false);
            } catch (ParseException pe) {
            }
        }
    });

    return pan;
}

From source file:Main.java

/**
 * @param okText/*from  w w  w .j a v  a 2  s.c o  m*/
 * @param cancelText
 * @param listener
 * @param root 
 * @return newly created JPanel with ok and cancel buttons
 */
public static JPanel createOkCancelPanel(String okText, String cancelText, ActionListener listener,
        JRootPane root) {
    JPanel panel = new JPanel();
    JButton ok = new JButton(okText);
    JButton cancel = new JButton(cancelText);
    panel.add(ok);
    panel.add(cancel);
    ok.addActionListener(listener);
    ok.setActionCommand(okText);
    if (root != null)
        root.setDefaultButton(ok);
    cancel.addActionListener(listener);
    cancel.setActionCommand(cancelText);
    return panel;
}

From source file:components.ListDialogRunner.java

public static JPanel createUI() {
    //Create the labels.
    JLabel intro = new JLabel("The chosen name:");
    final JLabel name = new JLabel(names[1]);
    intro.setLabelFor(name);//from w  w  w.j  a  v  a  2 s . co  m

    //Use a wacky font if it exists. If not, this falls
    //back to a font we know exists.
    name.setFont(getAFont());

    //Create the button.
    final JButton button = new JButton("Pick a new name...");
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String selectedName = ListDialog.showDialog(frame, button, "Baby names ending in O:",
                    "Name Chooser", names, name.getText(), "Cosmo  ");
            name.setText(selectedName);
        }
    });

    //Create the panel we'll return and set up the layout.
    JPanel panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
    panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 10, 20));
    intro.setAlignmentX(JComponent.CENTER_ALIGNMENT);
    name.setAlignmentX(JComponent.CENTER_ALIGNMENT);
    button.setAlignmentX(JComponent.CENTER_ALIGNMENT);

    //Add the labels to the content pane.
    panel.add(intro);
    panel.add(Box.createVerticalStrut(5)); //extra space
    panel.add(name);

    //Add a vertical spacer that also guarantees us a minimum width:
    panel.add(Box.createRigidArea(new Dimension(150, 10)));

    //Add the button.
    panel.add(button);

    return panel;
}

From source file:max.hubbard.Factoring.Graphing.java

public static void makeGraphingInterface() {

    Interface.mainInterface();//from   w  w  w .j ava  2s  .  c om

    panel = new JPanel(new BorderLayout(3, 225));

    JPanel pan = new JPanel();
    JLabel area = new JLabel("Graphing");
    area.setBackground(Color.lightGray);
    area.setFont(new Font("Times New Roman", Font.BOLD, 20));
    pan.add(area, BorderLayout.CENTER);

    panel.add(pan, BorderLayout.NORTH);

    final JTextField field = new JTextField();
    panel.add(field, BorderLayout.CENTER);
    field.setFont(new Font("Times New Roman", Font.BOLD, 25));
    field.setText("x^4-2x^2-8");

    JButton start = new JButton("GO!");
    start.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            panel.updateUI();
            Main.label.setText("");
            graph(field.getText());
        }
    });

    panel.add(start, BorderLayout.SOUTH);

    Main.getFrame().add(panel, BorderLayout.EAST);

    Main.getFrame().pack();

}

From source file:FactoryDemo.java

public static JPanel demo2() {
    // Demo 2: Change the format of a field when the user presses a button.
    // We can't call field.setFormatter() because that's a protected method.
    // (Plus it wouldn't work anyway. The old factory would replace our new
    // formatter with an "old" one next time the field gains or loses
    // focus.)//from   w  ww.  j  a va2  s.  co m
    // The thing to do is send a new factory to field.setFormatterFactory().

    JPanel pan = new JPanel(new BorderLayout());
    pan.setBorder(new TitledBorder("Demo 2: change format midstream"));

    MaskFormatter lowercase = null;
    try {
        lowercase = new MaskFormatter("LLLL");
    } catch (ParseException pe) {
    }
    final JFormattedTextField field = new JFormattedTextField(lowercase);
    field.setValue("Fore");
    pan.add(field, BorderLayout.CENTER);

    final JButton change = new JButton("change format");
    JPanel changePanel = new JPanel();
    changePanel.add(change);
    pan.add(changePanel, BorderLayout.SOUTH);

    change.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            try {
                field.commitEdit(); // commit current edit (if any)
                MaskFormatter uppercase = new MaskFormatter("UUUU");
                DefaultFormatterFactory factory = new DefaultFormatterFactory(uppercase);
                field.setFormatterFactory(factory);
                change.setEnabled(false);
            } catch (ParseException pe) {
            }
        }
    });

    return pan;
}