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

public static void main(String[] args) {
    AbstractButton jb = new JButton("Press Me");
    jb.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            System.out.println(((JComponent) arg0.getSource()).getFont());
        }/*from  w ww  .j a  v a2  s  .co m*/
    });

    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(jb);
    f.pack();
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    AbstractButton jb = new JButton("Press Me");
    jb.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            System.out.println(((JComponent) arg0.getSource()).getFont());
        }// w  ww  . ja va2  s. c  o m
    });
    System.out.println(jb.getAction());
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(jb);
    f.pack();
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    AbstractButton jb = new JButton("Press Me");
    jb.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            System.out.println(((JComponent) arg0.getSource()).getFont());
        }/*  w  ww  .  j a  v a 2  s  .co m*/
    });
    ActionListener[] lis = jb.getActionListeners();

    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(jb);
    f.pack();
    f.setVisible(true);
}

From source file:TimerSample.java

public static void main(String args[]) {
    Runnable runner = new Runnable() {
        public void run() {
            Timer.setLogTimers(true);
            ActionListener actionListener = new ActionListener() {
                public void actionPerformed(ActionEvent actionEvent) {
                    System.out.println("Hello World Timer");
                }/* www.ja va 2 s  .c  o  m*/
            };
            Timer timer = new Timer(500, actionListener);
            timer.start();
        }
    };
    EventQueue.invokeLater(runner);
}

From source file:Main.java

public static void main(String[] args) {

    final JTextField tf = new JTextField("press <enter>", 20);
    tf.setHorizontalAlignment(JTextField.RIGHT);

    tf.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            int old = tf.getHorizontalAlignment();
            if (old == JTextField.LEFT)
                tf.setHorizontalAlignment(JTextField.RIGHT);
            if (old == JTextField.RIGHT)
                tf.setHorizontalAlignment(JTextField.CENTER);
            if (old == JTextField.CENTER)
                tf.setHorizontalAlignment(JTextField.LEFT);
        }/*from w ww . j  a  v  a  2 s . com*/
    });

    JFrame frame = new JFrame("JTextFieldExample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(new java.awt.FlowLayout());
    frame.getContentPane().add(tf);
    frame.setSize(275, 75);
    frame.setVisible(true);
    tf.requestFocus();
}

From source file:CreateColorSamplePopup.java

public static void main(String args[]) {
    final JColorChooser colorChooser = new JColorChooser(Color.RED);

    ActionListener okActionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            System.out.println("Color change rejected");
        }// w  w w.  j  av  a 2s.c  om
    };

    // For cancel selection, change button background to red
    ActionListener cancelActionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            System.out.println("cancled");
        }
    };

    final JDialog dialog = JColorChooser.createDialog(null, "Change Button Background", true, colorChooser,
            okActionListener, cancelActionListener);

    dialog.setVisible(true);
}

From source file:ActionButtonSample.java

public static void main(String args[]) {
    JFrame frame = new JFrame("DefaultButton");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            String command = actionEvent.getActionCommand();
            System.out.println("Selected: " + command);
        }// w  ww.  j  a va  2s.  co m
    };
    frame.setLayout(new GridLayout(2, 2, 10, 10));
    JButton button1 = new JButton("Text Button");
    button1.setActionCommand("First");
    button1.addActionListener(actionListener);
    frame.add(button1);
    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    final JLabel label = new JLabel();
    int timerDelay = 1000;
    new Timer(timerDelay, new ActionListener() {
        int timeLeft = 5;

        @Override//from w w  w .  ja  v a 2 s  .  c  o  m
        public void actionPerformed(ActionEvent e) {
            if (timeLeft > 0) {
                label.setText("Closing in " + timeLeft + " seconds");
                timeLeft--;
            } else {
                ((Timer) e.getSource()).stop();
                Window win = SwingUtilities.getWindowAncestor(label);
                win.setVisible(false);
            }
        }
    }) {
        {
            setInitialDelay(0);
        }
    }.start();

    JOptionPane.showMessageDialog(null, label);
}

From source file:Main.java

public static void main(String[] argv) {
    final JColorChooser chooser = new JColorChooser();
    ActionListener okListener = new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            Color newColor = chooser.getColor();
        }/*from www. j  a  v  a  2  s .  c o m*/
    };

    ActionListener cancelListener = new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            Color newColor = chooser.getColor();
        }
    };

    boolean modal = false;

    JDialog dialog = JColorChooser.createDialog(null, "Dialog Title", modal, chooser, okListener,
            cancelListener);

    dialog.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent evt) {
            Color newColor = chooser.getColor();
        }
    });
}

From source file:MultiClickThreshholdDemo.java

public static void main(String[] a) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    AbstractButton bn = new JButton();
    bn.setMultiClickThreshhold(1000);//from w  ww.  ja v  a  2s .  co  m

    bn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.out.println("action");
        }
    });

    frame.add(bn);

    frame.setSize(300, 200);
    frame.setVisible(true);
}