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:Test.java

public Test() {
    this.setBounds(100, 100, 200, 100);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel panel = new JPanel();
    panel.setBorder(BorderFactory.createLoweredSoftBevelBorder());

    this.setLayout(new FlowLayout());

    JButton exitButton = new JButton("Exit");
    panel.add(exitButton);/*  w w w . j a va2  s.c o m*/
    this.add(panel);

    exitButton.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent event) {
            System.exit(0);
        }
    });
}

From source file:Main.java

public TestPane() {
    setLayout(new GridBagLayout());
    label = new JLabel();
    add(label);/*w  ww. ja va  2 s  .  c  om*/
    Timer timer = new Timer(1000, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            String labelText = label.getText();
            labelText += text.charAt(charIndex);
            label.setText(labelText);
            charIndex++;
            if (charIndex >= text.length()) {
                ((Timer) e.getSource()).stop();
            }
        }
    });
    timer.start();
}

From source file:Test.java

public Test() {
    this.setBounds(100, 100, 200, 100);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel panel = new JPanel();
    panel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1, true));

    this.setLayout(new FlowLayout());

    JButton exitButton = new JButton("Exit");
    panel.add(exitButton);/* w  ww  . java 2s  .  co  m*/
    this.add(panel);

    exitButton.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent event) {
            System.exit(0);
        }
    });
}

From source file:MyDialog.java

public MyDialog(JFrame parent) {
    super(parent, "My dialog", true);
    Container cp = getContentPane();
    cp.setLayout(new FlowLayout());
    cp.add(new JLabel("Here is my dialog"));
    JButton ok = new JButton("OK");
    ok.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            dispose(); // Closes the dialog
        }//  w w w.  ja v  a 2  s  .com
    });
    cp.add(ok);
    setSize(150, 125);
}

From source file:Main.java

public Main() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(300, 200);//  w  w w .ja va 2s.  com
    blocker = new JDialog(this, true);
    blocker.setLayout(new FlowLayout());
    blocker.add(new JLabel("I'm blocking EDT!"));
    JProgressBar progress = new JProgressBar();
    progress.setIndeterminate(true);
    blocker.add(progress);
    blocker.pack();

    timer = new Timer(3000, new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            doSomeWork();
        }
    });
    timer.setRepeats(false);
    timer.start();
}

From source file:MainClass.java

public MainClass() {
    setLayout(new BorderLayout());
    model = new DefaultListModel();
    list = new JList(model);
    JScrollPane pane = new JScrollPane(list);
    JButton addButton = new JButton("Add Element");
    JButton removeButton = new JButton("Remove Element");
    for (int i = 0; i < 15; i++)
        model.addElement("Element " + i);

    addButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            model.addElement("Element " + counter);
            counter++;//from   w ww .j  a  v  a2  s.  co m
        }
    });
    removeButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (model.getSize() > 0)
                model.removeElementAt(0);
        }
    });

    add(pane, BorderLayout.NORTH);
    add(addButton, BorderLayout.WEST);
    add(removeButton, BorderLayout.EAST);
}

From source file:Main.java

/**
 * Set the button to have simplified UI.
 * /*from w  ww.  j a  v a 2s. com*/
 * @param button
 *            button to be modified
 * @param <T>
 *            type of button
 * @return button
 */
