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

public static void main(String args[]) {

    JFrame frame = new JFrame("JColorChooser Sample Popup");
    frame.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, "Change Button Background", initialBackground);
            if (background != null) {
                button.setBackground(background);
            }/*  www.  jav  a 2  s  . c  o m*/
        }
    };
    button.addActionListener(actionListener);
    frame.add(button, BorderLayout.CENTER);

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

From source file:Main.java

public static void main(String[] args) {
    final JFrame parent1 = new JFrame("Parent Frame 1");
    parent1.setLayout(new FlowLayout());
    parent1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JButton button = new JButton("Application modal dialog");
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JDialog dialog = new JDialog(parent1, "Application-Modal Dialog",
                    Dialog.ModalityType.APPLICATION_MODAL);
            dialog.setBounds(200, 150, 200, 150);
            dialog.setVisible(true);/*ww w . ja  va  2s  .  c o m*/
        }
    });
    parent1.add(button);
    parent1.setBounds(100, 100, 200, 150);
    parent1.setVisible(true);

    JFrame parent2 = new JFrame("Parent Frame 2");
    parent2.setBounds(500, 100, 200, 150);
    parent2.setVisible(true);
}

From source file:PopupSample.java

public static void main(String args[]) {
    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            System.out.println("Selected: " + actionEvent.getActionCommand());
        }//w  w w. ja  v a 2  s. c o m
    };

    PopupMenuListener popupMenuListener = new PopupMenuListener() {
        public void popupMenuCanceled(PopupMenuEvent popupMenuEvent) {
            System.out.println("Canceled");
        }

        public void popupMenuWillBecomeInvisible(PopupMenuEvent popupMenuEvent) {
            System.out.println("Becoming Invisible");
        }

        public void popupMenuWillBecomeVisible(PopupMenuEvent popupMenuEvent) {
            System.out.println("Becoming Visible");
        }
    };

    JFrame frame = new JFrame("Popup Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPopupMenu popupMenu = new JPopupMenu();
    popupMenu.addPopupMenuListener(popupMenuListener);

    JMenuItem cutMenuItem = new JMenuItem("Cut");
    cutMenuItem.addActionListener(actionListener);
    popupMenu.add(cutMenuItem);

    JMenuItem copyMenuItem = new JMenuItem("Copy");
    copyMenuItem.addActionListener(actionListener);
    popupMenu.add(copyMenuItem);

    JMenuItem pasteMenuItem = new JMenuItem("Paste");
    pasteMenuItem.addActionListener(actionListener);
    pasteMenuItem.setEnabled(false);
    popupMenu.add(pasteMenuItem);

    popupMenu.addSeparator();

    JMenuItem findMenuItem = new JMenuItem("Find");
    findMenuItem.addActionListener(actionListener);
    popupMenu.add(findMenuItem);

    MouseListener mouseListener = new JPopupMenuShower(popupMenu);
    frame.addMouseListener(mouseListener);

    frame.setSize(350, 250);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    String[] items = new String[] { "One", "Two", "Three", "Four" };
    JList<String> list = new JList<>(items);
    JFrame f = new JFrame();
    f.add(list, BorderLayout.CENTER);
    JButton btn = new JButton("Get selected");
    btn.addActionListener(new ActionListener() {
        @Override// ww w .jav  a  2 s .  c  om
        public void actionPerformed(ActionEvent e) {
            JOptionPane.showConfirmDialog(f, "You Selected : " + list.getSelectedValue(), "Display",
                    JOptionPane.PLAIN_MESSAGE);
        }
    });

    f.add(btn, BorderLayout.SOUTH);
    f.pack();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
}

From source file:OptionPaneSample.java

public static void main(String args[]) {
    JFrame f = new JFrame("JOptionPane Sample");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container content = f.getContentPane();
    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 do you want to go tomorrow?",
                    "JOptionPane Sample", JOptionPane.QUESTION_MESSAGE, null,
                    new String[] { "A", "B", "C", "D", "E" }, "E");
            System.out.println("Response: " + response);
        }//from   ww  w.j a  va  2  s.com
    };
    button.addActionListener(actionListener);
    content.add(button, BorderLayout.CENTER);
    f.setSize(300, 200);
    f.setVisible(true);
}

From source file:DualModal.java

