Presentation is loading. Please wait.

Presentation is loading. Please wait.

Menus Pull-downs and popups. Tooltips –a ToolTip is a context-sensitive text string that is displayed in a popup window when the mouse rests over a particular.

Similar presentations


Presentation on theme: "Menus Pull-downs and popups. Tooltips –a ToolTip is a context-sensitive text string that is displayed in a popup window when the mouse rests over a particular."— Presentation transcript:

1 Menus Pull-downs and popups

2 Tooltips –a ToolTip is a context-sensitive text string that is displayed in a popup window when the mouse rests over a particular object on the screen JButton myButton = new JButton(“Accept Order”); myButton.setToolTipText(“By selecting this button you accept the order and it will be shipped to you”);

3 Menus To create a Menu: –create a Menu Bar –create a Menu Object –create Menu Items –add Menu Items to a Menu Object –add Menu Objects to the Menu Bar

4 Menus Pull-downs are found on a Menu bar Menu bars may only appear in frames JMenuBar menuBar = new JMenuBar();

5 Menus Create the Menu object JMenu hamburgerMenu = new JMenu(“Hamburger”); Next create the Menu items JMenuItem plainItem = new JMenuItem(“Plain”); JMenuItem mustardItem = new JMenuItem(“Mustard”); JMenuItem everythingItem = new JMenuItem(“Everything”):

6 Menus Now add the item to the Menu object hamburgerMenu.add(plainItem); Next add the Menu objects to the Menu Bar menuBar.add(hamburgerMenu);

7 Menus Selecting a menu item will generate an action event. You need to install an action event listener for each menu item plainItem.addActionListener(this); Adding menu items listeners is easy but TEDIOUS.

8 Cut and Paste example –JMenu menu = new JMenu(“Edit”); –item = new JMenuItem(“Cut”); –item.addActionListener(this); –menu.add(item); –item = new JMenuItem(“Paste”); –item.addActionListener(this); –menu.add(item); –menuBar.add(menu);

9 Reacting to menu events Public void actionPerformed(ActionEvent evt); –{ if (evt.getSource() instanceof JMenuItem) String arg = evt.getActionCommand(); if (arg.equals(“Cut”)){ do something } else if (arg.equals(“Paste”)) {do something else} –}

10 Menu Items Adding –adds a menu item Removing –removes a specific item from the menu Inserting –adds a new menu item (or submenu) to the menu at a specific index Gray-out menu items

11 Menus Icons on a menu item –Icon hamburgerIcon = new (“smallHamburger.gif”); –item = new JMenuItem(“Plain”, hamburgerIcon)); –hamburger.add(item);

12 Checkboxs on menu items Checkbox - when the user selects the menu item, the item automatically toggles between checked and unchecked. JCheckBoxMenuItem readonlyItem = new JCheckBoxMenuItem(“Read only”);

13 Radio buttons on menu items Radio button menu items work just like regular radio buttons. You must add them to a button group. When one of the buttons in the selected, all others are deselected.

14 Radio buttons on menu items ButtonGroup group = new ButtonGroup(); JRadioButtonMenuITem plainItem = new JRadioButtonMenuItem(“Plain”); plainItem.setSelected(true); JRadioButtonMenuItem everythingItem = new JRadioButtonMenuItem (“Everything”); group.add(plainItem); group.add(everythtingItem);

15 Popup menus A pop-up menu is a menu that is not attached to a menu bar but that floats somewhere on the frame or panel. JPopupMenu popup = new JPopupMenu(); You then add menu items in the usual way. You must specify the parent component and the location of the popup popup.show(panel, x, y);

16 Popups Pop-up trigger install a mouse listener add code for the mouse handler: –public void mouseReleased(MouseEvent e) –{ if (e. isPopupTrigger()) –popup.show(e.getComponent(), e.getX(), getY() ); –}

17 Keyboard Mnemonics It is a real convenience for the experienced user to select menu items by keyboard mnemonics. JMenuItem cutItem = new JMenuItem(“Cut”, ‘T’); To select a top-level menu from the menu bar, you hit ALT+ the mnemonic letter JMenu helpMenu = new Jmenu (“Help”); helpMenu.setMnemonic(‘H’);

18 Accelerators Accelerators are keyboard shortcuts that let you select menu items without ever opening a menu. E.g., CTRL+O and CTRL+S in many programs allow the user to Open and Save items in the File menu.

19 Accelerators setAccelerator method will attach an accelerator key to a menu item E.g., the following will attach the accelerator CTRL+O to the openITem menu item: openItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK _O, InputEvent.CTRL+MASK)); When the user presses the accelerator key combination, this will select the menu option and fires an action event, as if the user had selected the menu option manually


Download ppt "Menus Pull-downs and popups. Tooltips –a ToolTip is a context-sensitive text string that is displayed in a popup window when the mouse rests over a particular."

Similar presentations


Ads by Google