0

I have a page with objects on it. The object locations are defined by Rectangle2D.Double. When I move the mouse I check whether or not the a rectangle contains the mouse point. If yes I want to show a hover-over menu of the object (so far only text).

What I have in the constructor is this

annnotationPopupMenu = new JPopupMenu();
annotationTextArea = new JTextArea(5,20);
annotationTextArea.setLineWrap(true);
annnotationPopupMenu.add(annotationTextArea);

and if I'm over an object I run this

annotationTextArea.setText(annotation.getContent());
annnotationPopupMenu.setLocation(MouseInfo.getPointerInfo().getLocation().x+30, MouseInfo.getPointerInfo().getLocation().y);
annnotationPopupMenu.setVisible(true);

If I leave the object

annnotationPopupMenu.setVisible(false);

is run. The problem I have now is that when I tab out of the application and the mouse was over an object the JPopupMenu is still visible even though the rest of the application is now behind the window I tabbed to.

What do I need to do that the JPopup menu will either go invisible or at least does not have priority over the window I tabbed to.

Below a MWE which just opens a PopupMenu which stays on top even alt-tabbing:

package popupmenutest;

import javax.swing.JPopupMenu;
import javax.swing.JTextArea;

public class PopupMenuTest
{

    public static void main(String[] args)
    {
    JPopupMenu annotationPopupMenu = new JPopupMenu();
    JTextArea annotationTextArea = new JTextArea(5,20);
    annotationTextArea.setLineWrap(true);
    annotationPopupMenu.setLocation(500, 400);
    annotationPopupMenu.add(annotationTextArea);
    annotationPopupMenu.setVisible(true);
    }
}
Philipp
  • 2,376
  • 5
  • 29
  • 47
  • Please edit your question to include a [mcve] that exhibits the problem you describe and identifies the platform affected. – trashgod Apr 12 '16 at 11:06
  • Are you using `setComponentPopupMenu()`, for [example](http://stackoverflow.com/a/5129757/230513). – trashgod Apr 12 '16 at 11:13
  • Have you considered making use of the pre-existing tooltip functionality? – MadProgrammer Apr 12 '16 at 11:34
  • But the object I want to call the 'Tooltip' on is no swing object, it is a class with a rectangle and some other non swing related members. How would I use the tooltip functionality on an arbitrary object? – Philipp Apr 12 '16 at 11:37
  • So? Is it backed by a Swing component like a `JPanel` or `JComponent`? Even if you're using custom painting, you can still use tooltips. [This](http://stackoverflow.com/questions/31130938/get-tooltip-from-jcombobox-renderer/31131215#31131215) example shows you how to customise the text of the tooltip based on the mouse's location and [this](http://stackoverflow.com/questions/31238676/tooltip-position-for-cell-in-jtable/31238769#31238769) shows how you can set the location of the tooltip – MadProgrammer Apr 12 '16 at 11:40
  • It takes place in a class that extens JComponent. Could I call the tooltip on the JComponent given the position of the annotation object? – Philipp Apr 12 '16 at 11:58
  • In theory, yes. Just remember, all the coordinates will be in the `JComponent's coordinate space, so you might need to do some translations – MadProgrammer Apr 12 '16 at 12:03
  • There is at least one problem with the tooltip: When the text I add to the tooltip is long then I have just one long single lined tooltip that maybe even goes beyond the screen. I've been looking and manual line break solutions but they all look ugly. – Philipp Apr 15 '16 at 08:32

1 Answers1

0

I fixed this problem by adding the top frame to the WindowFocusListener and then on windowLostFocus I call the particular pane and set the visibility of the JPopupMenu to false.

Something like this:

package popupmenutest;

import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JFrame;

import java.awt.Dimension;
import java.awt.MouseInfo;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;

public class PopupMenuTest implements WindowFocusListener
{

    public PopupMenuTest()
    {

        frame = new JFrame();
        frame.setSize(800, 600);
        popupMenu = new JPopupMenu();
        JTextArea annotationTextArea = new JTextArea(5,20);
        JScrollPane scrollPane = new JScrollPane(annotationTextArea); 
        popupMenu.setPreferredSize(new Dimension(300, 120));
        annotationTextArea.setText("Dies hier ist ein Blindtext zum Testen von Textausgaben. Wer diesen Text liest, ist selbst schuld. Der Text gibt lediglich den Grauwert der Schrift an. Ist das wirklich so? "
            + " Ist es gleichgültig, ob ich schreibe: Dies ist ein Blindtext oder Huardest gefburn? Kjift – mitnichten! Ein Blindtext bietet mir wichtige Informationen. An ihm messe ich die Lesbarkeit einer Schrift, "
            + "ihre Anmutung, wie harmonisch ");

        annotationTextArea.setLineWrap(true);
        annotationTextArea.setWrapStyleWord(true);
        annotationTextArea.invalidate();
        scrollPane.setVisible(true);
        popupMenu.setLocation(MouseInfo.getPointerInfo().getLocation().x, MouseInfo.getPointerInfo().getLocation().y);
        popupMenu.add(scrollPane);
        popupMenu.pack();
        frame.add(popupMenu);
        frame.addWindowFocusListener(this);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    @Override
    public void windowGainedFocus(WindowEvent e)
    {

    }

    @Override
    public void windowLostFocus(WindowEvent e)
    {
        popupMenu.setVisible(false);

    }
    private JPopupMenu popupMenu;
    private JFrame frame;
}
Philipp
  • 2,376
  • 5
  • 29
  • 47