Example usage for javax.swing JButton setActionCommand

List of usage examples for javax.swing JButton setActionCommand

Introduction

In this page you can find the example usage for javax.swing JButton setActionCommand.

Prototype

public void setActionCommand(String actionCommand) 

Source Link

Document

Sets the action command for this button.

Usage

From source file:ActionButtonSample.java

public static void main(String args[]) {
    JFrame frame = new JFrame("DefaultButton");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            String command = actionEvent.getActionCommand();
            System.out.println("Selected: " + command);
        }/*from   w  w  w . j av  a2 s .  c o m*/
    };
    frame.setLayout(new GridLayout(2, 2, 10, 10));
    JButton button1 = new JButton("Text Button");
    button1.setActionCommand("First");
    button1.addActionListener(actionListener);
    frame.add(button1);
    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:ChangeLook.java

public static void main(String args[]) {
    final JFrame frame = new JFrame("Change Look");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            String lafClassName = null;
            lafClassName = actionEvent.getActionCommand();
            String finalLafClassName = lafClassName;
            try {
                UIManager.setLookAndFeel(finalLafClassName);
                SwingUtilities.updateComponentTreeUI(frame);
            } catch (Exception exception) {
                JOptionPane.showMessageDialog(frame, "Can't change look and feel", "Invalid PLAF",
                        JOptionPane.ERROR_MESSAGE);
            }//w w  w.j  a  v a 2 s  . c om

        }
    };

    UIManager.LookAndFeelInfo looks[] = UIManager.getInstalledLookAndFeels();

    JComboBox comboBox = new JComboBox(new String[] { "a", "b" });

    JPanel panel = new JPanel();

    for (int i = 0, n = looks.length; i < n; i++) {
        JButton button = new JButton(looks[i].getName());
        button.setActionCommand(looks[i].getClassName());
        button.addActionListener(actionListener);
        panel.add(button);
    }

    frame.add(comboBox, BorderLayout.NORTH);
    frame.add(panel, BorderLayout.SOUTH);
    frame.setSize(350, 150);
    frame.setVisible(true);

}

From source file:MainClass.java

public static void main(String[] a) {
    final int STRING_POSITION = 1;
    Object buttonColors[][] = { { Color.RED, "RED" }, { Color.BLUE, "BLUE" }, { Color.GREEN, "GREEN" },
            { Color.BLACK, "BLACK" }, null, // separator
            { Color.CYAN, "CYAN" } };

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    ActionListener actionListener = new TheActionListener();

    JToolBar toolbar = new JToolBar();
    toolbar.setRollover(true);//from  w  w w . ja v  a 2 s . c o  m

    for (Object[] color : buttonColors) {
        if (color == null) {
            toolbar.addSeparator();
        } else {
            Icon icon = MetalIconFactory.getTreeComputerIcon();
            JButton button = new JButton(icon);
            button.setActionCommand((String) color[STRING_POSITION]);
            button.addActionListener(actionListener);
            toolbar.add(button);
        }
    }

    Action action = new ShowAction(toolbar);
    JButton button = new JButton(action);
    toolbar.add(button);

    Container contentPane = frame.getContentPane();
    contentPane.add(toolbar, BorderLayout.NORTH);
    JTextArea textArea = new JTextArea();
    JScrollPane pane = new JScrollPane(textArea);
    contentPane.add(pane, BorderLayout.CENTER);
    frame.setSize(350, 150);
    frame.setVisible(true);

}

From source file:ActionButtonSample.java

