List of usage examples for java.awt.event ActionListener ActionListener
ActionListener
From source file:ContructMenuMenuSelectionManager.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);/* w w w . ja v a 2s. co m*/ // File->New, N - Mnemonic JMenuItem newMenuItem = new JMenuItem("New"); fileMenu.add(newMenuItem); newMenuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { MenuElement[] eles = MenuSelectionManager.defaultManager().getSelectedPath(); for (MenuElement ele : eles) { System.out.println(ele); } MenuSelectionManager.defaultManager().clearSelectedPath(); } }); frame.setJMenuBar(menuBar); frame.setSize(350, 250); frame.setVisible(true); }
From source file:JRadioButtonSelectedElements.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;//ww w . jav a2 s . c om 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); System.out.println(getSelectedElements(panel).hasMoreElements()); }
From source file:Main.java
public static void main(String[] args) { int TIME_VISIBLE = 3000; JFrame frame1 = new JFrame(); frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame1.setSize(100, 100);// w ww . j av a2s. c om frame1.setLocation(100, 100); JButton button = new JButton("My Button"); frame1.getContentPane().add(button); button.addActionListener(e -> { JOptionPane pane = new JOptionPane("Message", JOptionPane.INFORMATION_MESSAGE); JDialog dialog = pane.createDialog(null, "Title"); dialog.setModal(false); dialog.setVisible(true); new Timer(TIME_VISIBLE, new ActionListener() { @Override public void actionPerformed(ActionEvent e) { dialog.setVisible(false); } }).start(); }); frame1.setVisible(true); }
From source file:MainClass.java
public static void main(String args[]) { 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);//w w w.ja v a 2 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:ManualDisplayPopup.java
public static void main(String args[]) { JFrame frame = new JFrame("NoButton Popup"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton button = new JButton("Ask"); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { Component source = (Component) actionEvent.getSource(); JOptionPane optionPane = new JOptionPane("Continue printing?", JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION); JDialog dialog = optionPane.createDialog(source, "Manual Creation"); dialog.show();/*from www .ja v a 2s . c o m*/ int selection = OptionPaneUtils.getSelection(optionPane); System.out.println(selection); } }; button.addActionListener(actionListener); Container contentPane = frame.getContentPane(); contentPane.add(button, BorderLayout.SOUTH); frame.setSize(300, 200); frame.setVisible(true); }
From source file:Main.java
public static void main(String args[]) { JFrame frame = new JFrame("Type tab to and then type"); 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);/* ww w . j av a 2 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:PropertyChangeListenerDemo.java
public static void main(String args[]) { Runnable runner = new Runnable() { public void run() { 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(); // Define ActionListener 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 ww .j a v a2 s .c o m*/ }; // Define PropertyChangeListener 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); } }; EventQueue.invokeLater(runner); }
From source file:MainClass.java
public static void main(String args[]) throws Exception { JFrame vFrame = new JFrame("Vertical Split"); vFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JComponent leftButton = new JButton("Left"); JComponent rightButton = new JButton("Right"); final JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT); splitPane.setOneTouchExpandable(true); splitPane.setLeftComponent(leftButton); splitPane.setRightComponent(rightButton); ActionListener oneActionListener = new ActionListener() { public void actionPerformed(ActionEvent event) { splitPane.resetToPreferredSizes(); }/*from w w w . jav a 2s .c om*/ }; ((JButton) rightButton).addActionListener(oneActionListener); ActionListener anotherActionListener = new ActionListener() { public void actionPerformed(ActionEvent event) { splitPane.setDividerLocation(10); splitPane.setContinuousLayout(true); } }; ((JButton) leftButton).addActionListener(anotherActionListener); HierarchyListener hierarchyListener = new HierarchyListener() { public void hierarchyChanged(HierarchyEvent e) { long flags = e.getChangeFlags(); if ((flags & HierarchyEvent.SHOWING_CHANGED) == HierarchyEvent.SHOWING_CHANGED) { splitPane.setDividerLocation(.75); } } }; splitPane.addHierarchyListener(hierarchyListener); vFrame.add(splitPane, BorderLayout.CENTER); vFrame.setSize(300, 150); vFrame.setVisible(true); }
From source file:Main.java
public static void main(String[] args) { Runnable r = new Runnable() { @Override//from w w w .j av a 2s . c o m public void run() { JPanel gui = new JPanel(); final AnimatedImage[] tiles = new AnimatedImage[2]; for (int ii = 0; ii < tiles.length; ii++) { tiles[ii] = new AnimatedImage(); gui.add(new JLabel(new ImageIcon(tiles[ii]))); } ActionListener listener = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { for (int i = 0; i < tiles.length; i++) { tiles[i].paintImage(); gui.repaint(); } } }; Timer timer = new Timer(50, listener); timer.start(); JOptionPane.showMessageDialog(null, gui); timer.stop(); } }; SwingUtilities.invokeLater(r); }
From source file:Main.java
public static void main(String[] args) { JFrame frame = new JFrame(); JTextPane codearea = new JTextPane(); JScrollPane scroll;/*from w w w. j a v a 2s. c o m*/ scroll = new JScrollPane(codearea, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED); scroll.setPreferredSize(new Dimension(300, 300)); JPanel panel = new JPanel(new BorderLayout()); panel.add(scroll, BorderLayout.CENTER); JButton comp = new JButton("Print text"); comp.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.out.println(codearea.getText()); } }); panel.add(comp, BorderLayout.SOUTH); frame.getContentPane().add(panel); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); }