List of usage examples for java.awt.event ActionEvent getActionCommand
public String getActionCommand()
From source file:Main.java
public static void main(final String args[]) { JButton button = new JButton("Text Button"); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { String command = actionEvent.getActionCommand(); System.out.println("Selected: " + command); }//from w w w .j a v a 2s .c om }; button.setActionCommand("First"); button.addActionListener(actionListener); JOptionPane.showMessageDialog(null, button); }
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 w w w. j a v a 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(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 . j a v a2 s.c o m }; comboBox.addActionListener(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)); }//ww w.java 2s . 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()); }// w w w . ja v a 2 s . c om }; comboBox.addActionListener(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(", Selected: " + new Date(actionEvent.getWhen())); }/*from ww w .jav a2s .com*/ }; comboBox.addActionListener(actionListener); frame.setSize(400, 200); frame.setVisible(true); }
From source file:DefaultSample.java
public static void main(String args[]) { JFrame frame = new JFrame("Default Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JTextField textField = new JTextField(); frame.add(textField, BorderLayout.NORTH); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { System.out.println(actionEvent.getActionCommand() + " selected"); }/*from w w w .j av a 2 s .com*/ }; JPanel panel = new JPanel(); JButton defaultButton = new JButton("Default Button"); defaultButton.addActionListener(actionListener); panel.add(defaultButton); JButton otherButton = new JButton("Other Button"); otherButton.addActionListener(actionListener); panel.add(otherButton); frame.add(panel, BorderLayout.SOUTH); Keymap keymap = textField.getKeymap(); KeyStroke keystroke = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false); keymap.removeKeyStrokeBinding(keystroke); frame.getRootPane().setDefaultButton(defaultButton); frame.setSize(250, 150); frame.setVisible(true); }
From source file:DefaultSample.java
public static void main(String args[]) { JFrame frame = new JFrame("Default Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container content = frame.getContentPane(); JTextField textField = new JTextField(); content.add(textField, BorderLayout.NORTH); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { System.out.println(actionEvent.getActionCommand() + " selected"); }/*from w ww. j av a 2 s .co m*/ }; JPanel panel = new JPanel(); JButton defaultButton = new JButton("Default Button"); defaultButton.addActionListener(actionListener); panel.add(defaultButton); JButton otherButton = new JButton("Other Button"); otherButton.addActionListener(actionListener); panel.add(otherButton); content.add(panel, BorderLayout.SOUTH); Keymap keymap = textField.getKeymap(); KeyStroke keystroke = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false); keymap.removeKeyStrokeBinding(keystroke); frame.getRootPane().setDefaultButton(defaultButton); frame.setSize(250, 150); frame.setVisible(true); }
From source file:Main.java
public static void main(String args[]) { JFrame frame = new JFrame("Key Text Sample"); KeyTextComponent keyTextComponent = new KeyTextComponent(); final JTextField textField = new JTextField(); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { String keyText = actionEvent.getActionCommand(); textField.setText(keyText);//from w ww . j a v a2 s .c om } }; keyTextComponent.addActionListener(actionListener); frame.add(keyTextComponent, BorderLayout.CENTER); frame.add(textField, BorderLayout.SOUTH); frame.setSize(300, 200); frame.setVisible(true); }
From source file:KeyTextComponent.java
public static void main(String[] a) { JFrame frame = new JFrame("Key Text Sample"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); KeyTextComponent keyTextComponent = new KeyTextComponent(); final JTextField textField = new JTextField(); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { String keyText = actionEvent.getActionCommand(); textField.setText(keyText);//from w w w. j a va 2s. c o m } }; keyTextComponent.addActionListener(actionListener); frame.add(keyTextComponent, BorderLayout.CENTER); textField.setText("Press keyboard after clicking the above blank area"); frame.add(textField, BorderLayout.SOUTH); frame.setSize(300, 200); frame.setVisible(true); }