views:

124

answers:

3

Hello,

I'm currently building a Java Swing GUI and I was wondering how user (mouse, say) driven resizing is handled.

My problem is that when I try and resize my main window, or when some other window opens over my application, then my application's window gets distorted - all it's parts don't change well in response to the resizing or the other window. Does anyone have a diagnostic on where to start looking at this? Follow-ups questions, resources are also appreciated.

Cheers.

+1  A: 

Add a ComponentListener to handle the componentResized() event, ie

frame.addComponentListener(new ComponentListener() {
    public void componentResized(ComponentEvent e) {       
        // resize other components, refresh, etc 
    }
}

From JavaDoc:

When the component's size, location, or visibility changes, the relevant method in the listener object is invoked, and the ComponentEvent is passed to it. Component events are provided for notification purposes ONLY; The AWT will automatically handle component moves and resizes internally so that GUI layout works properly regardless of whether a program registers a ComponentListener or not.

See http://download.oracle.com/javase/1.4.2/docs/api/java/awt/event/ComponentListener.html

rob
So if I just repaint my overall frame - does that automatically repaint all it's components?
sparkFinder
+1  A: 

While Rob is correct that a ComponentListener will give you a callback for when the panel is resized, you should not have to use that in order to make sure your GUI looks correct. Instead you should ensure that you have configured your LayoutManager appropriately to handle being resized. It can be a real pain in the neck to get resizing to work appropriately with the standard Layouts; I would highly recommend you check out MigLayout.

I82Much
+1  A: 

My problem is that when I try and resize my main window, or when some other window opens over my application, then my application's window gets distorted

Your window should NOT get distorted when another application opens over top of your application. Swing is smart enough to repaint the frame as required. If it is not repainting properly then I would guess you have added custom painting code and are not doing it properly. The only think I can suggest is to NEVER override the paint() method of a frame.

For more help you need to post your SSCCE that demonstrates the problem.

camickr