views:

296

answers:

1

I am using a JWindow object in my Java application to simulate a mouseover dropdown menu. When the user mouses over a JLabel, the window appears and remains until the mouse exits either the label or the newly visible window. My problem is that each time the user performs this action, a new entry in the task bar at the bottom of the screen appears, with no title or icon, and disappears as soon as setVisible(false) is called on the window.

I tried switching to an undecorated JDialog, and this fixed my task bar problem but introduced a new one. When setVisible(true) is called on the JDialog, the focus is taken away from my frame. The color of the title bar changes to indicate this, which looks unprofessional.

Using an undecorated JFrame, both of the above problems occurred

I do not wish to use a JInternalFrame as that would require a complete redesign of my interface (switching to the JDesktopPane structure), and I do not require any of the other functionality of the JInternalFrame.

Any ideas?

A: 

You can use a JPopupMenu for this.

popupMenu = new JPopupMenu();

Action showPopupAction = new AbstractAction("Show Popup") {
  public void actionPerformed(ActionEvent e) {
    AbstractButton btn = (AbstractButton)e.getSource();
    Point buttonXY = btn.getLocationOnScreen();
    popupMenu.setLocation((int) buttonXY.getX(), ((int) buttonXY.getY()) + btn.getHeight() + 2);
    popupMenu.setVisible(true);
  }
};

JButton btn = new JButton(showPopupAction);

EDIT: An alternative to using a full JPopupMenu is to create a Popup that references your Component, which would require less refactoring; e.g.

Component myMnu = ...
Popup popup = new Popup(null, myMnu, 100, 100);
popup.show();

Other than that I don't think there's a "quick fix" to your problem: Per the Javadocs, a JWindow is a first class citizen of the desktop, which I imagine is why Solaris is adding a corresponding icon to the task bar.

Adamski
Thanks, this looks like it's worth looking into. However, I have already done all of the work for the popup behavior, and just wish to handle the small problem in the original question (if possible). I need to get a release out for this application in the next day or two, and need to postpone refactoring for now.
@Dan: See my most recent edit. Using a Popup directly will save you some refactoring as you can simply "inject" your JWindow into a Popup. You still need to code for when / where to spawn the pop-up but it's fairly simple (as per my original example).
Adamski