Layout Question

Hi,

I am here again with a basic question. I am using a gridlayout for my application. I am dividing my window in two part. It is dividing the window in two equal parts, But my requirment is to divide window in 1:3 proportion. How can this be achieved in grid layout or in any other layout. Plz help.........

[321 byte] By [Yogesh_Yadava] at [2007-10-2 2:04:09]
# 1
I don't believe any of the LayoutManagers will guarantee a 1:3 proportion.Try using a [url http://java.sun.com/docs/books/tutorial/uiswing/layout/visual.html]Box Layout[/url]. If the panels added are in a 1:3 proportion then it will resize in roughly the same proportion.
camickra at 2007-7-15 19:45:33 > top of Java-index,Desktop,Core GUI APIs...
# 2

this might get you close enough(?)

import java.awt.*;

import javax.swing.*;

class Testing extends JFrame

{

public Testing()

{

setSize(400,400);

setLocation(300,100);

setDefaultCloseOperation(EXIT_ON_CLOSE);

getContentPane().setLayout(new GridBagLayout());

GridBagConstraints gbc = new GridBagConstraints();

gbc.weightx = .5;

gbc.weighty = .25;

gbc.fill = GridBagConstraints.BOTH;

gbc.gridx = 0;

gbc.gridy = 0;

JPanel panelA = new JPanel();

panelA.setBorder(BorderFactory.createLineBorder(Color.BLACK));

getContentPane().add(panelA,gbc);

JPanel panelB = new JPanel();

panelB.setBorder(BorderFactory.createLineBorder(Color.BLACK));

gbc.weighty = .75;

gbc.gridy++;

getContentPane().add(panelB,gbc);

}

public static void main(String[] args){new Testing().setVisible(true);}

}

Michael_Dunna at 2007-7-15 19:45:33 > top of Java-index,Desktop,Core GUI APIs...
# 3
Hi, Thanks for fast reply. 1:3 mean that i want to divide the window in two parts one part should be small and other should be large but not same as the case i am facing with GridLayout. I hope now my question is more clear to you. So, any suggestions now.............
Yogesh_Yadava at 2007-7-15 19:45:33 > top of Java-index,Desktop,Core GUI APIs...
# 4
> So, any suggestions now.............Yes, read the tutorial on Using Layout Mangers and experiment until you get the desired effect.
camickra at 2007-7-15 19:45:33 > top of Java-index,Desktop,Core GUI APIs...
# 5

Set this layout on your container and do:

container.add(comp1, new PanelBounds(0.0, 0.0, 0.333, 1.0));

container.add(comp2, new PanelBounds(0.333, 0.0, 0.666, 1.0));

/**

* Layout manager for components which wish to be sized as a fraction of the size

* of their parent container.

*

* Containers using this layout must have components added using the

* <code>add(Component, Object)</code> method where the <code>Object</code>

* argument must be a <code>PanelBounds</code> object.

* The <code>PanelBounds</code> argument represents a rectangle of area covered

* by the <code>Component</code> to be added. The size of the rectangle should

* represent a fraction of the size of the parent <code>Container</code> where

* a rectangle of width = 1.0 and height = 1.0 would be the same size as the

* parent.

* E.g. If you wished to add a button to the whole of the bottom half of a container,

* you would do this: container.add(button, new PanelBounds(0.0, 0.5, 1.0, 0.5));

*

*

*

*

*

* @author Jim Morrison

* @version $Revision: 1.2 $

*/

public class FractionalLayout implements LayoutManager2

