Is this your first time here? SwingWiki is a Java Swing Developer community site with an big archive of Swing-related usenet groups and mailing lists, but also tips, tricks and articles and book reviews written by your colleagues from around the world. If you came here through a search engine and did not find what you were looking for, make sure to check the wiki table of contents.

Mixing fixed positioning and automatic layouts

Automatic layout management is one of the best features of Swing. However, sometimes it is important to precisely position certain elements to align them with a background image etc. You can do it with setBounds() method for each component, but remember to set the layout manager of the container to null.

If you want to mix absolute positioning and automatic layout management in the same container, you can create a panel without a layout manager, put absolutely positioned components in it, and then put that panel into a container, with a layout manager, that will take care about remaining components. However, unexpected results are shown on the screen (at least in J2SE 1.4) if a sub-container of an automatically managed container does not have a layout manager. To avoid messing up the layout management of parent containers, but enable absolute positioning of some components, create a dummy layout manager that positions components on absolute coordinates.

This way, you get the best of both worlds - use automatic layout management for most components, and manually position the required ones.

When adding components to a panel with this layout manager, use a Point instance as the second argument to add(JComponent component,Object constraints) method of the panel.

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.LayoutManager2;
import java.awt.Point;
import java.util.*;
 

public final class FixedPositionLayoutManager implements LayoutManager2 {
	private HashMap components=new HashMap();
	
	public void addLayoutComponent(Component comp, Object constraints) {
		if (! (constraints instanceof Point)) throw new UnsupportedOperationException ("Use point as constraint");
		components.put(comp, constraints);
	}
	public float getLayoutAlignmentX(Container target) {
			return (float)0.5;
	}
	public float getLayoutAlignmentY(Container target) {
		return (float)0.5;
	}
	public void invalidateLayout(Container target) {
	}
	public Dimension maximumLayoutSize(Container target) {
		return target.getMaximumSize();
	}
	public void removeLayoutComponent(Component comp) {
			components.remove(comp);
	}
        public void setPosition(Component c, Point position){
    	   components.put(c,position);
        }
	public void layoutContainer(Container parent) {
			///
		for (Iterator i=components.keySet().iterator(); i.hasNext(); ){
			Component cp=(Component) i.next();
			Point  origin=(Point) components.get(cp);
			cp.setLocation(origin.x, origin.y);
			cp.setSize(cp.getPreferredSize());
		}
		
	}
 
	public void addLayoutComponent(String name, Component comp) {
			throw new UnsupportedOperationException ("Use constraints!");
	}
 
	public Dimension minimumLayoutSize(Container parent) {
		return parent.getMinimumSize();
	}
 
	public Dimension preferredLayoutSize(Container parent) {
		return parent.getPreferredSize();
	}
 
}
 

Comments? Corrections? Contact us or Login to edit pages directly (registration is free and takes less than displaying a JLabel)
  workaround/fixed_position_layout.txt · Last modified: 2005/03/06 06:12
 
Recent changes | RSS changes | Table of contents | News Archive | Terms And Conditions | Register | The Quest For Software++| Ruby Resources

Sedo - Buy and Sell Domain Names and Websites project info: swingwiki.org Statistics for project swingwiki.org etracker� web controlling instead of log file analysis