JMenu Hell....

Status
Not open for further replies.

Ruziel

Ars Praetorian
573
Subscriptor++
Okay... as I've probable stated before, I'm in the process of modernizing a old JRE 1.1/AWT application to use JRE 1.3.x+ and Swing. The last major bit I have it to convert the AWT Menu bar to use Swing JMenu items.
The big problem is that the JMenu popups stay showing if you click on another window/application, unlike the AWT menus, and my product manager hates this behavior.

I thought I could kill the popups by adding this WindowListener:
code:<HR>
Code:
class MenuCloser extends WindowAdapter {<P>	public void windowDeactivated(WindowEvent event) {
		Object object = event.getSource();<P>		if (object == MainWindowLayout.this) {
            MenuSelectionManager.defaultManager().clearSelectedPath();
		}
	}
}

But this prevents the ActionListeners on the MenuItems from working. Anyone got a clue on how to kill the JMenu popups when the menu loses focus, without interfering with the JMenuItems' ActionListeners? :mad:
 

MikeApp

Ars Centurion
257
Subscriptor
I can't reproduce the problem. Here is a minimal example that does what you want (under the 1.4 RC):

code:<HR>
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;<P>public class MenuTest extends JFrame {
    
 JMenuBar menuBar;
 JMenu menu;   
 
 public MenuTest() { 
     super();    
     // create menu
     menu = new JMenu("Test");
     // create an item and add it to the menu
     menu.add( new DummyItem() );
     // create the menubar and add the menu
     menuBar = new JMenuBar();
     menuBar.add(menu);    
     //set up the window
     this.setJMenuBar( menuBar );
     this.addWindowListener( new WinAd(this) );
     this.resize( 400, 300 );
     this.show();
 }
 
 public static void main (String[] args) {
    MenuTest mt = new MenuTest(); 
 }
}<P>class WinAd extends WindowAdapter {
    private Component win;
    
    public WinAd(Component win) {
        this.win = win;
    }
    
    public void windowDeactivated(java.awt.event.WindowEvent windowEvent) {
        System.err.println("windowDeactivated");
        Object object = windowEvent.getSource();
        if (object == win) {
            MenuSelectionManager.defaultManager().clearSelectedPath();
        }  
    }
}<P>class DummyItem extends JMenuItem implements ActionListener {
    
    public DummyItem() {
        super("Click me!");
        this.addActionListener( this );
    }
    
    public void actionPerformed(java.awt.event.ActionEvent actionEvent) {
         System.err.println("actionPerformed");
    }
}
 
Status
Not open for further replies.