Taming Tiger: Context popup menus Author: John Zukowski

xiaoxiao2021-03-06  85

Taming Tiger: Context Popup Menus

Automated Popup Menu Handling

John Zukowski

(jaz@zukowski.net) President, JZ Ventures, Inc.12 May 2004

The JPopupMenu class has been around since the beginning of Swing time. It allows you to show context-sensitive options over a part of the screen so that common operations can be close at hand. Based on which version of the Java language you were using, there were different ways to determine if a particular mouse event was the triggering event (for instance, on some platforms the event was initiated by releasing the right mouse button while on others it was by pressing another button). Taking into account right-handed and . left-handed options and the number of mouse buttons available on different platforms, the task was not always easy Now, with Tiger, all that code goes away - you simply associate a JPopupMenu with any JComponent and it shows at the appropriate time Automatically.

Getting started with JPopupMenuThe JPopupMenu component is the container class for context-sensitive menus -. Those menus that appear when you typically right click over something on a Microsoft Windows box In a browser, for instance, bringing up the popup menu will typically allow you To Bookmark a page or view its source.

Creating a jpopupmenu for the Java Platform Is Relatively Easy. Create a jpopupmenu

Listing 1. CREANG A JPopUpMenu

JMenuitem amenuitem1 = ...

JMenuitem amenuitem2 = ...

JMenuitem amenuitem3 = ...

JPopupmenu Menu = new jpopupmenu ();

Menu.add (amenuitem1);

Menu.add (amenuitem2);

Menu.Add (Amenuitem3);

Just having a popup menu is not sufficient, though. It's your responsibility as the programmer to show the popup menu at the appropriate time and the appropriate location. That's where the fun begins. The show () method of JPopupMenu is the appropriate method to call, but when to call the method and where to show it has evolved over the various releases of the Java platform.In the 1.1 and 1.2 releases of Swing, the MouseEvent class offered the isPopupTrigger () method. By checking the results of this method in both the mousePressed () and mouseReleased () methods of your MouseListener, you could show a popup menu at the x and y coordinates of that event and the menu would appear where the user clicked. Listing 2 shows a complete example of this approach, With the results shown in Figure 1:

Listing 2. Showing a jpopunu in swing 1.1 / 1.2

Import java.awt. *;

Import java.awt.event. *;

Import javax.swing. *;

Public class popupsample {

Public static void main (string args []) {

ActionListener ActionListener = New ActionListener () {

Public void actionPerformed (ActionEvent ActionEvent) {

System.out.println ("SELECTED:" ActionEvent.getActionCommand ());

}

}

JFrame Frame = New JFrame ("Popup Example");

Frame.setDefaultCloseOperation (jframe.exit_on_close);

Final Jpopupmenu Popupmenu = New JPopUpMenu ();

// CUT

JMenuItem Cutmenem = New Jmenuitem ("CUT");

Cutmenuitem.AddActionListener (ActionListener);

PopupMenu.Add (cutmenuitem);

// COPY

JMenuItem CopyMenuItem = New Jmenuitem ("Copy");

CopyMenuItem.AddActionListener (ActionListener);

PopupMenu.Add (CopyMenuItem);

// paste

JMenuItem PasteMenuItem = New Jmenuitem ("Paste");

PasteMenuItem.AddActionListener (ActionListener); PasteMenuItem.setenabled (false);

PopupMenu.Add (PasteMenuItem);

// separator

Popupmenu.addseparator ();

// Find

JMenuItem FindMenuItem = New Jmenuitem ("Find");

FindMenuItem.AddActionListener (ActionListener);

PopupMenu.Add (FindMenuItem);

Mouselistener mouselistener = new mouseadapter () {

Private void showifpopuptrigger (mouseevent mouseevent) {

IF (mouseevent.ispopuptrigger ()) {

PopupMenu.show (mouseevent.getComponent (),

mouseevent.getx (),

MouseEvent.gety ());

}

}

Public void mousepressed (mouseeevent mouseevent) {

ShowifpopupTrigger (MouseEvent);

}

Public void mouseeleased (mouseevent mouseevent) {

ShowifpopupTrigger (MouseEvent);

}

}

Frame.addmouselistener (mouselistener);

Frame.setsize (350, 250);

Frame.setVisible (TRUE);

}

}

