Java Forum / GUI / October 2005
LayoutManager2.layoutContainer: "wrong height"?
Rainer Schwarze - 26 Oct 2005 14:12 GMT Hi,
I have run into a strange problem with my custom LayoutManager. The problem boils down to that one:
I call pack on the JWindow. The LayoutManager returns a preferred size. Upon the first call to layoutContainer, the size of the parent is different from the preferredSize. Where does the "wrong" size come from?
The sample output from my test code is:
LM: preferredLayoutSize = java.awt.Dimension[width=20,height=20] LM: layoutContainer: parent.size = java.awt.Dimension[width=20,height=19] btn bounds = java.awt.Rectangle[x=0,y=0,width=20,height=19]
You can see, that the height=20 turns into a height=19. So when I do nothing except calling pack(), I thought that the JWindow should live well with the preferred size returned by the layout manager, but instead it chooses to use a different height... Code follows below.
Can someone help me and give explanations, why this happens, how to correct it, or simply that I have to live with it?
Thanks & best wishes, Rainer
public class LayoutTest { public void run() { JWindow window; window = new JWindow(); LM lm = new LM(); lm.prefSize = new Dimension(20, 20); window.setLayout(lm); JButton btn = new JButton("test"); window.getContentPane().add(btn); window.pack(); System.out.println("btn bounds = " + btn.getBounds()); }
public static void main(String[] args) { LayoutTest lt = new LayoutTest(); lt.run(); System.exit(0); }
public static class LM implements LayoutManager2 { public Dimension prefSize;
public Dimension preferredLayoutSize(Container parent) { Insets insets = parent.getInsets(); Dimension d = new Dimension(); d.setSize( prefSize.width + insets.left + insets.right, prefSize.height + insets.top + insets.bottom); System.out.println("LM: preferredLayoutSize = " + d); return d; }
public Dimension minimumLayoutSize(Container parent) { return null; }
public void layoutContainer(Container parent) { System.out.println( "LM: layoutContainer: parent.size = " + parent.getSize()); Insets insets = parent.getInsets(); Rectangle r = new Rectangle( insets.left, insets.top, parent.getWidth() - insets.left - insets.right, parent.getHeight() - insets.top - insets.bottom); for (Component c : parent.getComponents()) { c.setBounds(r); } }
// we don't need them: public void addLayoutComponent(Component comp, Object constraints) { } public Dimension maximumLayoutSize(Container target) { return null; } public float getLayoutAlignmentX(Container target) { return 0; } public float getLayoutAlignmentY(Container target) { return 0; } public void invalidateLayout(Container target) { } public void addLayoutComponent(String name, Component comp) { } public void removeLayoutComponent(Component comp) { } } }
 Signature Rainer Schwarze (Mr.) -- remove .nospam for email
Andrew Thompson - 26 Oct 2005 15:20 GMT > The sample output from my test code is: > [quoted text clipped - 4 lines] > btn bounds = > java.awt.Rectangle[x=0,y=0,width=20,height=19] I ran your code against 1.5.0-beta (closest VM within reach) and got different results..
LM: preferredLayoutSize = java.awt.Dimension[width=20,height=20] LM: layoutContainer: parent.size = java.awt.Dimension[width=20,height=20] btn bounds = java.awt.Rectangle[x=0,y=0,width=20,height=20]
What Java version are you running on the desktop?
Rainer Schwarze - 26 Oct 2005 16:00 GMT >> The sample output from my test code is: >> [quoted text clipped - 13 lines] > > What Java version are you running on the desktop? Sorry - exactly that information popped up in my mind before and after posting, but not right when I posted :-( Here we go:
Java: Standard Edition (build 1.5.0_01-b08) Windows: 2000 SP4
I use Eclipse 3.1 and now ran the code directly under the JRE, but that was not the problem. (I had problems with Eclipse using the JCE which disappeared with the plain JRE.)
I compiled and ran the code (with modifications to the for(:) construct) under a JDK 1.4.1_01 which also happens to be on my machine, but it produces the same results.
Rainer
 Signature Rainer Schwarze (Mr.) -- remove .nospam for email
Roedy Green - 27 Oct 2005 01:02 GMT On Wed, 26 Oct 2005 17:00:04 +0200, Rainer Schwarze <rsc.nospam@nospam.q-17.de> wrote, quoted or indirectly quoted someone who said :
>Java: Standard Edition (build 1.5.0_01-b08) >Windows: 2000 SP4 there have been 4 bug fix releases since then. One simple thing you can do is upgrade your JVM and see if that fixes the problem.
see http://mindprod.com/jgloss/jre.html http://mindprod.com/jgloss/jdk.html
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
Rainer Schwarze - 27 Oct 2005 10:07 GMT > On Wed, 26 Oct 2005 17:00:04 +0200, Rainer Schwarze > <rsc.nospam@nospam.q-17.de> wrote, quoted or indirectly quoted someone [quoted text clipped - 8 lines] > see http://mindprod.com/jgloss/jre.html > http://mindprod.com/jgloss/jdk.html Funny - I already had the update on my hard disk, but simply did not switch to it... Well - thanks for the hint. That solved it.
In case anybody wishes to collect some more cases, I cleaned up the code a little bit - it now also compiles on versions before 1.5. The cases I could check follow below together with the source code.
For now it seems like I'd like to check whether the given size differs by one from the preferred size and stick to the preferred size then...
Best wishes, Rainer
/*----- test cases output ------------- */
actual height (19.0) / pref height (20.0) actual width (20.0) / pref width (20.0) Fail / Environment: jre=1.4.1_01, vm=1.4.1_01-b01, os=Windows 2000, osver=5.0
actual height (19.0) / pref height (20.0) actual width (20.0) / pref width (20.0) Fail / Environment: jre=1.5.0_01, vm=1.5.0_01-b08, os=Windows 2000, osver=5.0
actual height (20.0) / pref height (20.0) actual width (20.0) / pref width (20.0) OK / Environment: jre=1.4.2_03, vm=1.4.2_03-b02, os=Linux, osver=2.6.4-52-default
actual height (20.0) / pref height (20.0) actual width (20.0) / pref width (20.0) OK / Environment: jre=1.5.0_05, vm=1.5.0_05-b05, os=Windows 2000, osver=5.0
/* -------------- code ---------------- */
import java.awt.Component; import java.awt.Container; import java.awt.Dimension; import java.awt.Insets; import java.awt.LayoutManager2; import java.awt.Rectangle;
import javax.swing.JButton; import javax.swing.JWindow;
public class LayoutTest { public static void main(String[] args) { LayoutTest lt = new LayoutTest(); lt.run(); System.exit(0); }
public void run() { JWindow window = new JWindow(); LM lm = new LM(); lm.prefSize = new Dimension(20, 20); window.getContentPane().setLayout(lm); window.getContentPane().add(new JButton("test")); window.pack();
boolean ok = true; if (lm.paramSize.getHeight()!=lm.prefSize.getHeight()) { ok = false; } if (lm.paramSize.getWidth()!=lm.prefSize.getWidth()) { ok = false; } System.out.println( "actual height (" + lm.paramSize.getHeight() + ")" + " / pref height (" + lm.prefSize.getHeight() + ")"); System.out.println( "actual width (" + lm.paramSize.getWidth() + ")" + " / pref width (" + lm.prefSize.getWidth() + ")");
String res = ""; res += (ok) ? "OK" : "Fail"; res += " / Environment: "; res += "jre=" + System.getProperty("java.version", "(?)"); res += ", vm=" + System.getProperty("java.vm.version", "(?)"); res += ", os=" + System.getProperty("os.name", "(?)"); res += ", osver=" + System.getProperty("os.version", "(?)"); System.out.println(res); }
public static class LM implements LayoutManager2 { public Dimension prefSize; public Dimension paramSize;
public Dimension preferredLayoutSize(Container parent) { Insets insets = parent.getInsets(); Dimension d = new Dimension(); d.setSize( prefSize.width + insets.left + insets.right, prefSize.height + insets.top + insets.bottom); return d; }
public void layoutContainer(Container parent) { if (paramSize==null && parent.getSize().getWidth()!=0 && parent.getSize().getHeight()!=0) { /* store the first useful size information: */ paramSize = new Dimension(parent.getSize()); } /* simply set all components to the 'net-size' of the parent: */ Insets insets = parent.getInsets(); Rectangle r = new Rectangle( insets.left, insets.top, parent.getWidth() - insets.left - insets.right, parent.getHeight() - insets.top - insets.bottom); for (int i = 0; i < parent.getComponentCount(); i++) { Component c = parent.getComponent(i); c.setBounds(r); } }
// we don't need them: public Dimension minimumLayoutSize(Container parent) { return null; } public Dimension maximumLayoutSize(Container target) { return null; } public float getLayoutAlignmentX(Container target) { return 0; } public float getLayoutAlignmentY(Container target) { return 0; } public void invalidateLayout(Container target) { } public void addLayoutComponent(Component comp, Object constraints) { } public void addLayoutComponent(String name, Component comp) { } public void removeLayoutComponent(Component comp) { } } }
 Signature Rainer Schwarze (Mr.) -- remove .nospam for email
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|