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() 

Source Link

Document

Creates an Action .

Usage

From source file:MainClass.java

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

    Action actionListener = new AbstractAction() {
        public void actionPerformed(ActionEvent actionEvent) {
            System.out.println("Got an M");
        }//www .  j a va2s .com
    };

    JPanel content = (JPanel) frame.getContentPane();
    KeyStroke stroke = KeyStroke.getKeyStroke("M");

    InputMap inputMap = content.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    inputMap.put(stroke, "OPEN");
    content.getActionMap().put("OPEN", actionListener);

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

From source file:Main.java

public static void main(String[] args) {
    JList<String> list = new JList<String>(new String[] { "one", "two", "three", "four", "five" });
    JScrollPane scrollPane = new JScrollPane(list);
    JButton btn = new JButton(new AbstractAction() {
        {//from w w w  .ja  v a  2s  .  co  m
            putValue(NAME, "Select");
        }

        @Override
        public void actionPerformed(ActionEvent evt) {
            list.setSelectedIndex(0);
        }
    });
    JPanel panel = new JPanel();
    panel.add(scrollPane);
    panel.add(btn);
    JOptionPane.showMessageDialog(null, panel);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    MyFileChooser chooser = new MyFileChooser();
    chooser.setDialogType(JFileChooser.SAVE_DIALOG);
    final JDialog dialog = chooser.createDialog(null);
    chooser.addActionListener(new AbstractAction() {
        public void actionPerformed(ActionEvent evt) {
            JFileChooser chooser = (JFileChooser) evt.getSource();
            if (JFileChooser.APPROVE_SELECTION.equals(evt.getActionCommand())) {
                dialog.setVisible(false);
            } else if (JFileChooser.CANCEL_SELECTION.equals(evt.getActionCommand())) {
                dialog.setVisible(false);
            }/* w w w  .  ja  v  a  2s .  c o m*/
        }
    });

    dialog.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            dialog.setVisible(false);
        }
    });

    dialog.setVisible(true);
}

From source file:Main.java

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

    frame.setLayout(new BorderLayout());
    frame.add(new JLabel("Hit Escape to exit full screen", JLabel.CENTER), BorderLayout.CENTER);

    frame.setSize(300, 300);/*www.  j av a 2 s .c o  m*/

    KeyStroke escapeKeyStroke = KeyStroke.getKeyStroke("ESCAPE");
    Action escapeAction = new AbstractAction() {
        public void actionPerformed(ActionEvent e) {
            GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice()
                    .setFullScreenWindow(null);
        }
    };

    frame.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(escapeKeyStroke, "ESCAPE");
    frame.getRootPane().getActionMap().put("ESCAPE", escapeAction);

    GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(frame);

}

From source file:FrameKey.java