public static void main(String args[]) {
    final JFrame frame1 = new JFrame("Left");
    final JFrame frame2 = new JFrame("Right");
    frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JButton button1 = new JButton("Left");
    JButton button2 = new JButton("Right");
    frame1.add(button1);/*from  www  .j  ava2  s.co m*/
    frame2.add(button2);
    ActionListener listener = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JButton source = (JButton) e.getSource();

            JOptionPane pane = new JOptionPane("New label", JOptionPane.QUESTION_MESSAGE);
            pane.setWantsInput(true);
            JDialog dialog = pane.createDialog(frame2, "Enter Text");
            // dialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
            dialog.setModalityType(Dialog.ModalityType.DOCUMENT_MODAL);
            dialog.setVisible(true);
            String text = (String) pane.getInputValue();

            if (!JOptionPane.UNINITIALIZED_VALUE.equals(text) && text.trim().length() > 0) {
                source.setText(text);
            }
        }
    };
    button1.addActionListener(listener);
    button2.addActionListener(listener);
    frame1.setBounds(100, 100, 200, 200);
    frame1.setVisible(true);
    frame2.setBounds(400, 100, 200, 200);
    frame2.setVisible(true);
}

From source file:Main.java

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

    frame.pack();//from  ww  w. j  a v a 2  s  .  co m
    frame.setVisible(true);

    JButton btnGetSelectedText = new JButton("Get selected text");
    btnGetSelectedText.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println(jTextPane.getSelectedText());
        }
    });
    frame.getContentPane().add(jTextPane, BorderLayout.NORTH);
    frame.getContentPane().add(btnGetSelectedText, BorderLayout.SOUTH);
}

From source file:Main.java

public static void main(String[] args) {
    JProgressBar progressBar = new JProgressBar();
    JButton button = new JButton("Start");
    JFrame f = new JFrame();
    f.setLayout(new FlowLayout());
    f.add(progressBar);/*from   w  w  w  .ja  v a2  s.  c o  m*/
    f.add(button);

    ActionListener updateProBar = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            int val = progressBar.getValue();
            if (val >= 100) {
                //  timer.stop();
                button.setText("End");
                return;
            }
            progressBar.setValue(++val);
        }
    };
    Timer timer = new Timer(50, updateProBar);
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (timer.isRunning()) {
                timer.stop();
                button.setText("Start");
            } else if (button.getText() != "End") {
                timer.start();
                button.setText("Stop");
            }
        }
    });
    f.pack();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
}

From source file:JToggleButtonEvents.java

public static void main(String[] args) {
    JToggleButton jtb = new JToggleButton("Press Me");

    jtb.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ev) {
            System.out.println("ActionEvent!");
        }//from   ww  w . jav a  2s  .c om
    });
    jtb.addItemListener(new ItemListener() {
        public void itemStateChanged(ItemEvent ev) {
            System.out.println("ItemEvent!");
        }
    });
    jtb.addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent ev) {
            System.out.println("ChangeEvent!");
        }
    });
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container c = f.getContentPane();
    c.setLayout(new FlowLayout());
    c.add(jtb);
    f.pack();
    f.setVisible(true);
}

From source file:ChangeLook.java

public static void main(String args[]) {
    final JFrame frame = new JFrame("Change Look");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            String lafClassName = null;
            lafClassName = actionEvent.getActionCommand();
            String finalLafClassName = lafClassName;
            try {
                UIManager.setLookAndFeel(finalLafClassName);
                SwingUtilities.updateComponentTreeUI(frame);
            } catch (Exception exception) {
                JOptionPane.showMessageDialog(frame, "Can't change look and feel", "Invalid PLAF",
                        JOptionPane.ERROR_MESSAGE);
            }//from  ww  w  .  j  a  v  a  2s  . c o m

        }
    };

    UIManager.LookAndFeelInfo looks[] = UIManager.getInstalledLookAndFeels();

    JComboBox comboBox = new JComboBox(new String[] { "a", "b" });

    JPanel panel = new JPanel();

    for (int i = 0, n = looks.length; i < n; i++) {
        JButton button = new JButton(looks[i].getName());
        button.setActionCommand(looks[i].getClassName());
        button.addActionListener(actionListener);
        panel.add(button);
    }

    frame.add(comboBox, BorderLayout.NORTH);
    frame.add(panel, BorderLayout.SOUTH);
    frame.setSize(350, 150);
    frame.setVisible(true);

}