You are on page 1of 5

University of Guyana Faculty of Natural Sciences Department of Computer Science

University of Guyana Faculty of Natural Sciences Department of Computer Science

CSI323 Advanced Java

(Lecture 3)

1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18.

//...where instance variables are declared JPopupMenu popup; JMenuItem popupItem; JTextArea textArea; //...where the GUI is constructed //Create the popup menu. popup = new JPopupMenu(); popupItem = new JMenuItem("Cut"); popup.add(popupItem); popupItem = new JMenuItem("Copy"); popup.add(popupItem); popupItem = new JMenuItem("Paste); popup.add(popupItem); popupItem = new JMenuItem("Delete); popup.add(popupItem); popup.addSeparator(); popupItem = new JMenuItem("Select All); popup.add(popupItem);

Creating Swing Applications - JPopupMenu, JToolBar, JTabbedPane


Adding a Pop-up Menu On Windows platforms, a routine click of the right mouse button would bring up yet another menu under the cursor. This is what we call a pop-up menu and it may be quite useful in some GUI design cases. A pop-up menu in Java is created using the JPopupMenu class and since it is triggered by a mouse click and pops up where ever the mouse cursor is positioned, you would have to make use of the MouseListener or MouseAdapter classes as well. The class that is interested in processing a mouse event either implements the MouseListener interface (and all the methods it contains) or extends / instantiates the abstract MouseAdapter class (overriding only the methods of interest).

19. //build component 20. textArea = new JTextArea(0,0); 21. //register a mouse listener on a component 22. textArea.addMouseListener(new MouseAdapter() { 23. 24. public void mousePressed(MouseEvent e) { 25. if(e.isPopupTrigger()){ 26. popup.show(textArea, e.getX(), e.getY()); 27. } 28. } 29. public void mouseReleased(MouseEvent e) { 30. if(e.isPopupTrigger()){ 31. popup.show(textArea, e.getX(), e.getY()); 32. } 33. } 34. });

To bring up a pop-up menu, you must register a mouse listener on each component that the popup menu should be associated with. After this is done the pop-up menu can be brought up by pressing the right mouse button while the cursor is over a component that is popup-enabled.

Associated Methods: isPopupTrigger public boolean isPopupTrigger() Returns whether or not this mouse event is the popup menu trigger event for the platform.

Page 1

Eldon Marks 1/25/2006

Page 2

Eldon Marks 1/25/2006

University of Guyana Faculty of Natural Sciences Department of Computer Science

University of Guyana Faculty of Natural Sciences Department of Computer Science

Note: Popup menus are triggered differently on different systems. Therefore, isPopupTrigger should be checked in both mousePressed and mouseReleased for proper cross-platform functionality. Returns: boolean, true if this event is the popup menu trigger for this platform

show public void show(Component invoker, int x, int y) Displays the popup menu at the position x,y in the coordinate space of the component invoker. Parameters: invoker - the component in whose space the popup menu is to appear x - the x coordinate in invoker's coordinate space at which the popup menu is to be displayed y - the y coordinate in invoker's coordinate space at which the popup menu is to be displayed

By default, the user can drag the tool bar to a different edge of its container or out into a window of its own. The figures shown depict the toolbar in three different arrangements after the user has dragged it to its respective position. For the drag-out behavior to work correctly, the tool bar must be in a container that uses BorderLayout. If, by chance the toolbar must remain immovable, the setFloatable(false) method may be used on the JToolBar instance. The following code implements the tool bar displayed above:
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. //...where instance variables are declared JToolBar toolbar; JButton create, open, save, print, undo, redo, cut, copy, paste, select; //where the GUI is constructed //Create toolbar and items toolBar = new JToolBar();

Working with a Tool Bar For quicker access and easy identification of options associated with a given application, a good toolbar may be the right choice. A good example of what a toolbar looks like would be the bar with all the icons just below the menu bar in Microsoft Word, for instance. In Java, a toolbar is implemented using the JToolBar class. JToolBar is a container that groups several components, usually buttons with icons, into a row or column.

