Example usage for java.awt.event ActionListener actionPerformed

List of usage examples for java.awt.event ActionListener actionPerformed

Introduction

In this page you can find the example usage for java.awt.event ActionListener actionPerformed.

Prototype

public void actionPerformed(ActionEvent e);

Source Link

Document

Invoked when an action occurs.

Usage

From source file:MenuY.java

public static void main(String args[]) {
    ActionListener actionListener = new MenuActionListener();
    MenuKeyListener menuKeyListener = new MyMenuKeyListener();
    ChangeListener cListener = new MyChangeListener();
    MenuListener menuListener = new MyMenuListener();
    MenuSelectionManager manager = MenuSelectionManager.defaultManager();
    manager.addChangeListener(cListener);
    JFrame frame = new JFrame("MenuSample Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JMenuBar bar = new VerticalMenuBar();
    //    JMenuBar bar = new JMenuBar();

    // File Menu, F - Mnemonic
    JMenu file = new JMenu("File");
    file.setMnemonic(KeyEvent.VK_F);
    file.addChangeListener(cListener);//from   ww w .ja va 2 s  .c  o  m
    file.addMenuListener(menuListener);
    file.addMenuKeyListener(menuKeyListener);
    JPopupMenu popupMenu = file.getPopupMenu();
    popupMenu.setLayout(new GridLayout(3, 3));
    bar.add(file);

    // File->New, N - Mnemonic
    JMenuItem newItem = new JMenuItem("New", KeyEvent.VK_N);
    newItem.addActionListener(actionListener);
    newItem.addChangeListener(cListener);
    newItem.addMenuKeyListener(menuKeyListener);
    file.add(newItem);

    // File->Open, O - Mnemonic
    JMenuItem openItem = new JMenuItem("Open", KeyEvent.VK_O);
    openItem.addActionListener(actionListener);
    openItem.addChangeListener(cListener);
    openItem.addMenuKeyListener(menuKeyListener);
    file.add(openItem);

    // File->Close, C - Mnemonic
    JMenuItem closeItem = new JMenuItem("Close", KeyEvent.VK_C);
    closeItem.addActionListener(actionListener);
    closeItem.addChangeListener(cListener);
    closeItem.addMenuKeyListener(menuKeyListener);
    file.add(closeItem);

    // Separator
    file.addSeparator();

    // File->Save, S - Mnemonic
    JMenuItem saveItem = new JMenuItem("Save", KeyEvent.VK_S);
    saveItem.addActionListener(actionListener);
    saveItem.addChangeListener(cListener);
    saveItem.addMenuKeyListener(menuKeyListener);
    file.add(saveItem);

    // Separator
    file.addSeparator();

    // File->Exit, X - Mnemonic
    JMenuItem exitItem = new JMenuItem("Exit", KeyEvent.VK_X);
    exitItem.addActionListener(actionListener);
    exitItem.addChangeListener(cListener);
    exitItem.addMenuKeyListener(menuKeyListener);
    file.add(exitItem);

    // Edit Menu, E - Mnemonic
    JMenu edit = new JMenu("Edit");
    edit.setMnemonic(KeyEvent.VK_E);
    edit.addChangeListener(cListener);
    edit.addMenuListener(menuListener);
    edit.addMenuKeyListener(menuKeyListener);
    bar.add(edit);

    // Edit->Cut, T - Mnemonic, CTRL-X - Accelerator
    JMenuItem cutItem = new JMenuItem("Cut", KeyEvent.VK_T);
    cutItem.addActionListener(actionListener);
    cutItem.addChangeListener(cListener);
    cutItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, Event.CTRL_MASK));
    cutItem.addMenuKeyListener(menuKeyListener);
    edit.add(cutItem);

    // Edit->Copy, C - Mnemonic, CTRL-C - Accelerator
    JMenuItem copyItem = new JMenuItem("Copy", KeyEvent.VK_C);
    copyItem.addActionListener(actionListener);
    copyItem.addChangeListener(cListener);
    copyItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, Event.CTRL_MASK));
    copyItem.addMenuKeyListener(menuKeyListener);
    copyItem.setEnabled(false);
    edit.add(copyItem);

    // Edit->Paste, P - Mnemonic, CTRL-V - Accelerator, Disabled
    JMenuItem pasteItem = new JMenuItem("Paste", KeyEvent.VK_P);
    pasteItem.addActionListener(actionListener);
    pasteItem.addChangeListener(cListener);
    pasteItem.addMenuKeyListener(menuKeyListener);
    pasteItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, Event.CTRL_MASK));
    pasteItem.setEnabled(false);
    edit.add(pasteItem);

    // Separator
    edit.addSeparator();

    // Edit->Find, F - Mnemonic, F3 - Accelerator
    JMenuItem findItem = new JMenuItem("Find", KeyEvent.VK_F);
    findItem.addActionListener(actionListener);
    findItem.addChangeListener(cListener);
    findItem.addMenuKeyListener(menuKeyListener);
    findItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F3, 0));
    edit.add(findItem);

    // Edit->Options Submenu, O - Mnemonic, at.gif - Icon Image File
    Icon atIcon = new ImageIcon("at.gif");
    Action findAction = new AbstractAction("Options", atIcon) {
        ActionListener actionListener = new MenuActionListener();

        public void actionPerformed(ActionEvent e) {
            actionListener.actionPerformed(e);
        }
    };
    findAction.putValue(Action.MNEMONIC_KEY, new Integer(KeyEvent.VK_O));
    JMenuItem jMenuItem = new JMenuItem(findAction);

    JMenu findOptions = new JMenu(findAction);
    findOptions.addChangeListener(cListener);
    findOptions.addMenuListener(menuListener);
    findOptions.addMenuKeyListener(menuKeyListener);

    // ButtonGrou for radio buttons
    ButtonGroup directionGroup = new ButtonGroup();

    // Edit->Options->Forward, F - Mnemonic, in group
    JRadioButtonMenuItem forward = new JRadioButtonMenuItem("Forward", true);
    forward.addActionListener(actionListener);
    forward.addChangeListener(cListener);
    forward.addMenuKeyListener(menuKeyListener);
    forward.setMnemonic(KeyEvent.VK_F);
    findOptions.add(forward);
    directionGroup.add(forward);

    // Edit->Options->Backward, B - Mnemonic, in group
    JRadioButtonMenuItem backward = new JRadioButtonMenuItem("Backward");
    backward.addActionListener(actionListener);
    backward.addChangeListener(cListener);
    backward.addMenuKeyListener(menuKeyListener);
    backward.setMnemonic(KeyEvent.VK_B);
    findOptions.add(backward);
    directionGroup.add(backward);

    // Separator
    findOptions.addSeparator();

    // Edit->Options->Case Sensitive, C - Mnemonic
    JCheckBoxMenuItem caseItem = new JCheckBoxMenuItem("Case Sensitive");
    caseItem.addActionListener(actionListener);
    caseItem.addChangeListener(cListener);
    caseItem.addMenuKeyListener(menuKeyListener);
    caseItem.setMnemonic(KeyEvent.VK_C);
    findOptions.add(caseItem);
    edit.add(findOptions);

    frame.setJMenuBar(bar);
    //    frame.getContentPane().add(bar, BorderLayout.EAST);
    frame.setSize(350, 250);
    frame.setVisible(true);
}

