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'
}
}
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'
}
}