create = new JButton(new ImageIcon("textpad/images/new.gif")); 11. create.setToolTipText("Create a new file"); 12. toolBar.add(create); 13. 14. open = new JButton(new ImageIcon("textpad/images/open.gif")); 15. open.setToolTipText("Open a file"); 16. toolBar.add(open); 17. 18. save = new JButton(new ImageIcon("textpad/images/save.gif")); 19. save.setToolTipText("Save"); 20. toolBar.add(save); 21. 22. toolBar.addSeparator(); 23. 24. print = new JButton(new ImageIcon("textpad/images/print.gif")); 25. print.setToolTipText("Print"); 26. toolBar.add(print); 27. 28. toolBar.addSeparator(); 29. 30. undo = new JButton(new ImageIcon("textpad/images/undo.gif")); 31. undo.setToolTipText("Undo last action"); 32. toolBar.add(undo); 33.

Page 3

Eldon Marks 1/25/2006

Page 4

Eldon Marks 1/25/2006

University of Guyana Faculty of Natural Sciences Department of Computer Science

University of Guyana Faculty of Natural Sciences Department of Computer Science

34. redo = new JButton(new ImageIcon("textpad/images/redo.gif")); 35. redo.setToolTipText("Redo last action"); 36. toolBar.add(redo); 37. 38. cut = new JButton(new ImageIcon("textpad/images/cut.gif")); 39. cut.setToolTipText("Cut"); 40. toolBar.add(cut); 41. 42. copy = new JButton(new ImageIcon("textpad/images/copy.gif")); 43. copy.setToolTipText("Copy"); 44. toolBar.add(copy); 45. 46. paste = new JButton(new ImageIcon("textpad/images/paste.gif")); 47. paste.setToolTipText("Paste"); 48. toolBar.add(paste); 49. 50. select = new JButton(new ImageIcon("textpad/images/sel_all.gif")); 51. select.setToolTipText("Select all text"); 52. toolBar.add(select); 53. //add the toolbar to the container that uses BorderLayout (JFrame) 54. getContentPane.add(toolBar, BorderLayout.NORTH);

The above screen shot shows that tabbed panes may have icons and tool tips as well. The following code implements the tabbed panes shown above:
1. 2. 3. 4. 5. 6. 7. 8. //where GUI is constructed ImageIcon icon = new ImageIcon("images/middle.gif"); JTabbedPane tabbedPane = new JTabbedPane(); Component panel1 = makeTextPanel("Blah"); tabbedPane.addTab("One", icon, panel1, "Does nothing"); tabbedPane.setSelectedIndex(0); Component panel2 = makeTextPanel("Blah blah"); tabbedPane.addTab("Two", icon, panel2, "Does twice as much nothing");

9. Component panel3 = makeTextPanel("Blah blah blah"); 10. tabbedPane.addTab("Three", icon, panel3, "Still does nothing"); 11. Component panel4 = makeTextPanel("Blah blah blah blah"); 12. tabbedPane.addTab("Four", icon, panel4, "Does nothing at all");

Organizing with Tabbed Panes There may come a time when you are required to create an application that is packed with controls, displays and interface categories that would be clearly insane to place together on the JFrames content pane. In a case like this, tabbed panes would be ideal. With the JTabbedPane class, you may be able to organise / categorise the various interface components into panels that share the same space and therefore save space as well. The user chooses which component to view by selecting the tab corresponding to the desired component. To create a tabbed pane, you simply instantiate JTabbedPane, create the components you wish it to display, and then add the components to the tabbed pane using the addTab method.

13. //where methods are defined 14. private Component makeTextPanel(String text) { JPanel panel = new JPanel(); JLabel label = new JLabel(text); label.setHorizontalAlignment(JLabel.CENTER); panel.setLayout(new GridLayout(1, 1)); panel.add(label); return panel; 15. }

Associated Methods addTab public void addTab(String title, Icon icon, Component component, String tip) Adds a component and tip represented by a title and/or icon, either of which can be null. If icon is non-null and it implements ImageIcon a corresponding

Page 5

Eldon Marks 1/25/2006

Page 6

Eldon Marks 1/25/2006

University of Guyana Faculty of Natural Sciences Department of Computer Science

