Listen to JFrame resize events as the user drags their mouse?

You can add a component listener and implement the componentResized function like that:

JFrame component = new JFrame("My Frame");

component.addComponentListener(new ComponentAdapter() 
{  
        public void componentResized(ComponentEvent evt) {
            Component c = (Component)evt.getSource();
            //........
        }
});

EDIT: Apparently, for JFrame, the componentResized event is hooked to the mouseReleased event. That’s why the method is invoked when the mouse button is released.

One way to achieve what you want, is to add a JPanel that will cover the whole area of your JFrame. Then add the componentListener to the JPanel (componentResized for JPanel is called even while your mouse is still dragging). When your frame is resized, your panel will also be resized too.

I know, this isn’t the most elegant solution, but it works!

Leave a Comment