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

public static void main(String args[]) {
    JFrame frame = new JFrame("Iconizing CheckBox");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JCheckBox aCheckBox4 = new JCheckBox("Stuffed Crust");

    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            AbstractButton abstractButton = (AbstractButton) actionEvent.getSource();
            boolean selected = abstractButton.getModel().isSelected();
            System.out.println(selected);
            // abstractButton.setText(newLabel);
        }//from  w ww .j av a  2 s  . c  o m
    };
    aCheckBox4.addActionListener(actionListener);
    frame.add(aCheckBox4);
    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:MainClass.java

public static void main(String args[]) {
    JFrame f = new JFrame("JOptionPane Sample");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JButton button = new JButton("Ask");
    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            Component source = (Component) actionEvent.getSource();
            Object response = JOptionPane.showInputDialog(source, "Choose One?", "JOptionPane Sample",
                    JOptionPane.QUESTION_MESSAGE, null, new String[] { "A", "B", "C" }, "B");
            System.out.println("Response: " + response);
        }//from w ww.j  av a2s.  c  o m
    };
    button.addActionListener(actionListener);
    f.add(button, BorderLayout.CENTER);
    f.setSize(300, 200);
    f.setVisible(true);
}

From source file:ActionListenerTest3.java

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

    JButton button = new JButton("Select File");
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JFileChooser fileChooser = new JFileChooser();
            int returnVal = fileChooser.showOpenDialog(null);
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                System.out.println(fileChooser.getSelectedFile().getName());
            }/*from  w  w w.j  a  v  a 2 s  .  co m*/
        }
    });

    frame.add(button);
    frame.pack();
    frame.setVisible(true);
}

From source file:EventObject.java

public static void main(String[] args) {
    JFrame f = new JFrame();
    JButton ok = new JButton("Ok");

    ok.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            Calendar cal = Calendar.getInstance();
            cal.setTimeInMillis(event.getWhen());
            Locale locale = Locale.getDefault();
            String s = DateFormat.getTimeInstance(DateFormat.SHORT, locale).format(new Date());

            if (event.getID() == ActionEvent.ACTION_PERFORMED)
                System.out.println(" Event Id: ACTION_PERFORMED");

            System.out.println(" Time: " + s);

            String source = event.getSource().getClass().getName();
            System.out.println(" Source: " + source);

            int mod = event.getModifiers();
            if ((mod & ActionEvent.ALT_MASK) > 0)
                System.out.println("Alt ");

            if ((mod & ActionEvent.SHIFT_MASK) > 0)
                System.out.println("Shift ");

            if ((mod & ActionEvent.META_MASK) > 0)
                System.out.println("Meta ");

            if ((mod & ActionEvent.CTRL_MASK) > 0)
                System.out.println("Ctrl ");

        }//from   ww w .j a v  a2 s  .  c  om
    });

    f.add(ok);

    f.setSize(420, 250);
    f.setLocationRelativeTo(null);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
}

From source file:CardLayoutTest.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Card Layout");
    final Container contentPane = frame.getContentPane();
    final CardLayout layout = new CardLayout();
    contentPane.setLayout(layout);/* w  ww  .j  a  va2 s . c o  m*/
    ActionListener listener = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            layout.next(contentPane);
        }
    };
    for (int i = 0; i < 5; i++) {
        String label = "Card " + i;
        JButton button = new JButton(label);
        contentPane.add(button, label);
        button.addActionListener(listener);
    }
    frame.setSize(300, 200);
    frame.show();
}

From source file:Main.java

public static void main(String[] args) {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.add(pb);/* w  ww. j a  v  a 2s  .  c  om*/
    f.pack();
    f.setVisible(true);

    Timer timer = new Timer(50, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            progress += 1;
            if (progress >= 100) {
                progress = 100;
                ((Timer) e.getSource()).stop();
            }
            pb.setValue(progress);
        }
    });
    timer.start();
}

From source file:Main.java

public static void main(String args[]) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JToggleButton toggleButton = new JToggleButton("Toggle Button");
    // Define ActionListener
    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            AbstractButton abstractButton = (AbstractButton) actionEvent.getSource();
            boolean selected = abstractButton.getModel().isSelected();
            System.out.println("Action - selected=" + selected + "\n");
        }//  w  w w.ja va2s. c  om
    };
    // Attach Listeners
    toggleButton.addActionListener(actionListener);
    frame.add(toggleButton, BorderLayout.NORTH);
    frame.setSize(300, 125);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String args[]) {
    JFrame f = new JFrame("JColorChooser Sample");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final JButton button = new JButton("Pick to Change Background");

    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            Color initialBackground = button.getBackground();
            Color background = JColorChooser.showDialog(null, "JColorChooser Sample", initialBackground);
            if (background != null) {
                button.setBackground(background);
            }/*  www .j  ava  2s .  c o m*/
        }
    };
    button.addActionListener(actionListener);
    f.add(button, BorderLayout.CENTER);
    f.setSize(300, 200);
    f.setVisible(true);
}

From source file:ActiveSample.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Active Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    UIManager.put(LABEL_FACTORY, new ActiveLabel());

    final JPanel panel = new JPanel();

    JButton button = new JButton("Get");
    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            JLabel label = (JLabel) UIManager.get(LABEL_FACTORY);
            panel.add(label);/* w  w w  .j ava  2  s .  c o m*/
            panel.revalidate();
        }
    };
    button.addActionListener(actionListener);

    frame.add(panel, BorderLayout.CENTER);
    frame.add(button, BorderLayout.SOUTH);
    frame.setSize(200, 200);
    frame.setVisible(true);
}

From source file:SamplePopup.java

public static void main(String args[]) {

    JFrame frame = new JFrame("Sample Popup");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JButton button = new JButton("Ask");
    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            Component source = (Component) actionEvent.getSource();
            Object response = JOptionPane.showInputDialog(source, "Where would you like to go to lunch?",
                    "Select a Destination", JOptionPane.QUESTION_MESSAGE, null,
                    new String[] { "A", "B", "C", "D", "E" }, "B");
            System.out.println("Response: " + response);
        }/*from ww  w .  j  a  va2s.c  o m*/
    };
    button.addActionListener(actionListener);
    Container contentPane = frame.getContentPane();
    contentPane.add(button, BorderLayout.SOUTH);
    frame.setSize(300, 200);
    frame.setVisible(true);
}