public static <T extends AbstractButton> T decoratedToSimpleButton(final T button) {

    button.setForeground(Color.BLACK);
    button.setBackground(Color.LIGHT_GRAY);
    button.setBorderPainted(true);
    button.setFocusPainted(true);
    button.setContentAreaFilled(false);
    button.setOpaque(true);

    if (button instanceof JToggleButton) {
        ((JToggleButton) button).addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (button.isSelected()) {
                    button.setBackground(Color.WHITE);
                }
            }
        });
    }
    button.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseEntered(MouseEvent e) {
            super.mouseEntered(e);
            button.setBackground(Color.WHITE);
        }

        @Override
        public void mouseExited(MouseEvent e) {
            super.mouseExited(e);
            if (!button.isSelected()) {
                button.setBackground(Color.LIGHT_GRAY);
            }
        }
    });

    button.addFocusListener(new FocusAdapter() {
        @Override
        public void focusLost(FocusEvent e) {
            if (!button.isSelected()) {
                button.setBackground(Color.LIGHT_GRAY);
            }
        }
    });
    Border line = new LineBorder(Color.BLACK);
    Border margin = new EmptyBorder(5, 15, 5, 15);
    Border compound = new CompoundBorder(line, margin);
    button.setBorder(compound);
    return button;
}

From source file:SecretTest.java

public SecretTest() {
    super("EventListenerList Demo");
    setSize(200, 100);/*from   w  w w . j  a v  a  2 s. c o m*/
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    SecretLabel secret = new SecretLabel("Try Clicking Me");
    final JLabel reporter = new JLabel("Event reports will show here...");
    secret.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            reporter.setText("Got it: " + ae.getActionCommand());
        }
    });
    getContentPane().add(secret, BorderLayout.NORTH);
    getContentPane().add(reporter, BorderLayout.SOUTH);
}

From source file:Main.java

public BlinkPane() {
    label = new JLabel("Hello");
    setLayout(new GridBagLayout());

    add(label);/*from   w  w w  .ja  v a2  s. c  o m*/
    Timer timer = new Timer(1000, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent ae) {
            on = !on;
            repaint();
        }
    });
    timer.setRepeats(true);
    timer.setCoalesce(true);
    timer.start();
}

From source file:Main.java

public Main() {
    menuBar = new JMenuBar();
    JMenu justifyMenu = new JMenu("Justify");
    ActionListener actionPrinter = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try {
                pane.getStyledDocument().insertString(0, "Action [" + e.getActionCommand() + "] performed!\n",
                        null);/*from ww  w  .ja v  a  2  s .  c  o  m*/
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    };
    JRadioButtonMenuItem leftJustify = new JRadioButtonMenuItem("Left", new ImageIcon("1.gif"));
    leftJustify.setHorizontalTextPosition(JMenuItem.RIGHT);
    leftJustify
            .setAccelerator(KeyStroke.getKeyStroke('L', Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
    leftJustify.addActionListener(actionPrinter);
    JRadioButtonMenuItem rightJustify = new JRadioButtonMenuItem("Right", new ImageIcon("2.gif"));
    rightJustify.setHorizontalTextPosition(JMenuItem.RIGHT);
    rightJustify
            .setAccelerator(KeyStroke.getKeyStroke('R', Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
    rightJustify.addActionListener(actionPrinter);
    JRadioButtonMenuItem centerJustify = new JRadioButtonMenuItem("Center", new ImageIcon("3.gif"));
    centerJustify.setHorizontalTextPosition(JMenuItem.RIGHT);
    centerJustify
            .setAccelerator(KeyStroke.getKeyStroke('M', Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
    centerJustify.addActionListener(actionPrinter);
    JRadioButtonMenuItem fullJustify = new JRadioButtonMenuItem("Full", new ImageIcon("4.gif"));
    fullJustify.setHorizontalTextPosition(JMenuItem.RIGHT);
    fullJustify
            .setAccelerator(KeyStroke.getKeyStroke('F', Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
    fullJustify.addActionListener(actionPrinter);

    ButtonGroup group = new ButtonGroup();
    group.add(leftJustify);
    group.add(rightJustify);
    group.add(centerJustify);
    group.add(fullJustify);

    justifyMenu.add(leftJustify);
    justifyMenu.add(rightJustify);
    justifyMenu.add(centerJustify);
    justifyMenu.add(fullJustify);

    menuBar.add(justifyMenu);
    menuBar.setBorder(new BevelBorder(BevelBorder.RAISED));
}