Example usage for java.awt.event ActionEvent ACTION_PERFORMED

List of usage examples for java.awt.event ActionEvent ACTION_PERFORMED

Introduction

In this page you can find the example usage for java.awt.event ActionEvent ACTION_PERFORMED.

Prototype

int ACTION_PERFORMED

To view the source code for java.awt.event ActionEvent ACTION_PERFORMED.

Click Source Link

Document

This event id indicates that a meaningful action occurred.

Usage

From source file:EventObject.java

public static void main(String[] args) {
    JFrame f = new JFrame();
    JButton ok = new JButton("Ok");

    ok.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            Calendar cal = Calendar.getInstance();
            cal.setTimeInMillis(event.getWhen());
            Locale locale = Locale.getDefault();
            String s = DateFormat.getTimeInstance(DateFormat.SHORT, locale).format(new Date());

            if (event.getID() == ActionEvent.ACTION_PERFORMED)
                System.out.println(" Event Id: ACTION_PERFORMED");

            System.out.println(" Time: " + s);

            String source = event.getSource().getClass().getName();
            System.out.println(" Source: " + source);

            int mod = event.getModifiers();
            if ((mod & ActionEvent.ALT_MASK) > 0)
                System.out.println("Alt ");

            if ((mod & ActionEvent.SHIFT_MASK) > 0)
                System.out.println("Shift ");

            if ((mod & ActionEvent.META_MASK) > 0)
                System.out.println("Meta ");

            if ((mod & ActionEvent.CTRL_MASK) > 0)
                System.out.println("Ctrl ");

        }/*from w  ww .  ja v a  2s.  com*/
    });

    f.add(ok);

    f.setSize(420, 250);
    f.setLocationRelativeTo(null);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
}

From source file:Main.java

public static boolean invoke(Action action, Object source) {
    if (action == null || !action.isEnabled()) {
        return false;
    }/*from w  w  w . j a va2s .com*/
    ActionEvent evt = new ActionEvent(source, ActionEvent.ACTION_PERFORMED,
            (String) action.getValue(Action.ACTION_COMMAND_KEY), 0);
    action.actionPerformed(evt);
    return true;
}

From source file:Main.java

/**
 * Programmatically perform action on textfield.This does the same
 * thing as if the user had pressed enter key in textfield.
 *
 * @param textField textField on which action to be preformed
 *///from  w  w  w  .  j a  va 2 s . com
public static void doAction(JTextField textField) {
    String command = null;
    if (textField.getAction() != null)
        command = (String) textField.getAction().getValue(Action.ACTION_COMMAND_KEY);
    ActionEvent event = null;

    for (ActionListener listener : textField.getActionListeners()) {
        if (event == null)
            event = new ActionEvent(textField, ActionEvent.ACTION_PERFORMED, command,
                    System.currentTimeMillis(), 0);
        listener.actionPerformed(event);
    }
}

From source file:MyLabel.java

public MyLabel(String msg) {

    super(msg);/*from   w  ww.j  av  a2 s  . c o  m*/

    addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent me) {
            fireActionPerformed(new ActionEvent(MyLabel.this, ActionEvent.ACTION_PERFORMED, "SecretMessage"));
        }
    });
}

From source file:KeyTextComponent.java

public KeyTextComponent() {
    setBackground(Color.CYAN);// w  ww. java2  s.  c  o m
    KeyListener internalKeyListener = new KeyAdapter() {
        public void keyPressed(KeyEvent keyEvent) {
            if (actionListenerList != null) {
                int keyCode = keyEvent.getKeyCode();
                String keyText = KeyEvent.getKeyText(keyCode);
                ActionEvent actionEvent = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, keyText);
                actionListenerList.actionPerformed(actionEvent);
            }
        }
    };
    MouseListener internalMouseListener = new MouseAdapter() {
        public void mousePressed(MouseEvent mouseEvent) {
            requestFocusInWindow();
        }
    };
    addKeyListener(internalKeyListener);
    addMouseListener(internalMouseListener);
}

