setLayout() after JFrame resized

  • Thread starter Richard A. DeVenezia
  • Start date
R

Richard A. DeVenezia

I have a JFrame with two panels. The panels should be either side by side
or one atop the other, depending on the aspect of the frame. The panels
will be displaying a square figure, so I want to choose the layout that
gives me the biggest square.

My problem is that when my 'do-when-resized' logic dictates doing a
getContentPane().setLayout(layout);
the layout of the panels does not occur. (It does occur if I resize the
frame again; somehow the layout is always 'one-step-behind')

some detail

In the frame constructor I install a resize handler

addComponentListener(new ComponentAdapter(){
public void componentResized(ComponentEvent e) {
frameWasResized(e); }
});

and the handler is thus

private final GridLayout LEFT_RIGHT = new java.awt.GridLayout(1, 2, 3,
3);
private final GridLayout TOP_BOTTOM = new java.awt.GridLayout(2, 1, 3,
3);
private GridLayout layout = null;

void frameWasResized(ComponentEvent e) {
int w = getWidth();
int h = getHeight();

setTitle ("Snowflake "+w+"x"+h);

int minDim1 = (w/2 > h) ? h : w/2;
int minDim2 = (w > h/2) ? h/2 : w;

GridLayout grid;

if (minDim1 >= minDim2) {
grid = LEFT_RIGHT;
} else {
grid = TOP_BOTTOM;
}

System.out.println(w+"x"+h+" "+minDim1+" "+minDim2);

if (grid != layout) {
System.out.println("Layout should change");
layout = grid;
getContentPane().setLayout(layout);
// getContentPane().repaint(); // this didn't seem to make things
'catch up'
}
}
 
A

Andrew Thompson

...
| getContentPane().setLayout(layout);
| // getContentPane().repaint(); // this didn't seem to
make things
| 'catch up'

try a call to.. invalidate()

HTH
 
R

Richard A. DeVenezia

Ike said:
I believe you want Frame.pack();

-Ike

Thanks for the ideas.

pack() worked, but set everything to preferred size, which I didn't want.
invalidate() didn't seem to do anything.

I ended up doing:

getContentPane().setLayout(layout);
getContentPane().doLayout();
 
J

Jon A. Cruz

Richard said:
I have a JFrame with two panels. The panels should be either side by side
or one atop the other, depending on the aspect of the frame. The panels
will be displaying a square figure, so I want to choose the layout that
gives me the biggest square.

My problem is that when my 'do-when-resized' logic dictates doing a
getContentPane().setLayout(layout);
the layout of the panels does not occur. (It does occur if I resize the
frame again; somehow the layout is always 'one-step-behind')

Although the other posts here had good advice on details, they don't
quite address your problem as a whole.


In general, you should try a completely different approach.


What you describe seems as if it would be better solved by just creating
a custom layout manager yourself that does exactly what you want.

The advantages include proper integration with sizing and display, and
removing the need for all the hacks you're going through now.

Creating a custom layout manager is not that hard at all, and will
probably even be much less code than you're ending up with once you
finish all the 'fixes' needed.

http://java.sun.com/docs/books/tutorial/uiswing/layout/custom.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top