How to detect mouse moving outside Java Popup? - java

I'm creating the following UI in Java.
When a user hovers their mouse over a particular area on the screen, a popup appears that contains a bunch of buttons and controls.
PopupFactory factory = PopupFactory.getSharedInstance();
_hoverPanel = factory.getPopup( parent, panel, x, y );
_hoverPanel.show();
I want this panel to remain visible on the screen while the user interacts with the panel's components, but as soon as the users mouse exists the popup bounds I want the popup to hide.
I tried adding a mouselistener to the panel inside the popup but I noticed that mouseEntered only fire when I enter the panel (and not when I enter components found inside the panel) and mouseExited only fires when I exit the panel.
The problem here is that I can make the popup appear, but as soon as I move my mouse inside the panel, and then over top of a component inside the panel, mouseExited fires for the panel, and the popup hides :(
I can also move my mouse quickly inside the panel over top of a component, and mouseEntered never fires for the panel :(
How can I detect when the mouse goes inside and outside my popup panel?

I had a similar problem (catching all mouse events while the mouse hovers over my dialog) and I solved it by wrapping the root pane of the dialog in a JLayer (javax.swing.JLayer) as shown in http://docs.oracle.com/javase/tutorial/uiswing/misc/jlayer.html#events

Related

Resize java swing component from UI

I need to make resizable java swing component (JPanel), so when I click on the border of that component and drag with cursor to somewhere else, the component will change size by mouse position. Has this any easy solution?
Attach a MouseListener to the component, record mouse position at start of dragging in mouse pressed method after checking whether the mouse is pressed on what you have defined as border, then resize the component in mouse released method calculating the size difference by subtracting mouse positions at start and end of dragging.
If you need / want continuous resizing while you drag, use a MouseInputListener instead, record the size of the component too in the mouse pressed method and do the resizing in the mouse moved method.
If you also want to have a visible border, you can maybe add that border to the component and add the mouse listener to the border, so that you know you are on the border when you get the pressed event.

How can I get this imported MouseMotionListener to work again?

I made a JPanel with a mouse listener, a mouse motion listener and a scroll listener. And I have a JFrame that switches between JPanels like so:
//tile is a JPanel
//"this" is a JFrame
tile.removeAll();
tile=tree.getCurrentNode().getContent().panel;
this.add(tile);
this.revalidate();
tile.requestFocus();
Now when I switch out the old JPanel for the one with mouse listeners some weird things start to happen. The mouse listener and the scroll listener still work but the mouse motion listener doesn't get called anymore. I even get a nullpointer exception when I try getMousePosition() from within the JPanel.

Opening a new panel by just clicking a portion of the panel

I am using Net beans drag and drop GUI and in that i am displaying a chart in a jPanel and i want that when i click a portion of that chart then another panel opens up.I know we can write an event , when we click a whole panel(anywhere in panel) but i just want that when i click the small potion of that chart only then another panel opens up, how is this possible?
Attach a MouseListener to the panel. Detect the events, take action. See How to Write a Mouse Listener for more details.

Find the mouse position in a JFrame

I'm stuck with the following.
I'm programming a game and I need to get the mouse pointer location inside the JFrame, I don't need to get the pointer location on the screen, but just in the JFrame. When you use the MouseClick event you could get the position in the frame while the mouse button is pressed, but I want to get it's location when no button is pressed.
I hope you understood my question.
Sounds like you need a MouseMotionListener.
The MouseEvent contains the current mouse position.
void mouseMoved(MouseEvent e)
Invoked when the mouse cursor has been moved onto a component but no buttons have been pushed.

Java Glass pane

I have a problem. I want to ask you how can I implement GlassPane to paint on it.
I mean, if I click mouse button, in mouseClicked event, my transparent glass pane should be created, because I want to see all my components behind glassPane and I can paint on it using mouseDragged event. When I release mouse button my glassPane disappears.
I have another question too. When I will paint on glass pane all my components behind them will be refreshing and repainting? Maybe somebody have nice example with glass pane which might help me.
How to Create Translucent and Shaped Windows

Categories