Example usage for javax.swing AbstractAction AbstractAction

List of usage examples for javax.swing AbstractAction AbstractAction

Introduction

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

Prototype

public AbstractAction(String name) 

Source Link

Document

Creates an Action with the specified name.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Action action = new AbstractAction("Button Label") {
        // This method is called when the button is pressed
        public void actionPerformed(ActionEvent evt) {
            // Perform action...
        }/*from  w  w w  . j  a  va  2  s  .c o m*/
    };

    // Create the button from the action
    JButton button = new JButton(action);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Action action = new AbstractAction("CheckBox Label") {
        // called when the button is pressed
        public void actionPerformed(ActionEvent evt) {
            JCheckBox cb = (JCheckBox) evt.getSource();
            // Determine status
            boolean isSel = cb.isSelected();
            if (isSel) {
                // selected
            } else {
                // deselected
            }/*from   w w w  .  jav a  2 s.c  om*/
        }
    };

    // Create the checkbox from the action
    JCheckBox checkBox = new JCheckBox(action);

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Action action1 = new AbstractAction("RadioButton Label1") {
        public void actionPerformed(ActionEvent evt) {
        }/*from  www .  j a v  a  2  s  .  c  o m*/
    };
    Action action2 = new AbstractAction("RadioButton Label2") {
        public void actionPerformed(ActionEvent evt) {
        }
    };

    JRadioButton b1 = new JRadioButton(action1);
    JRadioButton b2 = new JRadioButton(action2);

    ButtonGroup group = new ButtonGroup();
    group.add(b1);
    group.add(b2);

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    // Create an action
    Action action = new AbstractAction("Action Label") {
        // This method is called when the action is triggered
        public void actionPerformed(ActionEvent evt) {
            Component c = (Component) evt.getSource();

            // Get the frame
            Component frame = SwingUtilities.getRoot(c);

            // Hide the frame
            frame.setVisible(false);/*  w w  w.j a  va2 s . com*/
        }
    };

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    final Action action = new AbstractAction("Action Name") {
        {/*  ww  w .jav  a  2  s.  com*/
            putValue(Action.SHORT_DESCRIPTION, "Tool Tip Text");

            putValue(Action.LONG_DESCRIPTION, "Help Text");

            Icon icon = new ImageIcon("icon.gif");
            putValue(Action.SMALL_ICON, icon);

            putValue(Action.MNEMONIC_KEY, new Integer(java.awt.event.KeyEvent.VK_A));
            putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("control F2"));
        }

        public void actionPerformed(ActionEvent evt) {
            System.out.println("action");
        }
    };

    JButton button = new JButton(action);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    final Action action = new AbstractAction("Action Name") {
        public void actionPerformed(ActionEvent evt) {
            System.out.println("action");
        }/*  w  w w  . j  av a 2s  .  c  om*/
    };

    JFrame frame = new JFrame();
    JButton button = new JButton(action);

    JTextField textfield = new JTextField();
    textfield.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke("F2"),
            action.getValue(Action.NAME));
    textfield.getActionMap().put(action.getValue(Action.NAME), action);
}

From source file:Main.java

public static void main(final String args[]) {
    Action action = new AbstractAction("CheckBox Label") {
        public void actionPerformed(ActionEvent evt) {
            System.out.println("called");
        }//from w w  w  .  java2 s  .  c om
    };

    JFrame frame = new JFrame("MenuSample Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JMenuBar menuBar = new JMenuBar();

    // File Menu, F - Mnemonic
    JMenu fileMenu = new JMenu("File");
    fileMenu.setMnemonic(KeyEvent.VK_F);
    menuBar.add(fileMenu);

    // File->New, N - Mnemonic
    JMenuItem newMenuItem = new JMenuItem("New", KeyEvent.VK_N);
    fileMenu.add(newMenuItem);

    JCheckBoxMenuItem caseMenuItem = new JCheckBoxMenuItem(action);
    caseMenuItem.setMnemonic(KeyEvent.VK_C);
    fileMenu.add(caseMenuItem);

    frame.setJMenuBar(menuBar);
    frame.setSize(350, 250);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setUndecorated(true);/*w  w  w  .  j a v  a 2s  . co  m*/
    JPanel panel = new JPanel();
    panel.add(new JLabel("Stackoverflow!"));
    panel.add(new JButton(new AbstractAction("Close") {
        @Override
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    }));
    f.add(panel);
    f.pack();
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JScrollPane sPane = new JScrollPane();
    sPane.setPreferredSize(new Dimension(200, 150));
    JButton button = new JButton(new AbstractAction("Create Table") {
        public void actionPerformed(ActionEvent arg0) {
            DefaultTableModel model = new DefaultTableModel(new Integer[][] { { 1, 2 }, { 3, 4 } },
                    new String[] { "A", "B" });
            JTable table = new JTable(model);
            sPane.getViewport().add(table);
        }//from w  w w .j  av  a  2  s  . c  o m
    });
    JPanel panel = new JPanel();
    panel.add(sPane);
    panel.add(button);
    JOptionPane.showMessageDialog(null, panel);

}

From source file:Main.java

public static void main(String[] argv) {
    InputMap im = new JTextArea().getInputMap(JComponent.WHEN_FOCUSED);
    im.put(KeyStroke.getKeyStroke("F2"), "actionName");

    ActionMap am = new JTextArea().getActionMap();
    am.put("actionName", new AbstractAction("actionName") {
        public void actionPerformed(ActionEvent evt) {
            System.out.println((JTextComponent) evt.getSource());
        }//from   w w w .  ja  va2 s. c o m
    });
    im.put(KeyStroke.getKeyStroke("F3"), "actionName2");
    am.put("actionName2", new AbstractAction("actionName2") {
        public void actionPerformed(ActionEvent evt) {
            System.out.println((JTextComponent) evt.getSource());
        }
    });
    JButton component1 = new JButton("button 1");
    JButton component2 = new JButton("button 2");

    component1.setInputMap(JComponent.WHEN_FOCUSED, im);
    component2.setInputMap(JComponent.WHEN_FOCUSED, im);

    component1.setActionMap(am);
    component2.setActionMap(am);

}