public static void main(String args[]) {
    JFrame frame = new JFrame("DefaultButton");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            String command = actionEvent.getActionCommand();
            System.out.println("Selected: " + command);
        }//ww w  . j a v a  2 s . c om
    };

    Container content = frame.getContentPane();
    content.setLayout(new GridLayout(2, 2));

    JButton button1 = new JButton("Text Button");
    button1.setMnemonic(KeyEvent.VK_B);
    button1.setActionCommand("First");
    button1.addActionListener(actionListener);
    content.add(button1);

    Icon warnIcon = new ImageIcon("Warn.gif");
    JButton button2 = new JButton(warnIcon);
    button2.setActionCommand("Second");
    button2.addActionListener(actionListener);
    content.add(button2);

    JButton button3 = new JButton("Warning", warnIcon);
    button3.setActionCommand("Third");
    button3.addActionListener(actionListener);
    content.add(button3);

    String htmlButton = "<html><sup>HTML</sup> <sub><em>Button</em></sub><br>"
            + "<font color=\"#FF0080\"><u>Multi-line</u></font>";
    JButton button4 = new JButton(htmlButton);
    button4.setActionCommand("Fourth");
    button4.addActionListener(actionListener);
    content.add(button4);

    JRootPane rootPane = frame.getRootPane();
    rootPane.setDefaultButton(button2);

    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:MainClass.java

public static void main(String args[]) {
    final JFrame frame = new JFrame("Change Look");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            Object source = actionEvent.getSource();
            String lafClassName = null;
            if (source instanceof JComboBox) {
                JComboBox comboBox = (JComboBox) source;
                lafClassName = (String) comboBox.getSelectedItem();
            } else if (source instanceof JButton) {
                lafClassName = actionEvent.getActionCommand();
            }/*from  w  w  w .  jav  a 2s .  c om*/
            if (lafClassName != null) {
                final String finalLafClassName = lafClassName;
                try {
                    UIManager.setLookAndFeel(finalLafClassName);
                    SwingUtilities.updateComponentTreeUI(frame);
                } catch (Exception exception) {
                    JOptionPane.showMessageDialog(frame, "Can't change look and feel", "Invalid PLAF",
                            JOptionPane.ERROR_MESSAGE);
                }
            }
        }
    };

    UIManager.LookAndFeelInfo looks[] = UIManager.getInstalledLookAndFeels();

    DefaultComboBoxModel model = new DefaultComboBoxModel();
    JComboBox comboBox = new JComboBox(model);

    JPanel panel = new JPanel();

    for (int i = 0, n = looks.length; i < n; i++) {
        JButton button = new JButton(looks[i].getName());
        model.addElement(looks[i].getClassName());
        button.setActionCommand(looks[i].getClassName());
        button.addActionListener(actionListener);
        panel.add(button);
    }

    comboBox.addActionListener(actionListener);

    frame.add(comboBox, BorderLayout.NORTH);
    frame.add(panel, BorderLayout.SOUTH);
    frame.setSize(350, 150);
    frame.setVisible(true);
}

From source file:OverlaySample.java

public static void main(String args[]) {

    ActionListener generalActionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            JComponent comp = (JComponent) actionEvent.getSource();
            System.out.println(actionEvent.getActionCommand() + ": " + comp.getBounds());
        }/*  www. j a v  a 2 s .  c o  m*/
    };

    ActionListener sizingActionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            setupButtons(actionEvent.getActionCommand());
        }
    };

    JFrame frame = new JFrame("Overlay Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel panel = new JPanel();
    LayoutManager overlay = new OverlayLayout(panel);
    panel.setLayout(overlay);

    Object settings[][] = { { "Small", new Dimension(25, 25), Color.white },
            { "Medium", new Dimension(50, 50), Color.gray },
            { "Large", new Dimension(100, 100), Color.black } };
    JButton buttons[] = { smallButton, mediumButton, largeButton };

    for (int i = 0, n = settings.length; i < n; i++) {
        JButton button = buttons[i];
        button.addActionListener(generalActionListener);
        button.setActionCommand((String) settings[i][0]);
        button.setMaximumSize((Dimension) settings[i][1]);
        button.setBackground((Color) settings[i][2]);
        panel.add(button);
    }

    setupButtons(SET_CENTRAL);

    JPanel actionPanel = new JPanel();
    actionPanel.setBorder(BorderFactory.createTitledBorder("Change Alignment"));
    String actionSettings[] = { SET_MINIMUM, SET_MAXIMUM, SET_CENTRAL, SET_MIXED };
    for (int i = 0, n = actionSettings.length; i < n; i++) {
        JButton button = new JButton(actionSettings[i]);
        button.addActionListener(sizingActionListener);
        actionPanel.add(button);
    }

    Container contentPane = frame.getContentPane();
    contentPane.add(panel, BorderLayout.CENTER);
    contentPane.add(actionPanel, BorderLayout.SOUTH);

    frame.setSize(400, 300);
    frame.setVisible(true);
}