{

private Map components = new HashMap();

/**

* Creates a new <code>FractionalLayout</code>.

*/

public FractionalLayout()

{

// empty constructor

}

/**

* Adds the specified component to the layout, using the specified

* constraint object.

* @param comp the component to be added

* @param constraints where/how the component is added to the layout.

*/

public void addLayoutComponent( Component comp, Object constraints )

{

synchronized(comp.getTreeLock())

{

if( constraints == null || !( constraints instanceof PanelBounds ) )

{

throw new IllegalArgumentException("cannot add to layout: constraint must be a PanelBounds"); //$NON-NLS-1$

}

components.put( constraints, comp );

}

}

/**

* @see java.awt.LayoutManager2#maximumLayoutSize(java.awt.Container)

*/

public Dimension maximumLayoutSize( Container target )

{

return new Dimension( Integer.MAX_VALUE, Integer.MAX_VALUE );

}

/**

* @see java.awt.LayoutManager2#getLayoutAlignmentX(java.awt.Container)

*/

public float getLayoutAlignmentX( Container target )

{

return 0.5f;

}

/**

* @see java.awt.LayoutManager2#getLayoutAlignmentY(java.awt.Container)

*/

public float getLayoutAlignmentY( Container target )

{

return 0.5f;

}

/**

* @see java.awt.LayoutManager2#invalidateLayout(java.awt.Container)

*/

public void invalidateLayout( Container target )

{

// do nothing

}

/**

* This does nothing since constraints are required.

*

* @see java.awt.LayoutManager#addLayoutComponent(java.lang.String, java.awt.Component)

*/

public void addLayoutComponent( String name, Component comp )

{

// do nothing

}

/**

* Removes the specified component from the layout.

* @param comp the component to be removed

*/

public void removeLayoutComponent( Component comp )

{

components.remove( comp );

}

/**

* @see java.awt.LayoutManager#preferredLayoutSize(java.awt.Container)

*/

public Dimension preferredLayoutSize( Container parent )

{

return parent.getPreferredSize();

}

/**

* @see java.awt.LayoutManager#minimumLayoutSize(java.awt.Container)

*/

public Dimension minimumLayoutSize( Container parent )

{

return new Dimension(50, 50);

}

/**

* Lays out the specified container.

* @param parent the component which needs to be laid out

*/

public void layoutContainer( Container parent )

{

synchronized( parent.getTreeLock() )

{

Insets insets = parent.getInsets();

if( components.size() == 0 )

{

return;

}

Dimension size = parent.getSize();

int totalW = size.width - ( insets.left + insets.right );

int totalH = size.height - ( insets.top + insets.bottom );

Iterator keys = components.keySet().iterator();

while( keys.hasNext() )

{

PanelBounds bounds = (PanelBounds) keys.next();

Component comp = (Component) components.get( bounds );

if( bounds != null )

{

int x = (int) ( insets.left + ( totalW * bounds.x ) );

int y = (int) ( insets.top + ( totalH * bounds.y ) );

int width = (int) ( insets.left + ( totalW * bounds.width ) );

int height = (int) ( insets.top + ( totalH * bounds.height ) );

comp.setBounds( x, y, width, height );

}

else

{

comp.setSize( 0, 0 );

}

}

}

}

}

public class PanelBounds extends Rectangle2D.Double

{

/**

* Constructs a new PanelBounds, initialized to location (0,?) and size (0,?).

*/

public PanelBounds()

{

}

/**

* Constructs and initializes a PanelBounds object from the specified double coordinates.

* @param x,爕 the coordinates of the upper left corner

* of the newly constructed PanelBounds

* @param width the width of the newly constructed PanelBounds

* @param height the height of the newly constructed PanelBounds

*/

public PanelBounds(double x, double y, double width, double height)

{

super(x, y, width, height);

}

/**

* Sets the x position of the upper left corner.

* @param x The x to set.

*/

public void setX(double x)

{

this.x = x;

}

/**

* Sets the y position of the upper left corner.

* @param y The y to set.

*/

public void setY(double y)

{

this.y = y;

}

/**

* Sets the width.

* @param width The width to set.

*/

public void setWidth(double width)

{

this.width = width;

}

/**

* Sets the height.

* @param height The height to set.

*/

public void setHeight(double height)

{

this.height = height;

}

//public String toString()

//{

//return

//}

}

Cheers,

Jim

patumairea at 2007-7-15 19:45:33 > top of Java-index,Desktop,Core GUI APIs...