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[]) {
    JFrame frame = new JFrame();
    Container contentPane = frame.getContentPane();
    ActionListener listener = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.out.println("Command: " + e.getActionCommand());
            int modifiers = e.getModifiers();
            System.out.println("\tALT : " + checkMod(modifiers, ActionEvent.ALT_MASK));
            System.out.println("\tCTRL : " + checkMod(modifiers, ActionEvent.CTRL_MASK));
            System.out.println("\tMETA : " + checkMod(modifiers, ActionEvent.META_MASK));
            System.out.println("\tSHIFT: " + checkMod(modifiers, ActionEvent.SHIFT_MASK));
            Object source = e.getSource();
            if (source instanceof JComboBox) {
                JComboBox jb = (JComboBox) source;
                System.out.println("Combo: " + jb.getSelectedItem());
            }/*from  w  ww . j a v a  2 s  .  c  om*/
        }

        private boolean checkMod(int modifiers, int mask) {
            return ((modifiers & mask) == mask);
        }
    };

    String flavors[] = { "Item 1", "Item 2", "Item 3" };
    JComboBox jc = new JComboBox(flavors);
    jc.setMaximumRowCount(4);
    jc.setEditable(true);
    jc.addActionListener(listener);
    contentPane.add(jc, BorderLayout.NORTH);

    JButton b = new JButton("Button!");
    b.addActionListener(listener);
    contentPane.add(b, BorderLayout.CENTER);

    JPanel panel = new JPanel();
    JLabel label = new JLabel("Label 1: ");
    JTextField text = new JTextField("Type your text", 15);
    text.addActionListener(listener);
    label.setDisplayedMnemonic(KeyEvent.VK_1);
    label.setLabelFor(text);
    panel.add(label);
    panel.add(text);
    contentPane.add(panel, BorderLayout.SOUTH);

    frame.pack();
    frame.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);
        }//from www. j a  va2s  .  c om
    };

    Container content = frame.getContentPane();
    content.setLayout(new GridLayout(2, 2));

    JButton button1 = new JButton("Text Button");
    button1.setMnemonic(KeyEvent.VK_B);
    button1.setActionCommand("First");
    button1.addActionListener(actionListener);
    content.add(button1);

    Icon warnIcon = new ImageIcon("Warn.gif");
    JButton button2 = new JButton(warnIcon);
    button2.setActionCommand("Second");
    button2.addActionListener(actionListener);
    content.add(button2);

    JButton button3 = new JButton("Warning", warnIcon);
    button3.setActionCommand("Third");
    button3.addActionListener(actionListener);
    content.add(button3);

    String htmlButton = "<html><sup>HTML</sup> <sub><em>Button</em></sub><br>"
            + "<font color=\"#FF0080\"><u>Multi-line</u></font>";
    JButton button4 = new JButton(htmlButton);
    button4.setActionCommand("Fourth");
    button4.addActionListener(actionListener);
    content.add(button4);

    JRootPane rootPane = frame.getRootPane();
    rootPane.setDefaultButton(button2);

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

From source file:SampleProgress.java

public static void main(String args[]) {
    JFrame frame = new JFrame("ProgressMonitor Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new GridLayout(0, 1));

    JButton startButton = new JButton("Start");
    ActionListener startActionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            Component parent = (Component) actionEvent.getSource();
            monitor = new ProgressMonitor(parent, "Loading Progress", "Getting Started...", 0, 200);
            progress = 0;/*from   w  w w .  j a va2 s .  co m*/
        }
    };
    startButton.addActionListener(startActionListener);
    frame.add(startButton);

    JButton increaseButton = new JButton("Manual Increase");
    ActionListener increaseActionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            if (monitor == null)
                return;
            if (monitor.isCanceled()) {
                System.out.println("Monitor canceled");
            } else {
                progress += 5;
                monitor.setProgress(progress);
                monitor.setNote("Loaded " + progress + " files");
            }
        }
    };
    increaseButton.addActionListener(increaseActionListener);
    frame.add(increaseButton);

    JButton autoIncreaseButton = new JButton("Automatic Increase");
    ActionListener autoIncreaseActionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            if (monitor != null) {
                if (timer == null) {
                    timer = new Timer(250, new ActionListener() {

                        public void actionPerformed(ActionEvent e) {
                            if (monitor == null)
                                return;
                            if (monitor.isCanceled()) {
                                System.out.println("Monitor canceled");
                                timer.stop();
                            } else {
                                progress += 3;
                                monitor.setProgress(progress);
                                monitor.setNote("Loaded " + progress + " files");
                            }
                        }
                    });
                }
                timer.start();
            }
        }
    };
    autoIncreaseButton.addActionListener(autoIncreaseActionListener);
    frame.add(autoIncreaseButton);

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

From source file:Main.java

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

    JFileChooser fileChooser = new JFileChooser(".");
    fileChooser.setControlButtonsAreShown(false);
    frame.add(fileChooser, BorderLayout.CENTER);

    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            JFileChooser theFileChooser = (JFileChooser) actionEvent.getSource();
            String command = actionEvent.getActionCommand();
            if (command.equals(JFileChooser.APPROVE_SELECTION)) {
                File selectedFile = theFileChooser.getSelectedFile();
                System.out.println(selectedFile.getParent());
                System.out.println(selectedFile.getName());
            } else if (command.equals(JFileChooser.CANCEL_SELECTION)) {
                System.out.println(JFileChooser.CANCEL_SELECTION);
            }/* w w  w.  j ava 2  s  . co  m*/
        }
    };
    fileChooser.addActionListener(actionListener);
    fileChooser.removeActionListener(actionListener);
    frame.pack();
    frame.setVisible(true);
}

