So I’m trying to have a window that I can drag around by the title bar. Since it’s inside another frame I need to do this manually. This is what the code looks like for the mouse listener.
titleBar.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
currentMouseEvent = e;
dragging = true;
}
public void mouseReleased(MouseEvent e)
{
dragging = false;
currentMouseEvent = null;
}
});
And this is the code for the update loop. (This is in a while loop on another thread)
if (dragging)
{
try
{
setPosition(currentMouseEvent.getXOnScreen(), currentMouseEvent.getYOnScreen());
}
catch(NullPointerException e)
{
//do nothing
Main.SoundSystem.fatalError();
}
}
I know that the setPosition needs to be changed to track where the mouse is on the title bar, but I wanted to test this out. Problem is that it will move once, and then not again as I drag my mouse around. Why is that?
Because you don’t update
currentMouseEvent, you keep moving the window to the location where the mouse was pressed.You will need a
MouseMotionListenerto get new events during dragging.