List of usage examples for java.awt.event ActionListener ActionListener
ActionListener
From source file:Main.java
public static final void main(String args[]) throws Exception { JFrame f = new JFrame(); DefaultListModel<String> dlm = new DefaultListModel<String>(); JList<String> list = new JList<>(dlm); f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); f.add(new JScrollPane(list)); f.add(new JButton("Add") { {/*from w ww . ja v a 2 s. c o m*/ addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { dlm.addElement("A"); } }); } }, BorderLayout.SOUTH); f.pack(); f.setVisible(true); }
From source file:MainClass.java
public static void main(final String args[]) { JFrame frame = new JFrame("Popup Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JTextField textField = new JTextField(); frame.add(textField, BorderLayout.NORTH); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { int dotPosition = textField.getCaretPosition(); Rectangle popupLocation = null; try { popupLocation = textField.modelToView(dotPosition); } catch (BadLocationException e) { e.printStackTrace();/*from w w w. j a v a 2 s . c o m*/ } System.out.println(popupLocation); } }; KeyStroke keystroke = KeyStroke.getKeyStroke(KeyEvent.VK_PERIOD, 0, false); textField.registerKeyboardAction(actionListener, keystroke, JComponent.WHEN_FOCUSED); frame.setSize(250, 150); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] args) { JPanel gui = new JPanel(new BorderLayout()); gui.setPreferredSize(new Dimension(400, 100)); JDesktopPane dtp = new JDesktopPane(); gui.add(dtp, BorderLayout.CENTER); JButton newFrame = new JButton("Add Frame"); ActionListener listener = new ActionListener() { private int disp = 10; @Override/*from ww w . jav a 2 s . c om*/ public void actionPerformed(ActionEvent e) { JInternalFrame jif = new JInternalFrame(); dtp.add(jif); jif.setLocation(disp, disp); jif.setSize(100, 100); disp += 10; jif.setVisible(true); } }; newFrame.addActionListener(listener); gui.add(newFrame, BorderLayout.PAGE_START); JFrame f = new JFrame(); f.add(gui); f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); f.setLocationByPlatform(true); f.pack(); f.setVisible(true); }
From source file:Main.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."); }/*from w w w .j av a2 s.c om*/ }; MouseListener mouseListener = new MouseAdapter() { public void mousePressed(MouseEvent mouseEvent) { int modifiers = mouseEvent.getModifiers(); if ((modifiers & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK) { // Mask may not be set properly prior to Java 2 // See SwingUtilities.isLeftMouseButton() for workaround 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); Container contentPane = frame.getContentPane(); contentPane.add(button, BorderLayout.SOUTH); frame.setSize(300, 100); frame.setVisible(true); }
From source file:Main.java
public static void main(String args[]) { JFrame frame = new JFrame("Button Sample"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JButton button1 = new JButton("Select Me"); final JButton button2 = new JButton("No Select Me"); final Random random = new Random(); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { JButton button = (JButton) actionEvent.getSource(); int red = random.nextInt(255); int green = random.nextInt(255); int blue = random.nextInt(255); button.setBackground(new Color(red, green, blue)); }/*from w w w. j a va 2s . c o m*/ }; PropertyChangeListener propertyChangeListener = new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent propertyChangeEvent) { String property = propertyChangeEvent.getPropertyName(); if ("background".equals(property)) { button2.setBackground((Color) propertyChangeEvent.getNewValue()); } } }; button1.addActionListener(actionListener); button1.addPropertyChangeListener(propertyChangeListener); button2.addActionListener(actionListener); frame.add(button1, BorderLayout.NORTH); frame.add(button2, BorderLayout.SOUTH); frame.setSize(300, 100); frame.setVisible(true); }
From source file:ActiveSample.java
public static void main(String args[]) { JFrame frame = new JFrame("Active Example"); UIManager.put(LABEL_FACTORY, new ActiveLabel()); final JPanel panel = new JPanel(); JButton button = new JButton("Get"); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { JLabel label = (JLabel) UIManager.get(LABEL_FACTORY); panel.add(label);/* w w w . j av a 2 s .co m*/ panel.revalidate(); } }; button.addActionListener(actionListener); Container contentPane = frame.getContentPane(); contentPane.add(panel, BorderLayout.CENTER); contentPane.add(button, BorderLayout.SOUTH); frame.setSize(200, 200); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] args) { JFrame frame = new JFrame("JFrame"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container contentPane = frame.getContentPane(); JButton counterButton = new JButton("Clicked #0"); JButton closeButton = new JButton("Close"); frame.setLayout(new FlowLayout()); contentPane.add(closeButton);/*from w w w .j av a2 s. c om*/ contentPane.add(counterButton); counterButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { counterButton.setText("Clicked #" + counter++); } }); closeButton.addActionListener(e -> System.exit(0)); frame.pack(); frame.setVisible(true); }
From source file:MainClass.java
public static void main(String[] a) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JToggleButton toggleButton = new JToggleButton("Selected"); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { AbstractButton abstractButton = (AbstractButton) actionEvent.getSource(); boolean selected = abstractButton.getModel().isSelected(); System.out.println("Action - selected=" + selected + "\n"); }/*from w ww. j av a 2 s. c o m*/ }; ChangeListener changeListener = new ChangeListener() { public void stateChanged(ChangeEvent changeEvent) { AbstractButton abstractButton = (AbstractButton) changeEvent.getSource(); ButtonModel buttonModel = abstractButton.getModel(); boolean armed = buttonModel.isArmed(); boolean pressed = buttonModel.isPressed(); boolean selected = buttonModel.isSelected(); System.out.println("Changed: " + armed + "/" + pressed + "/" + selected); } }; ItemListener itemListener = new ItemListener() { public void itemStateChanged(ItemEvent itemEvent) { int state = itemEvent.getStateChange(); if (state == ItemEvent.SELECTED) { System.out.println("Selected"); } else { System.out.println("Deselected"); } } }; toggleButton.addActionListener(actionListener); toggleButton.addChangeListener(changeListener); toggleButton.addItemListener(itemListener); frame.add(toggleButton, BorderLayout.NORTH); JToggleButton toggleButton2 = new JToggleButton("Focused"); frame.add(toggleButton2, BorderLayout.CENTER); JToggleButton toggleButton3 = new JToggleButton("Not Selected"); frame.add(toggleButton3, BorderLayout.SOUTH); frame.setSize(300, 125); frame.setVisible(true); }
From source file:SimpleToolbar.java
public static void main(String[] args) { JFrame f = new JFrame(); JMenuBar menubar = new JMenuBar(); JMenu file = new JMenu("File"); menubar.add(file);/*from www .j a va2 s . c om*/ f.setJMenuBar(menubar); JToolBar toolbar = new JToolBar(); ImageIcon icon = new ImageIcon("exit.png"); JButton exit = new JButton(icon); toolbar.add(exit); exit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { System.exit(0); } }); f.add(toolbar, BorderLayout.NORTH); f.setSize(300, 200); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); }
From source file:MainClass.java
public static void main(String args[]) { final String LABEL_FACTORY = "LabelFactory"; JFrame frame = new JFrame("Active Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); UIManager.put(LABEL_FACTORY, new ActiveLabel()); final JPanel panel = new JPanel(); JButton button = new JButton("Get"); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { JLabel label = (JLabel) UIManager.get(LABEL_FACTORY); panel.add(label);//from w w w . j a va 2s .co m panel.revalidate(); } }; button.addActionListener(actionListener); frame.add(panel, BorderLayout.CENTER); frame.add(button, BorderLayout.SOUTH); frame.setSize(200, 200); frame.setVisible(true); }