Figure 1. Jpopupmenu in action

Going from the 1.1 / 1.2 releases of Swing to the 1.3 release resulted in a minor change to the code in Listing 2. Instead of checking within the MouseEvent class itself to see if it is the popup trigger, the check moved into the JPopupMenu class. Given that the MouseEvent class should not really know about the JPopupMenu class, it seemed like the appropriate thing to do Listing 3 shows the change to the showIfPopupTrigger () method shown in Listing 2.:

Listing 3. Showing a jpopupmenu in swing 1.3

Private void showifpopuptrigger (mouseevent mouseevent) {

IF (PopupMenu.ispopupTrigger (mouseevent)) {// this line here changed

PopupMenu.show (mouseevent.getComponent (),

mouseevent.getx (),

MouseEvent.gety ());

}

}

................... ..

Tiger makes displaying popups easyWith the Tiger release, the triggering action to show a JPopupMenu has changed yet again. Sure, the old code will still work, but there is now an even easier way. You now call the newly introduced setComponentPopupMenu () method, which associates a JPopupMenu with a JComponent, so you no longer have to add the mouse listener that calls the show () method Listing 4 shows the sample code, with the results shown in Figure 2:. Listing 4. Showing a JPopupMenu in Tiger

Import java.awt. *;

Import java.awt.event. *;

Import javax.swing. *;

PUBLIC CLASS popupsample15 {

Public static void main (string args []) {

ActionListener ActionListener = New ActionListener () {

Public void actionPerformed (ActionEvent ActionEvent) {

System.out.println ("SELECTED:" ActionEvent.getActionCommand ());

}

}

JFrame Frame = New JFrame ("Tiger Popup Example");

Frame.setDefaultCloseOperation (jframe.exit_on_close);

Final Jpopupmenu Popupmenu = New JPopUpMenu ();

// CUT

JMenuItem Cutmenem = New Jmenuitem ("CUT");

Cutmenuitem.AddActionListener (ActionListener);

PopupMenu.Add (cutmenuitem);

// COPY

JMenuItem CopyMenuItem = New Jmenuitem ("Copy");

CopyMenuItem.AddActionListener (ActionListener);

PopupMenu.Add (CopyMenuItem);

// paste

JMenuItem PasteMenuItem = New Jmenuitem ("Paste");

PasteMenuItem.AddActionListener (ActionListener);

PasteMenuItem.setenabled (false);

PopupMenu.Add (PasteMenuItem);

// separator

Popupmenu.addseparator ();

// Find

JMenuItem FindMenuItem = New Jmenuitem ("Find");

FindMenuItem.AddActionListener (ActionListener);

PopupMenu.Add (FindMenuItem);

JButton Button = New JButton ("Hi");

Button.SetComponentPopupnupMenu (Popupmenu); Frame.getContentPane (). add (button, borderlayout.center);

Frame.setsize (350, 250);

Frame.setVisible (TRUE);

}

}

Figure 2. JPopupmenu in Tiger

Notice that the only thing done here was to call the setComponentPopupMenu () method of the JButton to associate the JPopupMenu with it. No show () call was necessary. With Tiger, the triggering of the popup menu is now part of the UI definition of ...................

One more thing introduced with Tiger popup menus is the setInheritsPopupMenu () method. All the components within a panel can be made to inherit the popup menu of that panel. Now you will not have to associate the popup menu with each component.

ConclusionIf you keep in mind that JFrame is not a JComponent, you'll find it easy to work with JPopupMenu components in the new way. The new setComponentPopupMenu () method simplifies your code and really moves the detection of the triggering event to the appropriate spot . As More uis area.................

转载请注明原文地址:https://www.9cbs.com/read-121922.html

New Post(0)