List of usage examples for javax.swing JMenu JMenu
public JMenu(Action a)
Action
supplied. From source file:ToggleSample.java
public static void main(String args[]) { JFrame frame = new JFrame("JToggleButtonMenuItem Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JMenuBar bar = new JMenuBar(); JMenu file = new JMenu("File"); file.setMnemonic('f'); JMenuItem newItem = new JMenuItem("New", 'N'); file.add(newItem);/* www. ja va 2s. co m*/ JMenuItem openItem = new JMenuItem("Open", 'O'); file.add(openItem); JMenuItem closeItem = new JMenuItem("Close", 'C'); file.add(closeItem); file.addSeparator(); JMenuItem saveItem = new JMenuItem("Save", 'S'); file.add(saveItem); file.addSeparator(); JMenuItem exitItem = new JMenuItem("Exit", 'X'); file.add(exitItem); bar.add(file); JMenu edit = new JMenu("Edit"); JMenuItem cutItem = new JMenuItem("Cut", 'T'); cutItem.setAccelerator(KeyStroke.getKeyStroke('X', Event.CTRL_MASK)); edit.add(cutItem); JMenuItem copyItem = new JMenuItem("Copy", 'C'); copyItem.setAccelerator(KeyStroke.getKeyStroke('C', Event.CTRL_MASK)); edit.add(copyItem); JMenuItem pasteItem = new JMenuItem("Paste", 'P'); pasteItem.setAccelerator(KeyStroke.getKeyStroke('V', Event.CTRL_MASK)); pasteItem.setEnabled(false); edit.add(pasteItem); edit.addSeparator(); JMenuItem findItem = new JMenuItem("Find", 'F'); findItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F3, 0)); edit.add(findItem); edit.setMnemonic('e'); Icon atIcon = new ImageIcon("at.gif"); JMenu findOptions = new JMenu("Options"); findOptions.setIcon(atIcon); findOptions.setMnemonic('O'); ButtonGroup directionGroup = new ButtonGroup(); JRadioButtonMenuItem forward = new JRadioButtonMenuItem("Forward", true); findOptions.add(forward); directionGroup.add(forward); JRadioButtonMenuItem backward = new JRadioButtonMenuItem("Backward"); findOptions.add(backward); directionGroup.add(backward); findOptions.addSeparator(); JCheckBoxMenuItem caseItem = new JCheckBoxMenuItem("Case Insensitive"); findOptions.add(caseItem); edit.add(findOptions); JToggleButtonMenuItem toggleItem = new JToggleButtonMenuItem("Ballon Help"); toggleItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("Selected"); } }); edit.add(toggleItem); bar.add(edit); frame.setJMenuBar(bar); frame.setSize(350, 250); frame.setVisible(true); }
From source file:RadioButtonSample.java
public static void main(String args[]) { ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { AbstractButton aButton = (AbstractButton) actionEvent.getSource(); boolean selected = aButton.getModel().isSelected(); System.out.println(actionEvent.getActionCommand() + " - selected? " + selected); }// w ww . ja va 2s. c om }; ItemListener itemListener = new ItemListener() { public void itemStateChanged(ItemEvent itemEvent) { AbstractButton aButton = (AbstractButton) itemEvent.getSource(); int state = itemEvent.getStateChange(); String selected = ((state == ItemEvent.SELECTED) ? "selected" : "not selected"); System.out.println(aButton.getText() + " - selected? " + selected); } }; JFrame frame = new JFrame("Radio Menu Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JMenuBar menuBar = new JMenuBar(); JMenu menu = new JMenu("Menu"); ButtonGroup buttonGroup = new ButtonGroup(); menu.setMnemonic(KeyEvent.VK_M); JRadioButtonMenuItem emptyMenuItem = new JRadioButtonMenuItem(); emptyMenuItem.setActionCommand("Empty"); emptyMenuItem.addActionListener(actionListener); buttonGroup.add(emptyMenuItem); menu.add(emptyMenuItem); JRadioButtonMenuItem oneMenuItem = new JRadioButtonMenuItem("Partridge"); oneMenuItem.addActionListener(actionListener); buttonGroup.add(oneMenuItem); menu.add(oneMenuItem); JRadioButtonMenuItem twoMenuItem = new JRadioButtonMenuItem("Turtle Doves", true); twoMenuItem.addActionListener(actionListener); buttonGroup.add(twoMenuItem); menu.add(twoMenuItem); JRadioButtonMenuItem threeMenuItem = new JRadioButtonMenuItem("French Hens", threeIcon); threeMenuItem.addItemListener(itemListener); buttonGroup.add(threeMenuItem); menu.add(threeMenuItem); JRadioButtonMenuItem fourMenuItem = new JRadioButtonMenuItem("Calling Birds", fourIcon, true); fourMenuItem.addActionListener(actionListener); buttonGroup.add(fourMenuItem); menu.add(fourMenuItem); JRadioButtonMenuItem fiveMenuItem = new JRadioButtonMenuItem(fiveIcon); fiveMenuItem.addActionListener(actionListener); fiveMenuItem.setActionCommand("Rings"); buttonGroup.add(fiveMenuItem); menu.add(fiveMenuItem); JRadioButtonMenuItem sixMenuItem = new JRadioButtonMenuItem(sixIcon, true); sixMenuItem.addActionListener(actionListener); sixMenuItem.setActionCommand("Geese"); buttonGroup.add(sixMenuItem); menu.add(sixMenuItem); menuBar.add(menu); frame.setJMenuBar(menuBar); frame.setSize(350, 250); frame.setVisible(true); }
From source file:ColorAction.java
public static void main(String[] args) { JFrame frame = new JFrame(); frame.setTitle("SeparateGUITest"); frame.setSize(300, 200);//from w ww .j av a 2 s . com frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); JPanel panel = new JPanel(); Action blueAction = new ColorAction("Blue", new ImageIcon("blue-ball.gif"), Color.blue, panel); Action yellowAction = new ColorAction("Yellow", new ImageIcon("yellow-ball.gif"), Color.yellow, panel); Action redAction = new ColorAction("Red", new ImageIcon("red-ball.gif"), Color.red, panel); panel.add(new JButton(yellowAction)); panel.add(new JButton(blueAction)); panel.add(new JButton(redAction)); panel.registerKeyboardAction(yellowAction, KeyStroke.getKeyStroke(KeyEvent.VK_Y, 0), JComponent.WHEN_IN_FOCUSED_WINDOW); panel.registerKeyboardAction(blueAction, KeyStroke.getKeyStroke(KeyEvent.VK_B, 0), JComponent.WHEN_IN_FOCUSED_WINDOW); panel.registerKeyboardAction(redAction, KeyStroke.getKeyStroke(KeyEvent.VK_R, 0), JComponent.WHEN_IN_FOCUSED_WINDOW); Container contentPane = frame.getContentPane(); contentPane.add(panel); JMenu m = new JMenu("Color"); m.add(yellowAction); m.add(blueAction); m.add(redAction); JMenuBar mbar = new JMenuBar(); mbar.add(m); frame.setJMenuBar(mbar); frame.show(); }
From source file:CheckBoxSample.java
public static void main(String args[]) { ActionListener aListener = new ActionListener() { public void actionPerformed(ActionEvent event) { AbstractButton aButton = (AbstractButton) event.getSource(); boolean selected = aButton.getModel().isSelected(); String newLabel;// w ww. j a v a2 s .c o m Icon newIcon; if (selected) { newLabel = "Girl"; newIcon = girlIcon; } else { newLabel = "Boy"; newIcon = boyIcon; } aButton.setText(newLabel); aButton.setIcon(newIcon); } }; ItemListener iListener = new ItemListener() { public void itemStateChanged(ItemEvent event) { AbstractButton aButton = (AbstractButton) event.getSource(); int state = event.getStateChange(); String newLabel; Icon newIcon; if (state == ItemEvent.SELECTED) { newLabel = "Girl"; newIcon = girlIcon; } else { newLabel = "Boy"; newIcon = boyIcon; } aButton.setText(newLabel); aButton.setIcon(newIcon); } }; JFrame frame = new JFrame("CheckBox Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JMenuBar bar = new JMenuBar(); JMenu menu = new JMenu("Menu"); menu.setMnemonic(KeyEvent.VK_M); JCheckBoxMenuItem one = new JCheckBoxMenuItem(); menu.add(one); JCheckBoxMenuItem two = new JCheckBoxMenuItem("Boy"); menu.add(two); JCheckBoxMenuItem three = new JCheckBoxMenuItem(boyIcon); menu.add(three); JCheckBoxMenuItem four = new JCheckBoxMenuItem("Girl", true); menu.add(four); JCheckBoxMenuItem five = new JCheckBoxMenuItem("Boy", boyIcon); five.addItemListener(iListener); menu.add(five); Icon stateIcon = new DiamondAbstractButtonStateIcon(Color.black); UIManager.put("CheckBoxMenuItem.checkIcon", stateIcon); JCheckBoxMenuItem six = new JCheckBoxMenuItem("Girl", girlIcon, true); six.addActionListener(aListener); menu.add(six); bar.add(menu); frame.setJMenuBar(bar); frame.setSize(350, 250); frame.setVisible(true); }
From source file:ActionsMenuBar.java
public static void main(String args[]) { final JFrame frame = new JFrame("TextAction Usage"); Container contentPane = frame.getContentPane(); final JScrollPane scrollPane = new JScrollPane(); contentPane.add(scrollPane, BorderLayout.CENTER); final JMenuBar menuBar = new JMenuBar(); frame.setJMenuBar(menuBar);/*w w w . ja va2s .c o m*/ ActionListener actionListener = new ActionListener() { JTextComponent component; public void actionPerformed(ActionEvent actionEvent) { // Determine which component selected String command = actionEvent.getActionCommand(); if (command.equals("JTextField")) { component = new JTextField(); } else if (command.equals("JPasswordField")) { component = new JPasswordField(); } else if (command.equals("JTextArea")) { component = new JTextArea(); } else if (command.equals("JTextPane")) { component = new JTextPane(); } else { component = new JEditorPane(); } scrollPane.setViewportView(component); // Process action list Action actions[] = component.getActions(); menuBar.removeAll(); menuBar.revalidate(); JMenu menu = null; for (int i = 0, n = actions.length; i < n; i++) { if ((i % 10) == 0) { menu = new JMenu("From " + i); menuBar.add(menu); } menu.add(actions[i]); } menuBar.revalidate(); } }; String components[] = { "JTextField", "JPasswordField", "JTextArea", "JTextPane", "JEditorPane" }; final Container componentsContainer = RadioButtonUtils.createRadioButtonGrouping(components, "Pick to List Actions", actionListener); contentPane.add(componentsContainer, BorderLayout.WEST); frame.setSize(400, 300); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] a) { final JFrame jf = new JFrame("JIFrameDemo Main Window"); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); screenSize.width -= 42;/*from w w w . j a v a2 s. co m*/ screenSize.height -= 42; jf.setSize(screenSize); jf.setLocation(20, 20); JMenuBar mb = new JMenuBar(); jf.setJMenuBar(mb); JMenu fm = new JMenu("File"); mb.add(fm); JMenuItem mi; fm.add(mi = new JMenuItem("Exit")); mi.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); JDesktopPane dtp = new JDesktopPane(); //dtp.setBackground(Color.GREEN); jf.setContentPane(dtp); JInternalFrame mboxFrame = new JInternalFrame("Mail Reader", true, true, true, true); JLabel reader = new JLabel("Mail Reader Would Be Here"); mboxFrame.setContentPane(reader); mboxFrame.setSize(400, 300); mboxFrame.setLocation(50, 50); mboxFrame.setVisible(true); dtp.add(mboxFrame); JInternalFrame compFrame = new JInternalFrame("Compose Mail", true, true, true, true); JLabel composer = new JLabel("Mail Compose Would Be Here"); compFrame.setContentPane(composer); compFrame.setSize(300, 200); compFrame.setLocation(200, 200); compFrame.setVisible(true); dtp.add(compFrame); JInternalFrame listFrame = new JInternalFrame("Users", true, true, true, true); JLabel list = new JLabel("List of Users Would Be Here"); listFrame.setContentPane(list); listFrame.setLocation(400, 400); listFrame.setSize(500, 200); listFrame.setVisible(true); dtp.add(listFrame); jf.setVisible(true); jf.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { jf.setVisible(false); jf.dispose(); System.exit(0); } }); }
From source file:flow.visibility.FlowMain.java
public static void main(String[] args) throws Exception { /************************************************************** * Creating the Main GUI //from w ww . ja va 2s . com **************************************************************/ final JDesktopPane desktop = new JDesktopPane(); final JMenuBar mb = new JMenuBar(); JMenu menu; /** Add File Menu to Open File and Save Result */ menu = new JMenu("File"); JMenuItem Open = new JMenuItem("Open"); JMenuItem Save = new JMenuItem("Save"); menu.add(Open); menu.add(Save); menu.addSeparator(); JMenuItem Exit = new JMenuItem("Exit"); menu.add(Exit); Exit.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.exit(0); } }); mb.add(menu); /** Add Control Menu to Start and Stop Capture */ menu = new JMenu("Control"); JMenuItem Start = new JMenuItem("Start Capture"); JMenuItem Stop = new JMenuItem("Stop Capture"); menu.add(Start); Start.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { FlowDumper.main(false); } }); menu.add(Stop); Stop.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { FlowDumper.main(true); } }); mb.add(menu); /** Add Configuration Menu for Tapping and Display */ menu = new JMenu("Configuration"); JMenuItem Tapping = new JMenuItem("Tapping Configuration"); JMenuItem Display = new JMenuItem("Display Configuration"); menu.add(Tapping); Tapping.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { FlowTapping.main(null); } }); menu.add(Display); mb.add(menu); /** Add Detail Menu for NetGrok Visualization and JEthereal Inspection */ menu = new JMenu("Flow Detail"); JMenuItem FlowVisual = new JMenuItem("Flow Visualization"); JMenuItem FlowInspect = new JMenuItem("Flow Inspections"); menu.add(FlowVisual); FlowVisual.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { NetworkView.main(null); } }); menu.add(FlowInspect); FlowInspect.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { Ethereal.main(null); } }); mb.add(menu); /** Add Help Menu for Software Information */ menu = new JMenu("Help"); JMenuItem About = new JMenuItem("About"); menu.add(About); About.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(null, "OF@TEIN Flow Visibility Tools @ 2015 by GIST", "About the Software", JOptionPane.PLAIN_MESSAGE); } }); mb.add(menu); /** Creating the main frame */ JFrame frame = new JFrame("OF@TEIN Flow Visibility Tools"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new BorderLayout()); frame.add(desktop); frame.setJMenuBar(mb); frame.setSize(1215, 720); frame.setLocationRelativeTo(null); frame.setVisible(true); /**Add Blank three (3) Internal Jframe*/ /**Internal Frame from Flow Summary Chart*/ JInternalFrame FlowStatistic = new JInternalFrame("Flow Statistic", true, true, true, true); FlowStatistic.setBounds(0, 0, 600, 330); ChartPanel chartPanel = new ChartPanel(createChart(null)); chartPanel.setMouseZoomable(true, false); FlowStatistic.add(chartPanel); FlowStatistic.setVisible(true); desktop.add(FlowStatistic); /**Internal Frame from Flow Summary Text*/ JInternalFrame FlowSummary = new JInternalFrame("Flow Summary", true, true, true, true); FlowSummary.setBounds(0, 331, 600, 329); JTextArea textArea = new JTextArea(50, 10); JScrollPane scrollPane = new JScrollPane(textArea); FlowSummary.add(scrollPane); FlowSummary.setVisible(true); desktop.add(FlowSummary); //JInternalFrame FlowInspection = new JInternalFrame("Flow Inspection", true, true, true, true); //FlowInspection.setBounds(601, 0, 600, 660); //JTextArea textArea2 = new JTextArea(50, 10); //JScrollPane scrollPane2 = new JScrollPane(textArea2); //FlowInspection.add(scrollPane2); //FlowInspection.setVisible(true); //desktop.add(FlowInspection); /**Internal Frame from Printing the Packet Sequence*/ JInternalFrame FlowSequence = new JInternalFrame("Flow Sequence", true, true, true, true); FlowSequence.setBounds(601, 0, 600, 660); JTextArea textArea3 = new JTextArea(50, 10); JScrollPane scrollPane3 = new JScrollPane(textArea3); FlowSequence.add(scrollPane3); FlowSequence.setVisible(true); desktop.add(FlowSequence); /************************************************************** * Update the Frame Regularly **************************************************************/ /** Regularly update the Frame Content every 3 seconds */ for (;;) { desktop.removeAll(); desktop.add(FlowProcess.FlowStatistic()); desktop.add(FlowProcess.FlowSummary()); //desktop.add(FlowProcess.FlowInspection()); desktop.add(FlowProcess.FlowSequence()); desktop.revalidate(); Thread.sleep(10000); } }
From source file:MenuX.java
public static void main(String args[]) { ActionListener actionListener = new MenuActionListener(); MenuKeyListener menuKeyListener = new MyMenuKeyListener(); ChangeListener cListener = new MyChangeListener(); MenuListener menuListener = new MyMenuListener(); MenuSelectionManager manager = MenuSelectionManager.defaultManager(); manager.addChangeListener(cListener); JFrame frame = new JFrame("MenuSample Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JMenuBar bar = new VerticalMenuBar(); // JMenuBar bar = new JMenuBar(); // File Menu, F - Mnemonic JMenu file = new JMenu("File"); file.setMnemonic(KeyEvent.VK_F); file.addChangeListener(cListener);//ww w. j a va 2 s . c o m file.addMenuListener(menuListener); file.addMenuKeyListener(menuKeyListener); JPopupMenu popupMenu = file.getPopupMenu(); popupMenu.setLayout(new GridLayout(3, 3)); bar.add(file); // File->New, N - Mnemonic JMenuItem newItem = new JMenuItem("New", KeyEvent.VK_N); newItem.addActionListener(actionListener); newItem.addChangeListener(cListener); newItem.addMenuKeyListener(menuKeyListener); file.add(newItem); // File->Open, O - Mnemonic JMenuItem openItem = new JMenuItem("Open", KeyEvent.VK_O); openItem.addActionListener(actionListener); openItem.addChangeListener(cListener); openItem.addMenuKeyListener(menuKeyListener); file.add(openItem); // File->Close, C - Mnemonic JMenuItem closeItem = new JMenuItem("Close", KeyEvent.VK_C); closeItem.addActionListener(actionListener); closeItem.addChangeListener(cListener); closeItem.addMenuKeyListener(menuKeyListener); file.add(closeItem); // Separator file.addSeparator(); // File->Save, S - Mnemonic JMenuItem saveItem = new JMenuItem("Save", KeyEvent.VK_S); saveItem.addActionListener(actionListener); saveItem.addChangeListener(cListener); saveItem.addMenuKeyListener(menuKeyListener); file.add(saveItem); // Separator file.addSeparator(); // File->Exit, X - Mnemonic JMenuItem exitItem = new JMenuItem("Exit", KeyEvent.VK_X); exitItem.addActionListener(actionListener); exitItem.addChangeListener(cListener); exitItem.addMenuKeyListener(menuKeyListener); file.add(exitItem); // Edit Menu, E - Mnemonic JMenu edit = new JMenu("Edit"); edit.setMnemonic(KeyEvent.VK_E); edit.addChangeListener(cListener); edit.addMenuListener(menuListener); edit.addMenuKeyListener(menuKeyListener); bar.add(edit); // Edit->Cut, T - Mnemonic, CTRL-X - Accelerator JMenuItem cutItem = new JMenuItem("Cut", KeyEvent.VK_T); cutItem.addActionListener(actionListener); cutItem.addChangeListener(cListener); cutItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, Event.CTRL_MASK)); cutItem.addMenuKeyListener(menuKeyListener); edit.add(cutItem); // Edit->Copy, C - Mnemonic, CTRL-C - Accelerator JMenuItem copyItem = new JMenuItem("Copy", KeyEvent.VK_C); copyItem.addActionListener(actionListener); copyItem.addChangeListener(cListener); copyItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, Event.CTRL_MASK)); copyItem.addMenuKeyListener(menuKeyListener); copyItem.setEnabled(false); edit.add(copyItem); // Edit->Paste, P - Mnemonic, CTRL-V - Accelerator, Disabled JMenuItem pasteItem = new JMenuItem("Paste", KeyEvent.VK_P); pasteItem.addActionListener(actionListener); pasteItem.addChangeListener(cListener); pasteItem.addMenuKeyListener(menuKeyListener); pasteItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, Event.CTRL_MASK)); pasteItem.setEnabled(false); edit.add(pasteItem); // Separator edit.addSeparator(); // Edit->Find, F - Mnemonic, F3 - Accelerator JMenuItem findItem = new JMenuItem("Find", KeyEvent.VK_F); findItem.addActionListener(actionListener); findItem.addChangeListener(cListener); findItem.addMenuKeyListener(menuKeyListener); findItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F3, 0)); edit.add(findItem); // Edit->Options Submenu, O - Mnemonic, at.gif - Icon Image File JMenu findOptions = new JMenu("Options"); findOptions.addChangeListener(cListener); findOptions.addMenuListener(menuListener); findOptions.addMenuKeyListener(menuKeyListener); Icon atIcon = new ImageIcon("at.gif"); findOptions.setIcon(atIcon); findOptions.setMnemonic(KeyEvent.VK_O); // ButtonGrou for radio buttons ButtonGroup directionGroup = new ButtonGroup(); // Edit->Options->Forward, F - Mnemonic, in group JRadioButtonMenuItem forward = new JRadioButtonMenuItem("Forward", true); forward.addActionListener(actionListener); forward.addChangeListener(cListener); forward.addMenuKeyListener(menuKeyListener); forward.setMnemonic(KeyEvent.VK_F); findOptions.add(forward); directionGroup.add(forward); // Edit->Options->Backward, B - Mnemonic, in group JRadioButtonMenuItem backward = new JRadioButtonMenuItem("Backward"); backward.addActionListener(actionListener); backward.addChangeListener(cListener); backward.addMenuKeyListener(menuKeyListener); backward.setMnemonic(KeyEvent.VK_B); findOptions.add(backward); directionGroup.add(backward); // Separator findOptions.addSeparator(); // Edit->Options->Case Sensitive, C - Mnemonic JCheckBoxMenuItem caseItem = new JCheckBoxMenuItem("Case Sensitive"); caseItem.addActionListener(actionListener); caseItem.addChangeListener(cListener); caseItem.addMenuKeyListener(menuKeyListener); caseItem.setMnemonic(KeyEvent.VK_C); findOptions.add(caseItem); edit.add(findOptions); frame.setJMenuBar(bar); // frame.getContentPane().add(bar, BorderLayout.EAST); frame.setSize(350, 250); frame.setVisible(true); }
From source file:MenuY.java
public static void main(String args[]) { ActionListener actionListener = new MenuActionListener(); MenuKeyListener menuKeyListener = new MyMenuKeyListener(); ChangeListener cListener = new MyChangeListener(); MenuListener menuListener = new MyMenuListener(); MenuSelectionManager manager = MenuSelectionManager.defaultManager(); manager.addChangeListener(cListener); JFrame frame = new JFrame("MenuSample Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JMenuBar bar = new VerticalMenuBar(); // JMenuBar bar = new JMenuBar(); // File Menu, F - Mnemonic JMenu file = new JMenu("File"); file.setMnemonic(KeyEvent.VK_F); file.addChangeListener(cListener);/*from www . ja v a2 s . c om*/ file.addMenuListener(menuListener); file.addMenuKeyListener(menuKeyListener); JPopupMenu popupMenu = file.getPopupMenu(); popupMenu.setLayout(new GridLayout(3, 3)); bar.add(file); // File->New, N - Mnemonic JMenuItem newItem = new JMenuItem("New", KeyEvent.VK_N); newItem.addActionListener(actionListener); newItem.addChangeListener(cListener); newItem.addMenuKeyListener(menuKeyListener); file.add(newItem); // File->Open, O - Mnemonic JMenuItem openItem = new JMenuItem("Open", KeyEvent.VK_O); openItem.addActionListener(actionListener); openItem.addChangeListener(cListener); openItem.addMenuKeyListener(menuKeyListener); file.add(openItem); // File->Close, C - Mnemonic JMenuItem closeItem = new JMenuItem("Close", KeyEvent.VK_C); closeItem.addActionListener(actionListener); closeItem.addChangeListener(cListener); closeItem.addMenuKeyListener(menuKeyListener); file.add(closeItem); // Separator file.addSeparator(); // File->Save, S - Mnemonic JMenuItem saveItem = new JMenuItem("Save", KeyEvent.VK_S); saveItem.addActionListener(actionListener); saveItem.addChangeListener(cListener); saveItem.addMenuKeyListener(menuKeyListener); file.add(saveItem); // Separator file.addSeparator(); // File->Exit, X - Mnemonic JMenuItem exitItem = new JMenuItem("Exit", KeyEvent.VK_X); exitItem.addActionListener(actionListener); exitItem.addChangeListener(cListener); exitItem.addMenuKeyListener(menuKeyListener); file.add(exitItem); // Edit Menu, E - Mnemonic JMenu edit = new JMenu("Edit"); edit.setMnemonic(KeyEvent.VK_E); edit.addChangeListener(cListener); edit.addMenuListener(menuListener); edit.addMenuKeyListener(menuKeyListener); bar.add(edit); // Edit->Cut, T - Mnemonic, CTRL-X - Accelerator JMenuItem cutItem = new JMenuItem("Cut", KeyEvent.VK_T); cutItem.addActionListener(actionListener); cutItem.addChangeListener(cListener); cutItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, Event.CTRL_MASK)); cutItem.addMenuKeyListener(menuKeyListener); edit.add(cutItem); // Edit->Copy, C - Mnemonic, CTRL-C - Accelerator JMenuItem copyItem = new JMenuItem("Copy", KeyEvent.VK_C); copyItem.addActionListener(actionListener); copyItem.addChangeListener(cListener); copyItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, Event.CTRL_MASK)); copyItem.addMenuKeyListener(menuKeyListener); copyItem.setEnabled(false); edit.add(copyItem); // Edit->Paste, P - Mnemonic, CTRL-V - Accelerator, Disabled JMenuItem pasteItem = new JMenuItem("Paste", KeyEvent.VK_P); pasteItem.addActionListener(actionListener); pasteItem.addChangeListener(cListener); pasteItem.addMenuKeyListener(menuKeyListener); pasteItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, Event.CTRL_MASK)); pasteItem.setEnabled(false); edit.add(pasteItem); // Separator edit.addSeparator(); // Edit->Find, F - Mnemonic, F3 - Accelerator JMenuItem findItem = new JMenuItem("Find", KeyEvent.VK_F); findItem.addActionListener(actionListener); findItem.addChangeListener(cListener); findItem.addMenuKeyListener(menuKeyListener); findItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F3, 0)); edit.add(findItem); // Edit->Options Submenu, O - Mnemonic, at.gif - Icon Image File Icon atIcon = new ImageIcon("at.gif"); Action findAction = new AbstractAction("Options", atIcon) { ActionListener actionListener = new MenuActionListener(); public void actionPerformed(ActionEvent e) { actionListener.actionPerformed(e); } }; findAction.putValue(Action.MNEMONIC_KEY, new Integer(KeyEvent.VK_O)); JMenuItem jMenuItem = new JMenuItem(findAction); JMenu findOptions = new JMenu(findAction); findOptions.addChangeListener(cListener); findOptions.addMenuListener(menuListener); findOptions.addMenuKeyListener(menuKeyListener); // ButtonGrou for radio buttons ButtonGroup directionGroup = new ButtonGroup(); // Edit->Options->Forward, F - Mnemonic, in group JRadioButtonMenuItem forward = new JRadioButtonMenuItem("Forward", true); forward.addActionListener(actionListener); forward.addChangeListener(cListener); forward.addMenuKeyListener(menuKeyListener); forward.setMnemonic(KeyEvent.VK_F); findOptions.add(forward); directionGroup.add(forward); // Edit->Options->Backward, B - Mnemonic, in group JRadioButtonMenuItem backward = new JRadioButtonMenuItem("Backward"); backward.addActionListener(actionListener); backward.addChangeListener(cListener); backward.addMenuKeyListener(menuKeyListener); backward.setMnemonic(KeyEvent.VK_B); findOptions.add(backward); directionGroup.add(backward); // Separator findOptions.addSeparator(); // Edit->Options->Case Sensitive, C - Mnemonic JCheckBoxMenuItem caseItem = new JCheckBoxMenuItem("Case Sensitive"); caseItem.addActionListener(actionListener); caseItem.addChangeListener(cListener); caseItem.addMenuKeyListener(menuKeyListener); caseItem.setMnemonic(KeyEvent.VK_C); findOptions.add(caseItem); edit.add(findOptions); frame.setJMenuBar(bar); // frame.getContentPane().add(bar, BorderLayout.EAST); frame.setSize(350, 250); frame.setVisible(true); }
From source file:SwingToolBarSample.java
public static void main(String args[]) { JFrame frame = new JFrame("Action Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Action showAction = new ShowAction(frame); JMenuBar menuBar = new JMenuBar(); JMenu fileMenu = new JMenu("File"); fileMenu.setMnemonic('f'); JMenuItem newMenuItem = new JMenuItem("New", 'N'); fileMenu.add(newMenuItem);//from w w w .j a v a 2s. c o m JMenuItem openMenuItem = new JMenuItem("Open", 'O'); fileMenu.add(openMenuItem); JMenuItem closeMenuItem = new JMenuItem("Close", 'C'); fileMenu.add(closeMenuItem); fileMenu.addSeparator(); JMenuItem saveMenuItem = new JMenuItem("Save", 'S'); fileMenu.add(saveMenuItem); fileMenu.add(showAction); fileMenu.addSeparator(); JMenuItem exitMenuItem = new JMenuItem("Exit", 'X'); fileMenu.add(exitMenuItem); menuBar.add(fileMenu); JMenu editMenu = new JMenu("Edit"); JMenuItem cutMenuItem = new JMenuItem("Cut", 'T'); KeyStroke ctrlXKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_X, Event.CTRL_MASK); cutMenuItem.setAccelerator(ctrlXKeyStroke); editMenu.add(cutMenuItem); JMenuItem copyMenuItem = new JMenuItem("Copy", 'C'); KeyStroke ctrlCKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_C, Event.CTRL_MASK); copyMenuItem.setAccelerator(ctrlCKeyStroke); editMenu.add(copyMenuItem); JMenuItem pasteMenuItem = new JMenuItem("Paste", 'P'); KeyStroke ctrlVKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_V, Event.CTRL_MASK); pasteMenuItem.setAccelerator(ctrlVKeyStroke); pasteMenuItem.setEnabled(false); editMenu.add(pasteMenuItem); editMenu.addSeparator(); JMenuItem findMenuItem = new JMenuItem("Find", 'F'); KeyStroke f3KeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_F3, 0); findMenuItem.setAccelerator(f3KeyStroke); editMenu.add(findMenuItem); editMenu.setMnemonic('e'); editMenu.add(showAction); menuBar.add(editMenu); frame.setJMenuBar(menuBar); frame.setSize(350, 250); frame.setVisible(true); }