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

MainClass(String title) {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    ActionListener a = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.out.println("Counter = " + counter);

            if (++counter > 10) {
                timer.stop();//w  w  w.jav a  2  s .  c  o m
                System.exit(0);
            }
        }
    };

    timer = new Timer(300, a);
    timer.start();

    pack();
    setVisible(true);
}

From source file:Main.java

/**
 * Creates an animation to fade the dialog opacity from 0 to 1, wait at 1
 * and then fade to 0 and dispose./*www.  j a  v  a  2 s.co m*/
 *
 * @param dialog the dialog to display
 * @param delay the delay in ms before starting and between each change
 * @param incrementSize the increment size
 * @param displayTime the time in ms the dialog is fully visible
 */
public static void fadeInAndOut(final JDialog dialog, final int delay, final float incrementSize,
        final int displayTime) {
    final Timer timer = new Timer(delay, null);
    timer.setRepeats(true);
    timer.addActionListener(new ActionListener() {
        private float opacity = 0;
        private boolean displayed = false;

        @Override
        public void actionPerformed(ActionEvent e) {

            if (!displayed) {
                opacity += incrementSize;
                dialog.setOpacity(Math.min(opacity, 1)); // requires java 1.7
                if (opacity >= 1) {
                    timer.setDelay(displayTime);
                    displayed = true;
                }
            } else {
                timer.setDelay(delay);
                opacity -= incrementSize;
                dialog.setOpacity(Math.max(opacity, 0)); // requires java 1.7
                if (opacity < 0) {
                    timer.stop();
                    dialog.dispose();
                }
            }
        }
    });

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

From source file:Main.java

public Main() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    field.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.out.println("Field=" + field.getText());
        }//from  w  w  w.java 2 s  .  c o m
    });

    getContentPane().add(field);
    pack();
    setVisible(true);
}

From source file:Main.java

private static void createTimer() {
    if (timer != null) {
        return;/*from   w  ww .ja  v a2  s.co  m*/
    }
    timer = new Timer(20, new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
        }
    });
    timer.setDelay(20);
    timer.setInitialDelay(20);
    timer.setCoalesce(true);
    timer.setRepeats(true);
    timer.start();
}

From source file:Main.java

public Main() {
    setSize(300, 150);//from   ww  w.  ja  v a 2 s . c  om
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    add(b);
    b.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ev) {
            b.setFont(new Font("Dialog", Font.PLAIN, ++size));
            b.revalidate();
        }
    });
    setVisible(true);
}

From source file:MainClass.java

public void init() {
    System.out.println("parameter=" + getParameter("param"));

    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.out.println("Button was clicked");
        }/*from   ww w.ja va2s  . c om*/
    });

    getContentPane().add(button);
}

From source file:org.jfree.chart.demo.Clock.java

public Clock() {

    timer = new javax.swing.Timer(10, new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (flag == false)
                inkr();//from w w w  . ja v a2s .  c  o m
        }
    });
    timer.start();
}

From source file:Main.java

public Main() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    step.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            int value = bar.getValue() + 7;
            if (value > bar.getMaximum()) {
                value = bar.getMaximum();
            }/*from w  w  w .j a v  a  2s  .  c  om*/
            bar.setValue(value);
        }
    });

    getContentPane().add(bar, BorderLayout.NORTH);
    getContentPane().add(step, BorderLayout.EAST);
    pack();
    setVisible(true);
}

From source file:Main.java

public Main() {
    frame.setSize(600, 600);//from  ww w  .  j av a2  s.  c om
    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    frame.setVisible(true);
    frame.setExtendedState(JFrame.ICONIFIED);
    Timer t = new Timer(3000, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent arg0) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    frame.setExtendedState(JFrame.NORMAL);
                }
            });
        }
    });
    t.setRepeats(false);
    t.start();
}

From source file:Main.java

public Main() throws HeadlessException {
    this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

    JButton button = new JButton("Close");
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.exit(0);//w  w w.  j a  v a 2  s .  c om
        }
    });

    this.setLayout(new FlowLayout(FlowLayout.CENTER));
    this.setSize(new Dimension(100, 100));
    this.getContentPane().add(button);
}