Example usage for java.awt.event ActionListener ActionListener

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

Introduction

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

Prototype

ActionListener

Source Link

Usage

From source file:Main.java

private static void triggerBringToFront(final JFrame f, final int delayMS) {
    Timer timer = new Timer(delayMS, new ActionListener() {
        @Override//  w ww  .ja  v  a2  s .c om
        public void actionPerformed(ActionEvent e) {
            // This will only cause the task bar entry
            // for this frame to blink
            // f.toFront();

            // This will bring the window to the front,
            // but not make it the "active" one
            // f.setAlwaysOnTop(true);
            // f.setAlwaysOnTop(false);

            if (!f.isActive()) {
                f.setState(JFrame.ICONIFIED);
                f.setState(JFrame.NORMAL);
            }
        }
    });
    timer.setRepeats(false);
    timer.start();
}

From source file:Main.java

/**
 * Adds to the dialog a key listener that makes the dialog invisible.
 * //w w  w  . j  a  va2s  .  co  m
 * @param dialog
 */
public static void addEscapeKeyCloseAction(final JDialog dialog) {
    dialog.getRootPane().registerKeyboardAction(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            dialog.setVisible(false);
        }
    }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_IN_FOCUSED_WINDOW);
}

From source file:Main.java

public static JPopupMenu createStdEditPopupMenu(final JTextComponent[] fields) {
    final JPopupMenu popupMenu = new JPopupMenu();

    /* text fields popup menu: "Cut" */
    final JMenuItem cutMenuItem = new JMenuItem("Cut", 't');
    cutMenuItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            final Component c = popupMenu.getInvoker();

            if (c instanceof JTextComponent) {
                ((JTextComponent) c).cut();
            }/*from  w w  w  . j a  v a2  s  . c om*/
        }
    });
    popupMenu.add(cutMenuItem);

    /* text fields popup menu: "Copy" */
    final JMenuItem copyMenuItem = new JMenuItem("Copy", 'C');
    copyMenuItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            final Component c = popupMenu.getInvoker();

            if (c instanceof JTextComponent) {
                ((JTextComponent) c).copy();
            }
        }
    });
    popupMenu.add(copyMenuItem);

    /* text fields popup menu: "Paste" */
    final JMenuItem pasteMenuItem = new JMenuItem("Paste", 'P');
    pasteMenuItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            final Component c = popupMenu.getInvoker();

            if (c instanceof JTextComponent) {
                ((JTextComponent) c).paste();
            }
        }
    });
    popupMenu.add(pasteMenuItem);
    popupMenu.addSeparator();

    /* text fields popup menu: "Select All" */
    final JMenuItem selectAllMenuItem = new JMenuItem("Select All", 'A');
    selectAllMenuItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            final Component c = popupMenu.getInvoker();

            if (c instanceof JTextComponent) {
                ((JTextComponent) c).selectAll();
            }
        }
    });
    popupMenu.add(selectAllMenuItem);

    /* add mouse listeners to the specified fields */
    for (final JTextComponent f : fields) {
        f.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                processMouseEvent(e);
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                processMouseEvent(e);
            }

            private void processMouseEvent(MouseEvent e) {
                if (e.isPopupTrigger()) {
                    popupMenu.show(e.getComponent(), e.getX(), e.getY());
                    popupMenu.setInvoker(f);
                }
            }
        });
    }
    return popupMenu;
}

From source file:Main.java

public static ActionListener getActionListenerForRunnable(final Runnable runnable) {
    return new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            runnable.run();//from   ww  w . j  a  v  a  2 s  . c o  m
        }
    };
}

From source file:AddingButtonWithActionListener.java

public static JButton getButton(final JOptionPane optionPane, String text, Icon icon) {
    final JButton button = new JButton(text, icon);
    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            // Return current text label, instead of argument to method
            optionPane.setValue(button.getText());
            System.out.println(button.getText());
        }//from   w w  w. java 2s . c om
    };
    button.addActionListener(actionListener);
    return button;
}

From source file:Main.java

/**
 * Creates an animation to fade the dialog opacity from 0 to 1.
 *
 * @param dialog the dialog to fade in//from   ww w.ja v a  2s . c  om
 * @param delay the delay in ms before starting and between each change
 * @param incrementSize the increment size
 */