University of Guyana Faculty of Natural Sciences Department of Computer Science

disabled icon will automatically be created and set on the tabbedpane. Cover method for insertTab. Parameters: title - the title to be displayed in this tab icon - the icon to be displayed in this tab component - the component to be displayed when this tab is clicked tip - the tooltip to be displayed for this tab setSelectedIndex public void setSelectedIndex(int index) Sets the selected index for this tabbedpane. The index must be a valid tab index or -1, which indicates that no tab should be selected (can also be used when there are no tabs in the tabbedpane). If a -1 value is specified when the tabbedpane contains one or more tabs, then the results will be implementation defined. Parameters: index - the index to be selected Throws: IndexOutOfBoundsException - if index is out of range (index < -1 || index >= tab count)

code implements the remaining three tab placements.


14. //Add the tabbed pane to the frame. 15. ...

16. tabbedPane.setTabPlacement(JTabbedPane.LEFT); 17. //for right and bottom, the arguments for setTabPlacement are JTabbedPane.RIGHT and JTabbedPane.BOTTOM resp. 18. getContentPane().add(tabbedPane);

Other Useful Methods insertTab public void insertTab(String title,Icon icon,Component component, String tip,int index) Inserts a component, at index, represented by a title and/or icon, either of which may be null. If icon is non-null and it implements ImageIcon a corresponding disabled icon will automatically be created and set on the tabbed pane. Uses java.util.Vector internally, see insertElementAt for details of insertion conventions. Parameters: title - the title to be displayed in this tab icon - the icon to be displayed in this tab component - The component to be displayed when this tab is clicked. tip - the tooltip to be displayed for this tab index - the position to insert this new tab removeTabAt public void removeTabAt(int index) Removes the tab at index. After the component associated with index is removed, its visibility is reset to true to ensure it will be visible if added to other containers.

By default, the arrangement used by JTabbedPane displays the tabs to the north of the container. By using the setTabPlacement method, you are able to specify different arrangements of the tabs.
setTabPlacement public void setTabPlacement(int tabPlacement) Sets the tab placement for this tabbedpane. Possible values are: JTabbedPane.TOP JTabbedPane.BOTTOM JTabbedPane.LEFT JTabbedPane.RIGHT The default value, if not set, is SwingConstants.TOP. Parameters: tabPlacement - the placement for the tabs relative to the content

The setTabPlacement method is used just before the tabbed pane is added to the container. The following

Page 7

Eldon Marks 1/25/2006

Page 8

Eldon Marks 1/25/2006

University of Guyana Faculty of Natural Sciences Department of Computer Science

University of Guyana Faculty of Natural Sciences Department of Computer Science

Parameters: index - the index of the tab to be removed Throws: IndexOutOfBoundsException - if index is out of range (index < 0 || index >= tab count)

2.

Using tabbed panes, model the Windows System Configuration Utility (msconfig.exe) shown below. You are only required to implement the components on the General tab.

Lecture Summary To add a pop-up menu, you create a menu using the JPopupMenu class and attach a mouse listener to the component that must trigger the pop-up when the right mouse button is pressed. You may implement the MouseListener interface (and all the methods it contains) or extend / instantiate the abstract MouseAdapter class (overriding only the methods of interest). To provide easy access to frequently used menu options, a toolbar is most useful. You may add a toolbar by using the JToolBar class. The added toolbar usually contains buttons with icons only, but it may be constructed with other components such as combo boxes and text fields. By default, toolbars may be dragged to various positions of its container provided that the container uses a BorderLayout policy. When the need to save space and organize UI components comes up, the best choice would be to use tabbed panes. To create a tabbed pane, instantiate JTabbedPane then create and add the various components to the tabbed pane. The components that must be added must be constructed within an object of type Component or must be extended extended from Component (such as JPanel) and then finally added using the addTab method. The placement of the tabs may be specified as well using the setTabPlacement method.

Exercises: 1. Build upon the previous exercise in JMenuBar,JMenu,JMenuItems by adding a toolbar as well as a pop-up menu over the text area.

Page 9

Eldon Marks 1/25/2006

Page 10

Eldon Marks 1/25/2006

You might also like