Example usage for java.awt.event ActionEvent getSource

List of usage examples for java.awt.event ActionEvent getSource

Introduction

In this page you can find the example usage for java.awt.event ActionEvent getSource.

Prototype

public Object getSource() 

Source Link

Document

The object on which the Event initially occurred.

Usage

From source file:Main.java

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

    // File Menu, F - Mnemonic
    JMenu fileMenu = new JMenu("File");
    menuBar.add(fileMenu);/*from   w ww. j  av  a  2  s  .c  o m*/

    // File->New, N - Mnemonic
    JMenuItem newMenuItem = new JMenuItem("New", KeyEvent.VK_N);
    newMenuItem.setArmed(false);
    fileMenu.add(newMenuItem);

    JCheckBoxMenuItem caseMenuItem = new JCheckBoxMenuItem("Case Sensitive");
    caseMenuItem.setMnemonic(KeyEvent.VK_C);
    fileMenu.add(caseMenuItem);

    ActionListener aListener = new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            AbstractButton aButton = (AbstractButton) event.getSource();
            boolean selected = aButton.getModel().isSelected();
            String newLabel;
            if (selected) {
                newLabel = "A";
            } else {
                newLabel = "B";
            }
            aButton.setText(newLabel);
        }
    };

    caseMenuItem.addActionListener(aListener);
    frame.setJMenuBar(menuBar);
    frame.setSize(350, 250);
    frame.setVisible(true);
}

From source file:Main.java

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

    // File Menu, F - Mnemonic
    JMenu fileMenu = new JMenu("File");
    menuBar.add(fileMenu);/*from   w w w .  j  a  v a 2  s  .  co m*/

    // File->New, N - Mnemonic
    JMenuItem newMenuItem = new JMenuItem("New", KeyEvent.VK_N);
    newMenuItem.setEnabled(false);
    fileMenu.add(newMenuItem);

    JCheckBoxMenuItem caseMenuItem = new JCheckBoxMenuItem("Case Sensitive");
    caseMenuItem.setMnemonic(KeyEvent.VK_C);
    fileMenu.add(caseMenuItem);

    ActionListener aListener = new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            AbstractButton aButton = (AbstractButton) event.getSource();
            boolean selected = aButton.getModel().isSelected();
            String newLabel;
            if (selected) {
                newLabel = "A";
            } else {
                newLabel = "B";
            }
            aButton.setText(newLabel);
        }
    };

    caseMenuItem.addActionListener(aListener);
    frame.setJMenuBar(menuBar);
    frame.setSize(350, 250);
    frame.setVisible(true);
}

From source file:Main.java

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

    // File Menu, F - Mnemonic
    JMenu fileMenu = new JMenu("File");
    menuBar.add(fileMenu);/*from w ww.j  a  v  a2 s.  com*/

    // File->New, N - Mnemonic
    JMenuItem newMenuItem = new JMenuItem("New", KeyEvent.VK_N);
    newMenuItem.setAccelerator(KeyStroke.getKeyStroke("N"));
    fileMenu.add(newMenuItem);

    JCheckBoxMenuItem caseMenuItem = new JCheckBoxMenuItem("Case Sensitive");
    caseMenuItem.setMnemonic(KeyEvent.VK_C);
    fileMenu.add(caseMenuItem);

    ActionListener aListener = new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            AbstractButton aButton = (AbstractButton) event.getSource();
            boolean selected = aButton.getModel().isSelected();
            String newLabel;
            if (selected) {
                newLabel = "A";
            } else {
                newLabel = "B";
            }
            aButton.setText(newLabel);
        }
    };

    caseMenuItem.addActionListener(aListener);
    frame.setJMenuBar(menuBar);
    frame.setSize(350, 250);
    frame.setVisible(true);
}

From source file:JCheckBoxMenuItemActionListener.java

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

    // File Menu, F - Mnemonic
    JMenu fileMenu = new JMenu("File");
    fileMenu.setMnemonic(KeyEvent.VK_F);
    menuBar.add(fileMenu);//from  w  w  w  .  j  a  v  a 2  s . c om

    // File->New, N - Mnemonic
    JMenuItem newMenuItem = new JMenuItem("New", KeyEvent.VK_N);
    fileMenu.add(newMenuItem);

    JCheckBoxMenuItem caseMenuItem = new JCheckBoxMenuItem("Case Sensitive");
    caseMenuItem.setMnemonic(KeyEvent.VK_C);
    fileMenu.add(caseMenuItem);

    ActionListener aListener = new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            AbstractButton aButton = (AbstractButton) event.getSource();
            boolean selected = aButton.getModel().isSelected();
            String newLabel;
            Icon newIcon;
            if (selected) {
                newLabel = "A";
            } else {
                newLabel = "B";
            }
            aButton.setText(newLabel);
        }
    };

    caseMenuItem.addActionListener(aListener);
    frame.setJMenuBar(menuBar);
    frame.setSize(350, 250);
    frame.setVisible(true);
}

From source file:JCheckBoxMenuItemActionListener.java

public static void main(final String args[]) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JMenuBar menuBar = new JMenuBar();

    // File Menu, F - Mnemonic
    JMenu fileMenu = new JMenu("File");
    fileMenu.setMnemonic(KeyEvent.VK_F);
    menuBar.add(fileMenu);//  ww w . j  av a  2 s .  com

    // File->New, N - Mnemonic
    JMenuItem newMenuItem = new JMenuItem("New", KeyEvent.VK_N);
    fileMenu.add(newMenuItem);

    JCheckBoxMenuItem caseMenuItem = new JCheckBoxMenuItem("Case Sensitive");
    caseMenuItem.setMnemonic(KeyEvent.VK_C);
    fileMenu.add(caseMenuItem);

    ActionListener aListener = new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            AbstractButton aButton = (AbstractButton) event.getSource();
            boolean selected = aButton.getModel().isSelected();
            String newLabel;
            Icon newIcon;
            if (selected) {
                newLabel = "A";
            } else {
                newLabel = "B";
            }
            aButton.setText(newLabel);
        }
    };

    caseMenuItem.addActionListener(aListener);
    frame.setJMenuBar(menuBar);
    frame.setSize(350, 250);
    frame.setVisible(true);
}