public static void fadeIn(final JDialog dialog, int delay, final float incrementSize) {
    final Timer timer = new Timer(delay, null);
    timer.setRepeats(true);
    timer.addActionListener(new ActionListener() {
        private float opacity = 0;

        @Override
        public void actionPerformed(ActionEvent e) {
            opacity += incrementSize;
            dialog.setOpacity(Math.min(opacity, 1)); // requires java 1.7
            if (opacity >= 1) {
                timer.stop();
            }
        }
    });

    dialog.setOpacity(0); // requires java 1.7
    timer.start();
    dialog.setVisible(true);
}

From source file:Main.java

/**
 * Creates an animation to fade the dialog opacity from 1 to 0, and then
 * dispose./*  ww w  .jav  a  2  s  . c o  m*/
 *
 * @param dialog the dialog to fade out
 * @param delay the delay in ms before starting and between each change
 * @param incrementSize the increment size
 */
public static void fadeOut(final JDialog dialog, int delay, final float incrementSize) {
    final Timer timer = new Timer(delay, null);
    timer.setRepeats(true);
    timer.addActionListener(new ActionListener() {
        private float opacity = 1;

        @Override
        public void actionPerformed(ActionEvent e) {
            opacity -= incrementSize;
            dialog.setOpacity(Math.max(opacity, 0)); // requires java 1.7
            if (opacity < 0) {
                timer.stop();
                dialog.dispose();
            }
        }
    });

    dialog.setOpacity(1); // requires java 1.7
    timer.start();
}

From source file:MainClass.java

public static JPanel demo1() {
    JPanel pan = new JPanel(new BorderLayout());
    pan.setBorder(new TitledBorder("change format midstream"));

    MaskFormatter lowercase = null;
    try {//from w  w  w.j  ava2  s  .c om
        lowercase = new MaskFormatter("LLLL");
    } catch (ParseException pe) {
    }
    final JFormattedTextField field = new JFormattedTextField(lowercase);
    field.setValue("lower case");
    pan.add(field, BorderLayout.CENTER);

    final JButton change = new JButton("change format");
    JPanel changePanel = new JPanel();
    changePanel.add(change);
    pan.add(changePanel, BorderLayout.SOUTH);

    change.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            try {
                field.commitEdit();
                MaskFormatter uppercase = new MaskFormatter("UUUU");
                DefaultFormatterFactory factory = new DefaultFormatterFactory(uppercase);
                field.setFormatterFactory(factory);
                change.setEnabled(false);
            } catch (ParseException pe) {
            }
        }
    });

    return pan;
}

From source file:Main.java

/**
 * Displays a specified <code>JFileChooser</code> in a JInternalFrame. <br />
 * The JInternalFrame will close when the dialog is closed. <br />
 * /*from  w  w w  .  ja  v a2 s  .  c o m*/
 * @param desktop the JDesktopPane on which to display the JFileChooser
 * @param ch the JFileChooser to display
 */
public static void showInternalFileChooser(JDesktopPane desktop, final JFileChooser ch) {
    final JInternalFrame frm = new JInternalFrame(ch.getDialogTitle());
    frm.setClosable(true);
    frm.setResizable(true);
    frm.setLayout(new BorderLayout());
    frm.add(ch, BorderLayout.CENTER);
    frm.setVisible(true);

    frm.pack();

    Dimension size = frm.getSize();
    frm.setLocation(desktop.getWidth() / 2 - size.width / 2, desktop.getHeight() / 2 - size.height / 2);
    desktop.add(frm, 0);

    ch.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent ae) {
            frm.dispose();
            ch.removeActionListener(this);
        }
    });

    try {
        frm.setSelected(true);
    } catch (java.beans.PropertyVetoException e) {
    }
}

From source file:Main.java

/** 
 * Runs the supplied class after a certain period of time, the thread
 * will be executed in the EDT. // w  ww .j  ava2  s  . com
 *
 * @param execute The runnable object whose method will be called after the
 * specified delay
 * @param after The delay in ms before the event will be called
 */
public static Timer invokeAfter(final Runnable execute, int after) {
    Timer timer = new Timer(after, new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            execute.run();
        }
    });
    timer.setRepeats(false);
    timer.start();
    return timer;
}