public static void main(String args[]) {
    final JFrame frame = new JFrame("Frame Key");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Action actionListener = new AbstractAction() {
        public void actionPerformed(ActionEvent actionEvent) {
            JDialog dialog = new EscapeDialog(frame, "Hey");
            JButton button = new JButton("Okay");
            ActionListener innerActionListener = new ActionListener() {
                public void actionPerformed(ActionEvent actionEvent) {
                    System.out.println("Dialog Button Selected");
                }/*w ww.j a v a 2 s .c o  m*/
            };
            button.addActionListener(innerActionListener);
            dialog.getContentPane().add(button, BorderLayout.SOUTH);
            dialog.setSize(200, 200);
            dialog.show();
        }
    };

    JPanel content = (JPanel) frame.getContentPane();
    KeyStroke stroke = KeyStroke.getKeyStroke("M");

    InputMap inputMap = content.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    inputMap.put(stroke, "OPEN");
    content.getActionMap().put("OPEN", actionListener);

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

From source file:LoadSave.java

public static void main(String args[]) {
    final String filename = "text.out";
    JFrame frame = new JFrame("Loading/Saving Example");
    Container content = frame.getContentPane();

    final JTextField textField = new JTextField();
    content.add(textField, BorderLayout.NORTH);

    JPanel panel = new JPanel();

    Action loadAction = new AbstractAction() {
        public void actionPerformed(ActionEvent e) {
            try {
                doLoadCommand(textField, filename);
            } catch (Exception e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();/* w  ww.  j a v  a 2 s .com*/
            }
        }
    };
    loadAction.putValue(Action.NAME, "Load");
    JButton loadButton = new JButton(loadAction);
    panel.add(loadButton);

    Action saveAction = new AbstractAction() {
        public void actionPerformed(ActionEvent e) {
            try {
                doSaveCommand(textField, filename);
            } catch (Exception e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
    };
    saveAction.putValue(Action.NAME, "Save");
    JButton saveButton = new JButton(saveAction);
    panel.add(saveButton);

    Action clearAction = new AbstractAction() {
        {
            putValue(Action.NAME, "Clear");
        }

        public void actionPerformed(ActionEvent e) {
            textField.setText("");
        }
    };
    JButton clearButton = new JButton(clearAction);
    panel.add(clearButton);

    content.add(panel, BorderLayout.SOUTH);

    frame.setSize(250, 150);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String args[]) {
    String ACTION_KEY = "The Action";

    JFrame frame = new JFrame("KeyStroke Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JButton buttonA = new JButton("FOCUSED (control alt 7)");
    JButton buttonB = new JButton("FOCUS/RELEASE (VK_ENTER)");
    JButton buttonC = new JButton("ANCESTOR  (VK_F4+SHIFT_MASK)");
    JButton buttonD = new JButton("WINDOW (' ')");

    Action actionListener = new AbstractAction() {
        public void actionPerformed(ActionEvent actionEvent) {
            JButton source = (JButton) actionEvent.getSource();
            System.out.println("Activated: " + source.getText());
        }/* w  w  w .j  av  a  2  s . c om*/
    };

    KeyStroke controlAlt7 = KeyStroke.getKeyStroke("control alt 7");
    InputMap inputMap = buttonA.getInputMap();
    inputMap.put(controlAlt7, ACTION_KEY);
    ActionMap actionMap = buttonA.getActionMap();
    Object[] keys = actionMap.allKeys();
    actionMap.put(ACTION_KEY, actionListener);

    KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, true);
    inputMap = buttonB.getInputMap();
    inputMap.put(enter, ACTION_KEY);
    buttonB.setActionMap(actionMap);

    KeyStroke shiftF4 = KeyStroke.getKeyStroke(KeyEvent.VK_F4, InputEvent.SHIFT_MASK);
    inputMap = buttonC.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    inputMap.put(shiftF4, ACTION_KEY);
    buttonC.setActionMap(actionMap);

    KeyStroke space = KeyStroke.getKeyStroke(' ');
    inputMap = buttonD.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    inputMap.put(space, ACTION_KEY);
    buttonD.setActionMap(actionMap);

    frame.setLayout(new GridLayout(2, 2));
    frame.add(buttonA);
    frame.add(buttonB);
    frame.add(buttonC);
    frame.add(buttonD);

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

From source file:Main.java

public static void main(String args[]) {
    String ACTION_KEY = "The Action";

    JFrame frame = new JFrame("KeyStroke Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JButton buttonA = new JButton("FOCUSED (control alt 7)");
    JButton buttonB = new JButton("FOCUS/RELEASE (VK_ENTER)");
    JButton buttonC = new JButton("ANCESTOR  (VK_F4+SHIFT_MASK)");
    JButton buttonD = new JButton("WINDOW (' ')");

    Action actionListener = new AbstractAction() {
        public void actionPerformed(ActionEvent actionEvent) {
            JButton source = (JButton) actionEvent.getSource();
            System.out.println("Activated: " + source.getText());
        }// ww  w .ja  va  2  s  .c  o  m
    };

    KeyStroke controlAlt7 = KeyStroke.getKeyStroke("control alt 7");
    InputMap inputMap = buttonA.getInputMap();
    inputMap.put(controlAlt7, ACTION_KEY);
    ActionMap actionMap = buttonA.getActionMap();
    actionMap.clear();
    actionMap.put(ACTION_KEY, actionListener);

    KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, true);
    inputMap = buttonB.getInputMap();
    inputMap.put(enter, ACTION_KEY);
    buttonB.setActionMap(actionMap);

    KeyStroke shiftF4 = KeyStroke.getKeyStroke(KeyEvent.VK_F4, InputEvent.SHIFT_MASK);
    inputMap = buttonC.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    inputMap.put(shiftF4, ACTION_KEY);
    buttonC.setActionMap(actionMap);

    KeyStroke space = KeyStroke.getKeyStroke(' ');
    inputMap = buttonD.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    inputMap.put(space, ACTION_KEY);
    buttonD.setActionMap(actionMap);

    frame.setLayout(new GridLayout(2, 2));
    frame.add(buttonA);
    frame.add(buttonB);
    frame.add(buttonC);
    frame.add(buttonD);

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

From source file:Main.java

public static void main(String args[]) {
    String ACTION_KEY = "The Action";

    JFrame frame = new JFrame("KeyStroke Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JButton buttonA = new JButton("FOCUSED (control alt 7)");
    JButton buttonB = new JButton("FOCUS/RELEASE (VK_ENTER)");
    JButton buttonC = new JButton("ANCESTOR  (VK_F4+SHIFT_MASK)");
    JButton buttonD = new JButton("WINDOW (' ')");

    Action actionListener = new AbstractAction() {
        public void actionPerformed(ActionEvent actionEvent) {
            JButton source = (JButton) actionEvent.getSource();
            System.out.println("Activated: " + source.getText());
        }//w  w  w  .ja  va 2 s.co m
    };

    KeyStroke controlAlt7 = KeyStroke.getKeyStroke("control alt 7");
    InputMap inputMap = buttonA.getInputMap();
    inputMap.put(controlAlt7, ACTION_KEY);
    ActionMap actionMap = buttonA.getActionMap();
    actionMap.put(ACTION_KEY, actionListener);

    KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, true);
    inputMap = buttonB.getInputMap();
    inputMap.put(enter, ACTION_KEY);
    buttonB.setActionMap(actionMap);

    KeyStroke shiftF4 = KeyStroke.getKeyStroke(KeyEvent.VK_F4, InputEvent.SHIFT_MASK);
    inputMap = buttonC.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    inputMap.put(shiftF4, ACTION_KEY);
    buttonC.setActionMap(actionMap);

    KeyStroke space = KeyStroke.getKeyStroke(' ');
    inputMap = buttonD.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    inputMap.put(space, ACTION_KEY);
    buttonD.setActionMap(actionMap);

    frame.setLayout(new GridLayout(2, 2));
    frame.add(buttonA);
    frame.add(buttonB);
    frame.add(buttonC);
    frame.add(buttonD);

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

From source file:Main.java

public static void main(String args[]) {
    String ACTION_KEY = "The Action";

    JFrame frame = new JFrame("KeyStroke Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JButton buttonA = new JButton("FOCUSED (control alt 7)");
    JButton buttonB = new JButton("FOCUS/RELEASE (VK_ENTER)");
    JButton buttonC = new JButton("ANCESTOR  (VK_F4+SHIFT_MASK)");
    JButton buttonD = new JButton("WINDOW (' ')");

    Action actionListener = new AbstractAction() {
        public void actionPerformed(ActionEvent actionEvent) {
            JButton source = (JButton) actionEvent.getSource();
            System.out.println("Activated: " + source.getText());
        }// w ww .j  a  v a  2 s.  c o  m
    };

    KeyStroke controlAlt7 = KeyStroke.getKeyStroke("control alt 7");
    InputMap inputMap = buttonA.getInputMap();
    inputMap.put(controlAlt7, ACTION_KEY);
    ActionMap actionMap = buttonA.getActionMap();
    Object[] keys = actionMap.keys();
    actionMap.put(ACTION_KEY, actionListener);

    KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, true);
    inputMap = buttonB.getInputMap();
    inputMap.put(enter, ACTION_KEY);
    buttonB.setActionMap(actionMap);

    KeyStroke shiftF4 = KeyStroke.getKeyStroke(KeyEvent.VK_F4, InputEvent.SHIFT_MASK);
    inputMap = buttonC.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    inputMap.put(shiftF4, ACTION_KEY);
    buttonC.setActionMap(actionMap);

    KeyStroke space = KeyStroke.getKeyStroke(' ');
    inputMap = buttonD.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    inputMap.put(space, ACTION_KEY);
    buttonD.setActionMap(actionMap);

    frame.setLayout(new GridLayout(2, 2));
    frame.add(buttonA);
    frame.add(buttonB);
    frame.add(buttonC);
    frame.add(buttonD);

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