From source file:FileSamplePanel.java

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

    final JLabel directoryLabel = new JLabel(" ");
    directoryLabel.setFont(new Font("Serif", Font.BOLD | Font.ITALIC, 36));
    frame.add(directoryLabel, BorderLayout.NORTH);

    final JLabel filenameLabel = new JLabel(" ");
    filenameLabel.setFont(new Font("Serif", Font.BOLD | Font.ITALIC, 36));
    frame.add(filenameLabel, BorderLayout.SOUTH);

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

    //  Create ActionListener
    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();
                directoryLabel.setText(selectedFile.getParent());
                filenameLabel.setText(selectedFile.getName());
            } else if (command.equals(JFileChooser.CANCEL_SELECTION)) {
                directoryLabel.setText(" ");
                filenameLabel.setText(" ");
            }//from  w ww .  j  a v  a  2 s .  c o m
        }
    };
    fileChooser.addActionListener(actionListener);

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

From source file:MainClass.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) {
            Object source = actionEvent.getSource();
            String lafClassName = null;
            if (source instanceof JComboBox) {
                JComboBox comboBox = (JComboBox) source;
                lafClassName = (String) comboBox.getSelectedItem();
            } else if (source instanceof JButton) {
                lafClassName = actionEvent.getActionCommand();
            }/*from www.  j av a  2  s  . c  o m*/
            if (lafClassName != null) {
                final 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);
                }
            }
        }
    };

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

    DefaultComboBoxModel model = new DefaultComboBoxModel();
    JComboBox comboBox = new JComboBox(model);

    JPanel panel = new JPanel();

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

    comboBox.addActionListener(actionListener);

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

From source file:Main.java

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

    JPanel panel = new JPanel(new GridLayout(0, 1));

    ButtonGroup group = new ButtonGroup();
    JRadioButton aRadioButton = new JRadioButton("A");
    JRadioButton bRadioButton = new JRadioButton("B");

    ActionListener crustActionListener = new ActionListener() {
        String lastSelected;/*from   w ww  . ja  va 2  s  . com*/

        public void actionPerformed(ActionEvent actionEvent) {
            AbstractButton aButton = (AbstractButton) actionEvent.getSource();
            String label = aButton.getText();
            String msgStart;
            if (label.equals(lastSelected)) {
                msgStart = "Reselected: ";
            } else {
                msgStart = "Selected: ";
            }
            lastSelected = label;
            System.out.println(msgStart + label);
        }
    };

    panel.add(aRadioButton);
    group.add(aRadioButton);
    panel.add(bRadioButton);
    group.add(bRadioButton);

    aRadioButton.addActionListener(crustActionListener);
    bRadioButton.addActionListener(crustActionListener);

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

From source file:Main.java

public static void main(String[] args) {
    JProgressBar pb = new JProgressBar();
    JTextArea ta = new JTextArea(10, 20);

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new BorderLayout());
    frame.add(new JScrollPane(ta));
    frame.add(pb, BorderLayout.SOUTH);
    frame.pack();//  w  w  w . ja  v  a  2s  .c  o  m
    frame.setVisible(true);

    Timer timer = new Timer(250, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            index++;
            if (index >= 100) {
                ((Timer) (e.getSource())).stop();
            }
            ta.append("Line " + index + "\n");
            pb.setValue(index);
        }
    });
    timer.setRepeats(true);
    timer.setCoalesce(true);
    timer.start();
}

From source file:Main.java

public static void main(String args[]) {
    String ACTION_KEY = "The Action";

    JFrame frame = new JFrame("KeyStroke Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JButton buttonA = new JButton("FOCUSED (control alt 7)");
    JButton buttonB = new JButton("FOCUS/RELEASE (VK_ENTER)");
    JButton buttonC = new JButton("ANCESTOR  (VK_F4+SHIFT_MASK)");
    JButton buttonD = new JButton("WINDOW (' ')");

    Action actionListener = new AbstractAction() {
        public void actionPerformed(ActionEvent actionEvent) {
            JButton source = (JButton) actionEvent.getSource();
            System.out.println("Activated: " + source.getText());
        }//from  ww w  . j a v  a 2s  . com
    };

    KeyStroke controlAlt7 = KeyStroke.getKeyStroke("control alt 7");
    InputMap inputMap = buttonA.getInputMap();
    inputMap.put(controlAlt7, ACTION_KEY);
    ActionMap actionMap = buttonA.getActionMap();
    actionMap.put(ACTION_KEY, actionListener);

    KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, true);
    inputMap = buttonB.getInputMap();
    inputMap.put(enter, ACTION_KEY);
    buttonB.setActionMap(actionMap);

    KeyStroke shiftF4 = KeyStroke.getKeyStroke(KeyEvent.VK_F4, InputEvent.SHIFT_MASK);
    inputMap = buttonC.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    inputMap.put(shiftF4, ACTION_KEY);
    buttonC.setActionMap(actionMap);

    KeyStroke space = KeyStroke.getKeyStroke(' ');
    inputMap = buttonD.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    inputMap.put(space, ACTION_KEY);
    buttonD.setActionMap(actionMap);

    frame.setLayout(new GridLayout(2, 2));
    frame.add(buttonA);
    frame.add(buttonB);
    frame.add(buttonC);
    frame.add(buttonD);

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