Re: LayoutManager2.layoutContainer: "wrong height"?



Roedy Green wrote:
On Wed, 26 Oct 2005 17:00:04 +0200, Rainer Schwarze
<rsc.nospam@xxxxxxxxxxxxxx> 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

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) {	}
	}
}

--
Rainer Schwarze (Mr.) -- remove .nospam for email
.


Quantcast