List of usage examples for java.awt.event ActionListener ActionListener
ActionListener
From source file:SelectingButton.java
public static void main(String args[]) { JFrame frame = new JFrame("Selecting Button"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton theButton = new JButton("Button"); ActionListener aListener = new ActionListener() { public void actionPerformed(ActionEvent event) { AbstractButton aButton = (AbstractButton) event.getSource(); boolean selected = aButton.getModel().isSelected(); System.out.println("Action - selected=" + selected + "\n"); }//from w w w . j av a2 s. c o m }; ChangeListener cListener = new ChangeListener() { public void stateChanged(ChangeEvent event) { AbstractButton aButton = (AbstractButton) event.getSource(); ButtonModel aModel = aButton.getModel(); boolean armed = aModel.isArmed(); boolean pressed = aModel.isPressed(); boolean selected = aModel.isSelected(); System.out.println("Changed: " + armed + "/" + pressed + "/" + selected); } }; theButton.addActionListener(aListener); theButton.addChangeListener(cListener); Container contentPane = frame.getContentPane(); contentPane.add(theButton, BorderLayout.CENTER); frame.setSize(300, 200); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] args) { JFrame frame = new JFrame("Testing"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new BorderLayout()); frame.add(new JLabel("Auto Hide")); frame.pack();// ww w . j av a 2 s. c o m frame.setVisible(true); Timer autoHideTimer = new Timer(1000, new ActionListener() { @Override public void actionPerformed(ActionEvent e) { frame.dispose(); } }); autoHideTimer.setRepeats(false); frame.addMouseListener(new MouseAdapter() { @Override public void mouseExited(MouseEvent e) { System.out.println("Restart..."); autoHideTimer.restart(); } @Override public void mouseEntered(MouseEvent e) { System.out.println("Stop"); autoHideTimer.stop(); } }); }
From source file:Main.java
public static void main(final String args[]) { final String labels[] = { "A", "B", "C", "D", "E" }; JFrame frame = new JFrame("Editable JComboBox"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JComboBox comboBox = new JComboBox(labels); comboBox.setMaximumRowCount(5);/*from w w w. jav a2 s. co m*/ comboBox.setEditable(true); frame.add(comboBox, BorderLayout.NORTH); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { System.out.println("Selected: " + comboBox.getSelectedItem()); System.out.println(", Position: " + comboBox.getSelectedIndex()); } }; comboBox.addActionListener(actionListener); frame.setSize(300, 200); 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 ww w . j ava 2s.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) { String[][] data = { { "Data", "Data" } }; String[] col = { "Col", "Col" }; final DefaultTableModel model = new DefaultTableModel(data, col); JTable table = new JTable(model); JButton addRow = new JButton("Add Empty Row"); addRow.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { model.addRow(new Object[] {}); }/*from w w w . j ava 2 s. co m*/ }); JPanel panel = new JPanel(new BorderLayout()); panel.add(new JScrollPane(table)); panel.add(addRow, BorderLayout.SOUTH); JOptionPane.showMessageDialog(null, panel); }
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 www. j a v a2 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); 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:MainClass.java
public static void main(String args[]) { JFrame frame = new JFrame("Button Sample"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton button = new JButton("Select Me"); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { System.out.println("I was selected."); }/* ww w .j a v a 2s .co m*/ }; MouseListener mouseListener = new MouseAdapter() { public void mousePressed(MouseEvent mouseEvent) { int modifiers = mouseEvent.getModifiers(); if ((modifiers & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK) { System.out.println("Left button pressed."); } if ((modifiers & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK) { System.out.println("Middle button pressed."); } if ((modifiers & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK) { System.out.println("Right button pressed."); } } public void mouseReleased(MouseEvent mouseEvent) { if (SwingUtilities.isLeftMouseButton(mouseEvent)) { System.out.println("Left button released."); } if (SwingUtilities.isMiddleMouseButton(mouseEvent)) { System.out.println("Middle button released."); } if (SwingUtilities.isRightMouseButton(mouseEvent)) { System.out.println("Right button released."); } System.out.println(); } }; button.addActionListener(actionListener); button.addMouseListener(mouseListener); frame.add(button, BorderLayout.SOUTH); frame.setSize(300, 100); frame.setVisible(true); }
From source file:JFileChooserTest.java
public static void main(String[] args) { JFrame.setDefaultLookAndFeelDecorated(true); JDialog.setDefaultLookAndFeelDecorated(true); JFrame frame = new JFrame("JComboBox Test"); frame.setLayout(new FlowLayout()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton button = new JButton("Select File"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { JFileChooser fileChooser = new JFileChooser(); int returnValue = fileChooser.showOpenDialog(null); if (returnValue == JFileChooser.APPROVE_OPTION) { File selectedFile = fileChooser.getSelectedFile(); System.out.println(selectedFile.getName()); }//from ww w.jav a 2 s.c om } }); frame.add(button); frame.pack(); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] args) { JDialog dialog;/* w ww. j a v a 2s. c om*/ JList jlist; ActionListener otherListener = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.out.println("current"); } }; JButton okButton = new JButton("OK"); okButton.addActionListener(e -> close(true)); JButton cancelButton = new JButton("Cancel"); cancelButton.addActionListener(e -> close(false)); jlist = new JList(new String[] { "A", "B", "C", "D", "E", "F", "G" }); jlist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); jlist.setVisibleRowCount(5); JScrollPane scroll = new JScrollPane(jlist); JPanel buttonsPanel = new JPanel(new FlowLayout()); buttonsPanel.add(okButton); buttonsPanel.add(cancelButton); JPanel content = new JPanel(new BorderLayout()); content.add(scroll, BorderLayout.CENTER); content.add(buttonsPanel, BorderLayout.SOUTH); dialog = new JDialog((Frame) null, true); dialog.setContentPane(content); dialog.pack(); dialog.getRootPane().registerKeyboardAction(otherListener, KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); dialog.getRootPane().registerKeyboardAction(otherListener, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); dialog.getRootPane().getInputMap().put(KeyStroke.getKeyStroke("SPACE"), "doSomething"); dialog.getRootPane().getActionMap().put("doSomething", new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { System.exit(0); } }); dialog.setVisible(true); }
From source file:PopupMenu.java
public static void main(String[] args) { JFrame frame = new JFrame("JPopupMenu"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final Toolkit toolkit = frame.getToolkit(); final JPopupMenu menu = new JPopupMenu(); JMenuItem menuItemBeep = new JMenuItem("Beep"); menuItemBeep.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { toolkit.beep();// ww w . j av a 2 s . co m } }); menu.add(menuItemBeep); JMenuItem menuItemClose = new JMenuItem("Close"); menuItemClose.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); menu.add(menuItemClose); frame.addMouseListener(new MouseAdapter() { public void mouseReleased(MouseEvent e) { if (e.getButton() == e.BUTTON3) { menu.show(e.getComponent(), e.getX(), e.getY()); } } }); frame.setSize(250, 200); frame.setLocationRelativeTo(null); frame.setVisible(true); }