List of usage examples for java.awt.event ActionEvent getActionCommand
public String getActionCommand()
From source file:MainClass.java
public static void main(String[] a) { JFrame frame = new JFrame("JFileChooser Popup"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JFileChooser fileChooser = new JFileChooser("."); fileChooser.setControlButtonsAreShown(false); frame.add(fileChooser, BorderLayout.CENTER); 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(); System.out.println(selectedFile.getParent()); System.out.println(selectedFile.getName()); } else if (command.equals(JFileChooser.CANCEL_SELECTION)) { System.out.println(JFileChooser.CANCEL_SELECTION); }//w ww. j av a2 s .c o m } }; fileChooser.addActionListener(actionListener); frame.pack(); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] a) { JFrame frame = new JFrame("JFileChooser Popup"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JFileChooser fileChooser = new JFileChooser("."); fileChooser.setControlButtonsAreShown(false); frame.add(fileChooser, BorderLayout.CENTER); 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(); System.out.println(selectedFile.getParent()); System.out.println(selectedFile.getName()); } else if (command.equals(JFileChooser.CANCEL_SELECTION)) { System.out.println(JFileChooser.CANCEL_SELECTION); }//ww w . java 2s . co m } }; fileChooser.addActionListener(actionListener); fileChooser.removeActionListener(actionListener); frame.pack(); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] argv) throws Exception { MyFileChooser chooser = new MyFileChooser(); chooser.setDialogType(JFileChooser.SAVE_DIALOG); final JDialog dialog = chooser.createDialog(null); chooser.addActionListener(new AbstractAction() { public void actionPerformed(ActionEvent evt) { JFileChooser chooser = (JFileChooser) evt.getSource(); if (JFileChooser.APPROVE_SELECTION.equals(evt.getActionCommand())) { dialog.setVisible(false); } else if (JFileChooser.CANCEL_SELECTION.equals(evt.getActionCommand())) { dialog.setVisible(false); }//from ww w. j a va 2 s .c om } }); dialog.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { dialog.setVisible(false); } }); dialog.setVisible(true); }
From source file:PopupSample.java
public static void main(String args[]) { ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { System.out.println("Selected: " + actionEvent.getActionCommand()); }/*from w w w .j a va2 s.c o m*/ }; PopupMenuListener popupMenuListener = new PopupMenuListener() { public void popupMenuCanceled(PopupMenuEvent popupMenuEvent) { System.out.println("Canceled"); } public void popupMenuWillBecomeInvisible(PopupMenuEvent popupMenuEvent) { System.out.println("Becoming Invisible"); } public void popupMenuWillBecomeVisible(PopupMenuEvent popupMenuEvent) { System.out.println("Becoming Visible"); } }; JFrame frame = new JFrame("Popup Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPopupMenu popupMenu = new JPopupMenu(); popupMenu.addPopupMenuListener(popupMenuListener); JMenuItem cutMenuItem = new JMenuItem("Cut"); cutMenuItem.addActionListener(actionListener); popupMenu.add(cutMenuItem); JMenuItem copyMenuItem = new JMenuItem("Copy"); copyMenuItem.addActionListener(actionListener); popupMenu.add(copyMenuItem); JMenuItem pasteMenuItem = new JMenuItem("Paste"); pasteMenuItem.addActionListener(actionListener); pasteMenuItem.setEnabled(false); popupMenu.add(pasteMenuItem); popupMenu.addSeparator(); JMenuItem findMenuItem = new JMenuItem("Find"); findMenuItem.addActionListener(actionListener); popupMenu.add(findMenuItem); MouseListener mouseListener = new JPopupMenuShower(popupMenu); frame.addMouseListener(mouseListener); 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(" "); }// w w w . j a v a 2 s . c om } }; fileChooser.addActionListener(actionListener); frame.pack(); frame.setVisible(true); }
From source file:SimpleMenu.java
/** A simple test program for the above code */ public static void main(String[] args) { // Get the locale: default, or specified on command-line Locale locale;/*w w w .ja v a2s. co m*/ if (args.length == 2) locale = new Locale(args[0], args[1]); else locale = Locale.getDefault(); // Get the resource bundle for that Locale. This will throw an // (unchecked) MissingResourceException if no bundle is found. ResourceBundle bundle = ResourceBundle.getBundle("com.davidflanagan.examples.i18n.Menus", locale); // Create a simple GUI window to display the menu with final JFrame f = new JFrame("SimpleMenu: " + // Window title locale.getDisplayName(Locale.getDefault())); JMenuBar menubar = new JMenuBar(); // Create a menubar. f.setJMenuBar(menubar); // Add menubar to window // Define an action listener for that our menu will use. ActionListener listener = new ActionListener() { public void actionPerformed(ActionEvent e) { String s = e.getActionCommand(); Component c = f.getContentPane(); if (s.equals("red")) c.setBackground(Color.red); else if (s.equals("green")) c.setBackground(Color.green); else if (s.equals("blue")) c.setBackground(Color.blue); } }; // Now create a menu using our convenience routine with the resource // bundle and action listener we've created JMenu menu = SimpleMenu.create(bundle, "colors", new String[] { "red", "green", "blue" }, listener); // Finally add the menu to the GUI, and pop it up menubar.add(menu); // Add the menu to the menubar f.setSize(300, 150); // Set the window size. f.setVisible(true); // Pop the window up. }
From source file:MediumPopupMenuSample.java
public static void main(String args[]) { // Define ActionListener ActionListener aListener = new ActionListener() { public void actionPerformed(ActionEvent event) { System.out.println("Selected: " + event.getActionCommand()); }/*from w w w .j a v a 2s . com*/ }; // Define PopupMenuListener PopupMenuListener pListener = new PopupMenuListener() { public void popupMenuCanceled(PopupMenuEvent event) { System.out.println("Canceled"); } public void popupMenuWillBecomeInvisible(PopupMenuEvent event) { System.out.println("Becoming Invisible"); } public void popupMenuWillBecomeVisible(PopupMenuEvent event) { System.out.println("Becoming Visible"); } }; // Define JFrame frame = new JFrame("Popup Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPopupMenu.setDefaultLightWeightPopupEnabled(false); // Create popup menu, attach popup menu listener final JPopupMenu popupMenu = new JPopupMenu(); popupMenu.addPopupMenuListener(pListener); // Cut JMenuItem cutItem = new JMenuItem("Cut"); cutItem.addActionListener(aListener); popupMenu.add(cutItem); // Copy JMenuItem copyItem = new JMenuItem("Copy"); copyItem.addActionListener(aListener); popupMenu.add(copyItem); // Paste JMenuItem pasteItem = new JMenuItem("Paste"); pasteItem.addActionListener(aListener); pasteItem.setEnabled(false); popupMenu.add(pasteItem); // Separator popupMenu.addSeparator(); // Find JMenuItem findItem = new JMenuItem("Find"); findItem.addActionListener(aListener); popupMenu.add(findItem); // Enable showing MouseListener mouseListener = new JPopupMenuShower(popupMenu); frame.addMouseListener(mouseListener); Button button = new Button("Label"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println(popupMenu.isLightWeightPopupEnabled()); } }); frame.getContentPane().add(button, BorderLayout.SOUTH); frame.setSize(350, 250); frame.setVisible(true); }
From source file:SelectingComboSample.java
public static void main(String args[]) { String labels[] = { "A", "B", "C", "D", "E", "F", "G", "H", "J", "I" }; JFrame frame = new JFrame("Selecting JComboBox"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container contentPane = frame.getContentPane(); JComboBox comboBox = new JComboBox(labels); contentPane.add(comboBox, BorderLayout.SOUTH); final JTextArea textArea = new JTextArea(); textArea.setEditable(false);/*from w w w . j a v a 2s . co m*/ JScrollPane sp = new JScrollPane(textArea); contentPane.add(sp, BorderLayout.CENTER); ItemListener itemListener = new ItemListener() { public void itemStateChanged(ItemEvent itemEvent) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); int state = itemEvent.getStateChange(); String stateString = ((state == ItemEvent.SELECTED) ? "Selected" : "Deselected"); pw.print("Item: " + itemEvent.getItem()); pw.print(", State: " + stateString); ItemSelectable is = itemEvent.getItemSelectable(); pw.print(", Selected: " + selectedString(is)); pw.println(); textArea.append(sw.toString()); } }; comboBox.addItemListener(itemListener); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); pw.print("Command: " + actionEvent.getActionCommand()); ItemSelectable is = (ItemSelectable) actionEvent.getSource(); pw.print(", Selected: " + selectedString(is)); pw.println(); textArea.append(sw.toString()); } }; comboBox.addActionListener(actionListener); frame.setSize(400, 200); frame.setVisible(true); }
From source file:SwingToolBarSample.java
public static void main(String args[]) { ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { System.out.println(actionEvent.getActionCommand()); }/*from w w w . j a v a 2 s . c o m*/ }; JFrame frame = new JFrame("JToolBar Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JToolBar toolbar = new JToolBar(); toolbar.putClientProperty("JToolBar.isRollover", Boolean.TRUE); for (int i = 0, n = buttonColors.length; i < n; i++) { Object color[] = buttonColors[i]; if (color == null) { toolbar.addSeparator(); } else { Icon icon = new DiamondIcon((Color) color[COLOR_POSITION], true, 20, 20); JButton button = new JButton(icon); button.setActionCommand((String) color[STRING_POSITION]); button.addActionListener(actionListener); toolbar.add(button); } } Action action = new ActionMenuSample.ShowAction(frame); toolbar.add(action); Container contentPane = frame.getContentPane(); contentPane.add(toolbar, BorderLayout.NORTH); JTextArea textArea = new JTextArea(); JScrollPane pane = new JScrollPane(textArea); contentPane.add(pane, BorderLayout.CENTER); frame.setSize(350, 150); frame.setVisible(true); }
From source file:OverlaySample.java
public static void main(String args[]) { ActionListener generalActionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { JComponent comp = (JComponent) actionEvent.getSource(); System.out.println(actionEvent.getActionCommand() + ": " + comp.getBounds()); }//w ww. ja v a 2 s . c om }; ActionListener sizingActionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { setupButtons(actionEvent.getActionCommand()); } }; JFrame frame = new JFrame("Overlay Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(); LayoutManager overlay = new OverlayLayout(panel); panel.setLayout(overlay); Object settings[][] = { { "Small", new Dimension(25, 25), Color.white }, { "Medium", new Dimension(50, 50), Color.gray }, { "Large", new Dimension(100, 100), Color.black } }; JButton buttons[] = { smallButton, mediumButton, largeButton }; for (int i = 0, n = settings.length; i < n; i++) { JButton button = buttons[i]; button.addActionListener(generalActionListener); button.setActionCommand((String) settings[i][0]); button.setMaximumSize((Dimension) settings[i][1]); button.setBackground((Color) settings[i][2]); panel.add(button); } setupButtons(SET_CENTRAL); JPanel actionPanel = new JPanel(); actionPanel.setBorder(BorderFactory.createTitledBorder("Change Alignment")); String actionSettings[] = { SET_MINIMUM, SET_MAXIMUM, SET_CENTRAL, SET_MIXED }; for (int i = 0, n = actionSettings.length; i < n; i++) { JButton button = new JButton(actionSettings[i]); button.addActionListener(sizingActionListener); actionPanel.add(button); } Container contentPane = frame.getContentPane(); contentPane.add(panel, BorderLayout.CENTER); contentPane.add(actionPanel, BorderLayout.SOUTH); frame.setSize(400, 300); frame.setVisible(true); }