From source file:Main.java

public static void associaTeclaAtalho(final AbstractButton button, String nomeAcao, String... atalhos) {
    Action action = new AbstractAction(nomeAcao) {
        @Override/*from   w w w  . j a  v  a 2  s.  c o  m*/
        public void actionPerformed(ActionEvent e) {
            for (ActionListener listener : button.getActionListeners()) {
                listener.actionPerformed(e);
            }
        }
    };
    associaTeclaAtalho(button, action, nomeAcao, atalhos);
}

From source file:KeyUtils.java

public static void pressKey(Component component) {
    if (component.getKeyListeners().length > 0) {
        KeyEvent event = new KeyEvent(component, KeyEvent.KEY_PRESSED, 0, 1, 32, (char) 32);
        for (int i = 0; i < component.getKeyListeners().length; i++) {
            KeyListener keyListener = component.getKeyListeners()[i];
            keyListener.keyPressed(event);
        }/*from   w w w. j av a  2s  .  c  o  m*/
    }

    if (JComponent.class.isInstance(component)) {
        KeyStroke keyStroke = KeyStroke.getKeyStroke(32, 1);
        final ActionListener actionForKeyStroke = ((JComponent) component).getActionForKeyStroke(keyStroke);
        if (actionForKeyStroke != null) {
            actionForKeyStroke.actionPerformed(new ActionEvent(component, KeyEvent.KEY_PRESSED, ""));
        }
    }
}

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.jav a  2s .c om*/
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:Main.java

/**
 * Fires a {@link ActionListener}. This is useful for firing 
 * {@link ApplicationAction}s to show blocking dialog. This can be called
 * on and off the EDT./*from w ww. j  a v a  2s  .c o m*/
 *
 * @param listener the listener to fire.
 * @param evt the action event to fire.
 * @param modifiers the modifier keys held down during this action.
 * @see ActionEvent#ActionEvent(Object, int, String, long, int)
 */
public static void fireAction(final ActionListener listener, final ActionEvent evt) {
    if (!SwingUtilities.isEventDispatchThread()) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                fireAction(listener, evt);
            }
        });
        return;
    }
    listener.actionPerformed(evt);
}

From source file:com.moneydance.modules.features.importlist.table.AbstractEditor.java

