List of usage examples for javax.swing JMenu JMenu
public JMenu(Action a)
Action
supplied. From source file:Main.java
public static void main(String args[]) { JFrame frame = new JFrame("Use TextAction"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JTextArea leftArea = new JTextArea(); final JTextArea rightArea = new JTextArea(); JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, new JScrollPane(leftArea), new JScrollPane(rightArea)); splitPane.setDividerLocation(.5);//from ww w .jav a 2s.co m JMenuBar menuBar = new JMenuBar(); frame.setJMenuBar(menuBar); JMenu menu = new JMenu("Options"); menuBar.add(menu); JMenuItem menuItem; Action readAction = leftArea.getActionMap().get(DefaultEditorKit.readOnlyAction); menuItem = menu.add(readAction); menuItem.setText("Make read-only"); Action writeAction = leftArea.getActionMap().get(DefaultEditorKit.writableAction); menuItem = menu.add(writeAction); menuItem.setText("Make writable"); menu.addSeparator(); Action cutAction = leftArea.getActionMap().get(DefaultEditorKit.cutAction); menuItem = menu.add(cutAction); menuItem.setText("Cut"); Action copyAction = leftArea.getActionMap().get(DefaultEditorKit.copyAction); menuItem = menu.add(copyAction); menuItem.setText("Copy"); Action pasteAction = leftArea.getActionMap().get(DefaultEditorKit.pasteAction); menuItem = menu.add(pasteAction); menuItem.setText("Paste"); frame.add(splitPane, BorderLayout.CENTER); frame.setSize(400, 250); frame.setVisible(true); }
From source file:DesktopDemo.java
public static void main(String[] args) { if (Desktop.isDesktopSupported()) { desktop = Desktop.getDesktop(); } else {/* w w w . ja va2 s .co m*/ System.out.println("Desktop class is not supported"); System.exit(1); } JMenuItem openItem = new JMenuItem("Open"); JMenuItem editItem = new JMenuItem("Edit"); JMenuItem printItem = new JMenuItem("Print"); JMenuItem browseToItem = new JMenuItem("Go to www.java2s.com"); JMenuItem mailToItem = new JMenuItem("Email to a@java.com"); JMenu fileMenu = new JMenu("File"); JMenu mailMenu = new JMenu("Email"); JMenu browseMenu = new JMenu("Browser"); openItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JFileChooser chooser = new JFileChooser(); if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { try { desktop.open(chooser.getSelectedFile().getAbsoluteFile()); } catch (Exception ex) { ex.printStackTrace(); } } } }); fileMenu.add(openItem); editItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JFileChooser chooser = new JFileChooser(); if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { try { desktop.edit(chooser.getSelectedFile().getAbsoluteFile()); } catch (Exception ex) { ex.printStackTrace(); } } } }); fileMenu.add(editItem); printItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JFileChooser chooser = new JFileChooser(); if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { try { desktop.print(chooser.getSelectedFile().getAbsoluteFile()); } catch (Exception ex) { ex.printStackTrace(); } } } }); fileMenu.add(printItem); browseToItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { URI browseURI = new URI("www.java2s.com"); desktop.browse(browseURI); } catch (Exception ex) { System.out.println(ex.getMessage()); } } }); browseMenu.add(browseToItem); mailToItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { URI mailURI = new URI("mailto:support@java.com"); desktop.mail(mailURI); } catch (Exception ex) { System.out.println(ex.getMessage()); } } }); mailMenu.add(mailToItem); JMenuBar jMenuBar = new JMenuBar(); jMenuBar.add(fileMenu); jMenuBar.add(browseMenu); jMenuBar.add(mailMenu); JFrame frame = new JFrame(); frame.setTitle("Desktop Helper Applications"); frame.setSize(300, 100); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setJMenuBar(jMenuBar); frame.setVisible(true); }
From source file:PrintHelloAction.java
public static void main(String args[]) { JFrame frame = new JFrame("Action Sample"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final Action printAction = new PrintHelloAction(); JMenuBar menuBar = new JMenuBar(); JMenu menu = new JMenu("File"); menuBar.add(menu);// w w w . ja v a 2s .c o m menu.add(new JMenuItem(printAction)); JToolBar toolbar = new JToolBar(); toolbar.add(new JButton(printAction)); JButton enableButton = new JButton("Enable"); ActionListener enableActionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { printAction.setEnabled(true); } }; enableButton.addActionListener(enableActionListener); JButton disableButton = new JButton("Disable"); ActionListener disableActionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { printAction.setEnabled(false); } }; disableButton.addActionListener(disableActionListener); JButton relabelButton = new JButton("Relabel"); ActionListener relabelActionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { printAction.putValue(Action.NAME, "Changed Action Value"); } }; relabelButton.addActionListener(relabelActionListener); JPanel buttonPanel = new JPanel(); buttonPanel.add(enableButton); buttonPanel.add(disableButton); buttonPanel.add(relabelButton); frame.setJMenuBar(menuBar); frame.add(toolbar, BorderLayout.SOUTH); frame.add(buttonPanel, BorderLayout.NORTH); frame.setSize(300, 200); frame.setVisible(true); }
From source file:MenuDemo1.java
public static void main(String[] args) { // Create a window for this demo JFrame frame = new JFrame("Menu Demo"); JPanel panel = new JPanel(); frame.getContentPane().add(panel, "Center"); // Create an action listener for the menu items we will create // The MenuItemActionListener class is defined below ActionListener listener = new MenuItemActionListener(panel); // Create some menu panes, and fill them with menu items // The menuItem() method is important. It is defined below. JMenu file = new JMenu("File"); file.setMnemonic('F'); file.add(menuItem("New", listener, "new", 'N', KeyEvent.VK_N)); file.add(menuItem("Open...", listener, "open", 'O', KeyEvent.VK_O)); file.add(menuItem("Save", listener, "save", 'S', KeyEvent.VK_S)); file.add(menuItem("Save As...", listener, "saveas", 'A', KeyEvent.VK_A)); JMenu edit = new JMenu("Edit"); edit.setMnemonic('E'); edit.add(menuItem("Cut", listener, "cut", 0, KeyEvent.VK_X)); edit.add(menuItem("Copy", listener, "copy", 'C', KeyEvent.VK_C)); edit.add(menuItem("Paste", listener, "paste", 0, KeyEvent.VK_V)); // Create a menubar and add these panes to it. JMenuBar menubar = new JMenuBar(); menubar.add(file);/*from ww w . ja va 2 s. co m*/ menubar.add(edit); // Add menubar to the main window. Note special method to add menubars frame.setJMenuBar(menubar); // Now create a popup menu and add the some stuff to it final JPopupMenu popup = new JPopupMenu(); popup.add(menuItem("Open...", listener, "open", 0, 0)); popup.addSeparator(); // Add a separator between items JMenu colors = new JMenu("Colors"); // Create a submenu popup.add(colors); // and add it to the popup menu // Now fill the submenu with mutually-exclusive radio buttons ButtonGroup colorgroup = new ButtonGroup(); colors.add(radioItem("Red", listener, "color(red)", colorgroup)); colors.add(radioItem("Green", listener, "color(green)", colorgroup)); colors.add(radioItem("Blue", listener, "color(blue)", colorgroup)); // Arrange to display the popup menu when the user clicks in the window panel.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { // Check whether this is the right type of event to pop up a popup // menu on this platform. Usually checks for right button down. if (e.isPopupTrigger()) popup.show((Component) e.getSource(), e.getX(), e.getY()); } }); // Finally, make our main window appear frame.setSize(450, 300); frame.setVisible(true); }
From source file:ShowAction.java
public static void main(final String args[]) { JFrame frame = new JFrame(); 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 a2 s . com*/ // File->New, N - Mnemonic JMenuItem newMenuItem = new JMenuItem(new ShowAction(frame)); fileMenu.add(newMenuItem); frame.setJMenuBar(menuBar); frame.setSize(350, 250); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] args) { JFrame frame = new JFrame(); frame.setUndecorated(true);/*from ww w . j ava 2 s. c o m*/ frame.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { point.x = e.getX(); point.y = e.getY(); } }); frame.addMouseMotionListener(new MouseMotionAdapter() { public void mouseDragged(MouseEvent e) { Point p = frame.getLocation(); frame.setLocation(p.x + e.getX() - point.x, p.y + e.getY() - point.y); } }); frame.setSize(300, 300); frame.setLocation(200, 200); frame.setLayout(new BorderLayout()); frame.getContentPane().add(new JLabel("Drag to move", JLabel.CENTER), BorderLayout.CENTER); JMenuBar menuBar = new JMenuBar(); JMenu menu = new JMenu("Menu"); menuBar.add(menu); JMenuItem item = new JMenuItem("Exit"); item.addActionListener(e -> System.exit(0)); menu.add(item); frame.setJMenuBar(menuBar); frame.setVisible(true); }
From source file:UseActions.java
public static void main(String args[]) { JFrame frame = new JFrame("Use TextAction"); Container contentPane = frame.getContentPane(); Dimension empty = new Dimension(0, 0); final JTextArea leftArea = new JTextArea(); JScrollPane leftScrollPane = new JScrollPane(leftArea); leftScrollPane.setPreferredSize(empty); final JTextArea rightArea = new JTextArea(); JScrollPane rightScrollPane = new JScrollPane(rightArea); rightScrollPane.setPreferredSize(empty); JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftScrollPane, rightScrollPane); JMenuBar menuBar = new JMenuBar(); frame.setJMenuBar(menuBar);/* ww w .jav a2 s .co m*/ JMenu menu = new JMenu("Options"); menuBar.add(menu); JMenuItem menuItem; Action readAction = leftArea.getActionMap().get(DefaultEditorKit.readOnlyAction); menuItem = menu.add(readAction); menuItem.setText("Make read-only"); Action writeAction = leftArea.getActionMap().get(DefaultEditorKit.writableAction); menuItem = menu.add(writeAction); menuItem.setText("Make writable"); menu.addSeparator(); Action cutAction = leftArea.getActionMap().get(DefaultEditorKit.cutAction); menuItem = menu.add(cutAction); menuItem.setText("Cut"); Action copyAction = leftArea.getActionMap().get(DefaultEditorKit.copyAction); menuItem = menu.add(copyAction); menuItem.setText("Copy"); Action pasteAction = leftArea.getActionMap().get(DefaultEditorKit.pasteAction); menuItem = menu.add(pasteAction); menuItem.setText("Paste"); contentPane.add(splitPane, BorderLayout.CENTER); frame.setSize(400, 250); frame.setVisible(true); splitPane.setDividerLocation(.5); }
From source file:MediumPopupSample.java
public static void main(String args[]) { ActionListener menuListener = new MenuActionListener(); 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 va 2s . co m*/ // File->New, N - Mnemonic JMenuItem newMenuItem = new JMenuItem("New", KeyEvent.VK_N); newMenuItem.addActionListener(menuListener); fileMenu.add(newMenuItem); // File->Open, O - Mnemonic JMenuItem openMenuItem = new JMenuItem("Open", KeyEvent.VK_O); openMenuItem.addActionListener(menuListener); fileMenu.add(openMenuItem); // File->Close, C - Mnemonic JMenuItem closeMenuItem = new JMenuItem("Close", KeyEvent.VK_C); closeMenuItem.addActionListener(menuListener); fileMenu.add(closeMenuItem); // Separator fileMenu.addSeparator(); // File->Save, S - Mnemonic JMenuItem saveMenuItem = new JMenuItem("Save", KeyEvent.VK_S); saveMenuItem.addActionListener(menuListener); fileMenu.add(saveMenuItem); // Separator fileMenu.addSeparator(); // File->Exit, X - Mnemonic JMenuItem exitMenuItem = new JMenuItem("Exit", KeyEvent.VK_X); exitMenuItem.addActionListener(menuListener); fileMenu.add(exitMenuItem); // Edit Menu, E - Mnemonic JMenu editMenu = new JMenu("Edit"); editMenu.setMnemonic(KeyEvent.VK_E); menuBar.add(editMenu); // Edit->Cut, T - Mnemonic, CTRL-X - Accelerator JMenuItem cutMenuItem = new JMenuItem("Cut", KeyEvent.VK_T); cutMenuItem.addActionListener(menuListener); KeyStroke ctrlXKeyStroke = KeyStroke.getKeyStroke("control X"); cutMenuItem.setAccelerator(ctrlXKeyStroke); editMenu.add(cutMenuItem); // Edit->Copy, C - Mnemonic, CTRL-C - Accelerator JMenuItem copyMenuItem = new JMenuItem("Copy", KeyEvent.VK_C); copyMenuItem.addActionListener(menuListener); KeyStroke ctrlCKeyStroke = KeyStroke.getKeyStroke("control C"); copyMenuItem.setAccelerator(ctrlCKeyStroke); editMenu.add(copyMenuItem); // Edit->Paste, P - Mnemonic, CTRL-V - Accelerator, Disabled JMenuItem pasteMenuItem = new JMenuItem("Paste", KeyEvent.VK_P); pasteMenuItem.addActionListener(menuListener); KeyStroke ctrlVKeyStroke = KeyStroke.getKeyStroke("control V"); pasteMenuItem.setAccelerator(ctrlVKeyStroke); pasteMenuItem.setEnabled(false); editMenu.add(pasteMenuItem); // Separator editMenu.addSeparator(); // Edit->Find, F - Mnemonic, F3 - Accelerator JMenuItem findMenuItem = new JMenuItem("Find", KeyEvent.VK_F); findMenuItem.addActionListener(menuListener); KeyStroke f3KeyStroke = KeyStroke.getKeyStroke("F3"); findMenuItem.setAccelerator(f3KeyStroke); editMenu.add(findMenuItem); // Edit->Options Submenu, O - Mnemonic, at.gif - Icon Image File JMenu findOptionsMenu = new JMenu("Options"); Icon atIcon = new ImageIcon("at.gif"); findOptionsMenu.setIcon(atIcon); findOptionsMenu.setMnemonic(KeyEvent.VK_O); // ButtonGroup for radio buttons ButtonGroup directionGroup = new ButtonGroup(); // Edit->Options->Forward, F - Mnemonic, in group JRadioButtonMenuItem forwardMenuItem = new JRadioButtonMenuItem("Forward", true); forwardMenuItem.addActionListener(menuListener); forwardMenuItem.setMnemonic(KeyEvent.VK_F); findOptionsMenu.add(forwardMenuItem); directionGroup.add(forwardMenuItem); // Edit->Options->Backward, B - Mnemonic, in group JRadioButtonMenuItem backwardMenuItem = new JRadioButtonMenuItem("Backward"); backwardMenuItem.addActionListener(menuListener); backwardMenuItem.setMnemonic(KeyEvent.VK_B); findOptionsMenu.add(backwardMenuItem); directionGroup.add(backwardMenuItem); // Separator findOptionsMenu.addSeparator(); // Edit->Options->Case Sensitive, C - Mnemonic JCheckBoxMenuItem caseMenuItem = new JCheckBoxMenuItem("Case Sensitive"); caseMenuItem.addActionListener(menuListener); caseMenuItem.setMnemonic(KeyEvent.VK_C); findOptionsMenu.add(caseMenuItem); editMenu.add(findOptionsMenu); frame.setJMenuBar(menuBar); frame.setSize(350, 250); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] args) throws Exception { Toolkit tk = Toolkit.getDefaultToolkit(); final Main main = new Main(); tk.addAWTEventListener(main, AWTEvent.WINDOW_EVENT_MASK); final JFrame frame = new JFrame(""); frame.setName("your frame"); JMenuBar mb = new JMenuBar(); JMenu menu = new JMenu("File"); menu.add(new AbstractAction("Quit") { public void actionPerformed(ActionEvent evt) { try { main.saveSettings();//from w w w. java 2 s . c o m System.exit(0); } catch (Exception ex) { System.out.println(ex); } } }); mb.add(menu); frame.setJMenuBar(mb); frame.pack(); frame.setVisible(true); }
From source file:AboutDialog.java
public static void main(String[] args) { JMenuBar menubar = new JMenuBar(); JMenu file = new JMenu("File"); file.setMnemonic(KeyEvent.VK_F); JMenu help = new JMenu("Help"); help.setMnemonic(KeyEvent.VK_H); JMenuItem about = new JMenuItem("About"); help.add(about);//from w w w . j a v a 2 s . c o m about.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { AboutDialog ad = new AboutDialog(); ad.setVisible(true); } }); menubar.add(file); menubar.add(help); JFrame f = new JFrame(); f.setJMenuBar(menubar); f.setSize(300, 200); f.setLocationRelativeTo(null); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); }