From source file:MainClass.java

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

    JFileChooser fileChooser = new JFileChooser(".");
    fileChooser.setControlButtonsAreShown(false);
    frame.add(fileChooser, BorderLayout.CENTER);

    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            JFileChooser theFileChooser = (JFileChooser) actionEvent.getSource();
            String command = actionEvent.getActionCommand();
            if (command.equals(JFileChooser.APPROVE_SELECTION)) {
                File selectedFile = theFileChooser.getSelectedFile();
                System.out.println(selectedFile.getParent());
                System.out.println(selectedFile.getName());
            } else if (command.equals(JFileChooser.CANCEL_SELECTION)) {
                System.out.println(JFileChooser.CANCEL_SELECTION);
            }//from   ww w.ja  v a  2  s.  c  o m
        }
    };
    fileChooser.addActionListener(actionListener);
    frame.pack();
    frame.setVisible(true);
}

From source file:ActionTest.java

public static void main(String args[]) {
    JFrame frame = new JFrame();
    Container contentPane = frame.getContentPane();
    ActionListener listener = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.out.println("Command: " + e.getActionCommand());
            System.out.println("Modifiers: ");
            int modifiers = e.getModifiers();
            System.out.println("\tALT : " + checkMod(modifiers, ActionEvent.ALT_MASK));
            System.out.println("\tCTRL : " + checkMod(modifiers, ActionEvent.CTRL_MASK));
            System.out.println("\tMETA : " + checkMod(modifiers, ActionEvent.META_MASK));
            System.out.println("\tSHIFT: " + checkMod(modifiers, ActionEvent.SHIFT_MASK));
            Object source = e.getSource();
            if (source instanceof JComboBox) {
                JComboBox jb = (JComboBox) source;
                System.out.println("Combo: " + jb.getSelectedItem());
            }/* w w w . j  ava 2s. co  m*/
        }

        private boolean checkMod(int modifiers, int mask) {
            return ((modifiers & mask) == mask);
        }
    };

    String flavors[] = { "Item 1", "Item 2", "Item 3" };
    JComboBox jc = new JComboBox(flavors);
    jc.setMaximumRowCount(4);
    jc.setEditable(true);
    jc.addActionListener(listener);
    contentPane.add(jc, BorderLayout.NORTH);

    JButton b = new JButton("Button!");
    b.addActionListener(listener);
    contentPane.add(b, BorderLayout.CENTER);

    JPanel panel = new JPanel();
    JLabel label = new JLabel("Label 1: ");
    JTextField text = new JTextField("Type your text", 15);
    text.addActionListener(listener);
    label.setDisplayedMnemonic(KeyEvent.VK_1);
    label.setLabelFor(text);
    panel.add(label);
    panel.add(text);
    contentPane.add(panel, BorderLayout.SOUTH);

    frame.pack();
    frame.show();
}

From source file:Main.java

public static void main(final String args[]) {
    final String labels[] = { "A", "B", "C", "D", "E" };
    JFrame frame = new JFrame("Selecting JComboBox");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JComboBox comboBox = new JComboBox(labels);
    frame.add(comboBox, BorderLayout.SOUTH);

    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            System.out.println("Command: " + actionEvent.getActionCommand());
            ItemSelectable is = (ItemSelectable) actionEvent.getSource();
            System.out.println(", Selected: " + selectedString(is));
        }/*from   w w  w.  ja va2 s  .  c  om*/
    };
    comboBox.addActionListener(actionListener);
    frame.setSize(400, 200);
    frame.setVisible(true);

}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    JFrame frame = new JFrame("Text Slider");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    final TextSlider ts = new TextSlider();
    ts.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.out.println("Text: " + ts.getText());
        }/*from w w w  . j  a  v  a  2 s .  co  m*/
    });

    frame.add(ts, BorderLayout.NORTH);
    frame.setSize(300, 100);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(final String args[]) {
    final String labels[] = { "A", "B", "C", "D", "E" };
    JFrame frame = new JFrame("Selecting JComboBox");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JComboBox comboBox = new JComboBox(labels);
    frame.add(comboBox, BorderLayout.SOUTH);

    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            System.out.println("Command: " + actionEvent.getActionCommand());
            ItemSelectable is = (ItemSelectable) actionEvent.getSource();
            System.out.println(", Selected: " + selectedString(is));
        }/*from  w ww.  ja  v a 2  s .co m*/
    };
    comboBox.addActionListener(actionListener);
    comboBox.removeActionListener(actionListener);
    frame.setSize(400, 200);
    frame.setVisible(true);

}

From source file:Main.java

public static void main(final String args[]) {
    final String labels[] = { "A", "B", "C", "D", "E" };
    JFrame frame = new JFrame("Selecting JComboBox");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JComboBox comboBox = new JComboBox(labels);
    frame.add(comboBox, BorderLayout.SOUTH);

    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            System.out.println("Command: " + actionEvent.getActionCommand());
            ItemSelectable is = (ItemSelectable) actionEvent.getSource();
            System.out.println(", Selected: " + selectedString(is));
            System.out.println("paramString:" + actionEvent.paramString());
        }//from   w  w w. j  a v a  2 s.c  o m
    };
    comboBox.addActionListener(actionListener);
    frame.setSize(400, 200);
    frame.setVisible(true);

}