public final void registerKeyboardShortcut(final JComponent jComponent) {
    Validate.notNull(jComponent, "jComponent must not be null");
    if (this.getKeyStroke() == null) {
        return;/*  w  w  w.  j a  v  a2 s. co m*/
    }

    final Action action = new AbstractAction() {
        private static final long serialVersionUID = 1L;

        @Override
        public void actionPerformed(final ActionEvent actionEvent) {
            ActionListener actionListener = AbstractEditor.this.getActionListener(0);
            actionListener.actionPerformed(actionEvent);
        }
    };

    final String actionMapKey = this.getClass().getName(); // unique
    jComponent.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(this.getKeyStroke(), actionMapKey);
    jComponent.getActionMap().put(actionMapKey, action);
}

From source file:it.polimi.modaclouds.qos.linebenchmark.solver.LineConnectionHandler.java

private synchronized void updateModelEvaluation(String message) {
    message = message.trim().replaceAll(" +", " ");
    String[] tokens = message.split(" ");
    String modelName = tokens[1];
    modelName = modelName.replace("_res.xml", ".xml");
    modelName = Paths.get(modelName).toString();
    String status = null;//from w w w. jav a  2 s .com
    if (tokens.length == 4)
        status = tokens[3];
    else
        status = tokens[2];
    Path modelPath = Paths.get(modelName);
    evaluations.put(modelPath, status);

    StopWatch timer;
    if (status.equals("SUBMITTED")) {
        timer = new StopWatch();
        timers.put(modelPath, timer);
        timer.start();
        logger.debug("Model: " + modelName + " SUBMITTED");
    } else {
        timer = timers.get(modelPath);
        timer.stop();

        EvaluationCompletedEvent evaluationCompleted = new EvaluationCompletedEvent(this, 0, null);
        evaluationCompleted.setEvaluationTime(timer.getTime());
        evaluationCompleted.setSolverName(Main.LINE_SOLVER);
        evaluationCompleted.setModelPath(modelPath.getFileName());
        logger.debug("Model: " + modelName + " " + status);
        for (ActionListener l : listeners)
            l.actionPerformed(evaluationCompleted);
    }
}

From source file:it.polimi.modaclouds.qos.linebenchmark.solver.SolutionEvaluator.java

@Override
public void actionPerformed(ActionEvent e) {
    for (ActionListener l : listeners)
        l.actionPerformed(e);
}

From source file:com.googlecode.commons.swing.component.datetime.MiniDateCalendar.java

protected void fireActionListener() {
    ActionListener[] listeners = listenerList.getListeners(ActionListener.class);
    for (ActionListener l : listeners) {
        l.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, ""));
    }//from  ww  w . j a v  a  2s .c o m
}

From source file:it.polimi.modaclouds.qos.linebenchmark.solver.SolutionEvaluator.java

private void runWithLQNS() {
    StopWatch timer = new StopWatch();
    String solverProgram = "lqns";

    String command = solverProgram + " " + filePath + " -f"; //using the fast option
    logger.info("Launch: " + command);
    //String command = solverProgram+" "+filePath; //without using the fast option
    try {//from ww w.  j a v  a2  s .c  om
        ProcessBuilder pb = new ProcessBuilder(splitToCommandArray(command));

        //start counting
        timer.start();
        Process proc = pb.start();
        readStream(proc.getInputStream(), false);
        readStream(proc.getErrorStream(), true);
        int exitVal = proc.waitFor();
        //stop counting
        timer.stop();
        proc.destroy();

        //evaluation error messages
        if (exitVal == LQNS_RETURN_SUCCESS)
            ;
        else if (exitVal == LQNS_RETURN_MODEL_FAILED_TO_CONVERGE) {
            System.err.println(Main.LQNS_SOLVER + " exited with " + exitVal
                    + ": The model failed to converge. Results are most likely inaccurate. ");
            System.err.println("Analysis Result has been written to: " + resultfilePath);
        } else {
            String message = "";
            if (exitVal == LQNS_RETURN_INVALID_INPUT) {
                message = solverProgram + " exited with " + exitVal + ": Invalid Input.";
            } else if (exitVal == LQNS_RETURN_FATAL_ERROR) {
                message = solverProgram + " exited with " + exitVal + ": Fatal error";
            } else {
                message = solverProgram + " returned an unrecognised exit value " + exitVal
                        + ". Key: 0 on success, 1 if the model failed to meet the convergence criteria, 2 if the input was invalid, 4 if a command line argument was incorrect, 8 for file read/write problems and -1 for fatal errors. If multiple input files are being processed, the exit code is the bit-wise OR of the above conditions.";
            }
            System.err.println(message);
        }
    } catch (IOException | InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    //tell listeners that the evaluation has been performed
    EvaluationCompletedEvent evaluationCompleted = new EvaluationCompletedEvent(this, 0, null);
    evaluationCompleted.setEvaluationTime(timer.getTime());
    evaluationCompleted.setSolverName(solver);
    evaluationCompleted.setModelPath(filePath.getFileName());
    for (ActionListener l : listeners)
        l.actionPerformed(evaluationCompleted);
}