From source file:SwingToolBarSample.java

public static void main(String args[]) {

    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            System.out.println(actionEvent.getActionCommand());
        }/* w  w  w.j a  v  a2  s.c om*/
    };

    JFrame frame = new JFrame("JToolBar Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JToolBar toolbar = new JToolBar();
    toolbar.putClientProperty("JToolBar.isRollover", Boolean.TRUE);

    for (int i = 0, n = buttonColors.length; i < n; i++) {
        Object color[] = buttonColors[i];
        if (color == null) {
            toolbar.addSeparator();
        } else {
            Icon icon = new DiamondIcon((Color) color[COLOR_POSITION], true, 20, 20);
            JButton button = new JButton(icon);
            button.setActionCommand((String) color[STRING_POSITION]);
            button.addActionListener(actionListener);
            toolbar.add(button);
        }
    }

    Action action = new ActionMenuSample.ShowAction(frame);
    toolbar.add(action);

    Container contentPane = frame.getContentPane();
    contentPane.add(toolbar, BorderLayout.NORTH);
    JTextArea textArea = new JTextArea();
    JScrollPane pane = new JScrollPane(textArea);
    contentPane.add(pane, BorderLayout.CENTER);
    frame.setSize(350, 150);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(final String args[]) {
    JButton button = new JButton("Text Button");
    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            String command = actionEvent.getActionCommand();
            System.out.println("Selected: " + command);
        }//from w w w. j  av a 2  s  .c  o m
    };

    button.setActionCommand("First");
    button.addActionListener(actionListener);

    JOptionPane.showMessageDialog(null, button);
}

From source file:ListProperties.java

public static void main(String args[]) {
    final JFrame frame = new JFrame("List Properties");

    final CustomTableModel model = new CustomTableModel();
    model.uiDefaultsUpdate(UIManager.getDefaults());
    TableSorter sorter = new TableSorter(model);

    JTable table = new JTable(sorter);
    TableHeaderSorter.install(sorter, table);

    table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);

    UIManager.LookAndFeelInfo looks[] = UIManager.getInstalledLookAndFeels();

    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            final String lafClassName = actionEvent.getActionCommand();
            Runnable runnable = new Runnable() {
                public void run() {
                    try {
                        UIManager.setLookAndFeel(lafClassName);
                        SwingUtilities.updateComponentTreeUI(frame);
                        // Added
                        model.uiDefaultsUpdate(UIManager.getDefaults());
                    } catch (Exception exception) {
                        JOptionPane.showMessageDialog(frame, "Can't change look and feel", "Invalid PLAF",
                                JOptionPane.ERROR_MESSAGE);
                    }/*from w  ww .jav  a2 s  .  com*/
                }
            };
            SwingUtilities.invokeLater(runnable);
        }
    };

    JToolBar toolbar = new JToolBar();
    for (int i = 0, n = looks.length; i < n; i++) {
        JButton button = new JButton(looks[i].getName());
        button.setActionCommand(looks[i].getClassName());
        button.addActionListener(actionListener);
        toolbar.add(button);
    }

    Container content = frame.getContentPane();
    content.add(toolbar, BorderLayout.NORTH);
    JScrollPane scrollPane = new JScrollPane(table);
    content.add(scrollPane, BorderLayout.CENTER);
    frame.setSize(400, 400);
    frame.setVisible(true);
}

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);/*from ww w. j  av a 2  s.co m*/

    return button;
}