From source file:Main.java

public void actionPerformed(ActionEvent e) {

    Locale locale = Locale.getDefault();
    Date date = new Date(e.getWhen());
    String s = DateFormat.getTimeInstance(DateFormat.SHORT, locale).format(date);

    if (!model.isEmpty()) {
        model.clear();//  w ww.  java  2s  .c  o  m
    }

    if (e.getID() == ActionEvent.ACTION_PERFORMED) {
        model.addElement(" Event Id: ACTION_PERFORMED");

    }

    model.addElement("Time: " + s);

    String source = e.getSource().getClass().getName();

    int mod = e.getModifiers();

    StringBuffer buffer = new StringBuffer("Modifiers: ");

    if ((mod & ActionEvent.ALT_MASK) > 0) {
        buffer.append("Alt ");

    }

    if ((mod & ActionEvent.SHIFT_MASK) > 0) {
        buffer.append("Shift ");

    }

    if ((mod & ActionEvent.META_MASK) > 0) {
        buffer.append("Meta ");

    }

    if ((mod & ActionEvent.CTRL_MASK) > 0) {
        buffer.append("Ctrl ");

    }
    model.addElement(buffer);

}

From source file:Main.java

public KeyTextComponent() {
    setBackground(Color.cyan);/* ww w.  ja  va2 s.c  o m*/
    KeyListener internalKeyListener = new KeyAdapter() {
        public void keyPressed(KeyEvent keyEvent) {
            if (actionListenerList != null) {
                int keyCode = keyEvent.getKeyCode();
                String keyText = KeyEvent.getKeyText(keyCode);
                ActionEvent actionEvent = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, keyText);
                actionListenerList.actionPerformed(actionEvent);
            }
        }
    };

    MouseListener internalMouseListener = new MouseAdapter() {
        public void mousePressed(MouseEvent mouseEvent) {
            requestFocus();
        }
    };

    addKeyListener(internalKeyListener);
    addMouseListener(internalMouseListener);
}

From source file:SecretTest.java

public SecretLabel(String msg) {
    super(msg);/* w w w  .  j a  v  a2  s .co m*/
    addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent me) {
            fireActionPerformed(
                    new ActionEvent(SecretLabel.this, ActionEvent.ACTION_PERFORMED, "SecretMessage"));
        }
    });
}

From source file:MainClass.java

public KeyTextComponent() {
    setBackground(Color.CYAN);//from w w w. j a v a2 s  .c o  m
    KeyListener internalKeyListener = new KeyAdapter() {
        public void keyPressed(KeyEvent keyEvent) {
            if (actionListenerList != null) {
                int keyCode = keyEvent.getKeyCode();
                String keyText = KeyEvent.getKeyText(keyCode);
                ActionEvent actionEvent = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, keyText);
                fireActionPerformed(actionEvent);
            }
        }
    };

    MouseListener internalMouseListener = new MouseAdapter() {
        public void mousePressed(MouseEvent mouseEvent) {
            requestFocusInWindow();
        }
    };

    addKeyListener(internalKeyListener);
    addMouseListener(internalMouseListener);
}

From source file:KeyTextTester2.java

public KeyTextComponent2() {
    setBackground(Color.cyan);//from   w w w. j a  v a 2  s.c o  m
    KeyListener internalKeyListener = new KeyAdapter() {
        public void keyPressed(KeyEvent keyEvent) {
            if (actionListenerList != null) {
                int keyCode = keyEvent.getKeyCode();
                String keyText = KeyEvent.getKeyText(keyCode);
                ActionEvent actionEvent = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, keyText);
                fireActionPerformed(actionEvent);
            }
        }
    };

    MouseListener internalMouseListener = new MouseAdapter() {
        public void mousePressed(MouseEvent mouseEvent) {
            requestFocus();
        }
    };

    addKeyListener(internalKeyListener);
    addMouseListener(internalMouseListener);
}