Reputation: 3529
My question is how can I specify size of the parts on my layout?
I need somehow set size of "parts" not using preferedSize, maybe in layout managers, doesn't matter where - only I need is stable size.
I want to create layout for game. I've already created one but I'm dealing with problem with size of components. So I considered that it would be better to make better concept of my layout.
Let's look at my draft.
+-----------+ | UPPER | +-----+-----+ | A | | +-----+ C | | B | | +-----+-----+ | Footer | +-----------+ A+B+C make together Center.
Main part consist of this tree parts:
Upper- there will be menu.
Center - this consists of 3 parts A,B,C
Footer - there will be status bar
My idea is to be able to set the size of each component.
All layout is dependent on part C it could have size 450x450 px or 600x600 px.
For part A and B i need specify only the width, because there will be only some text info - it should be about 300 px.
I tryed to use GridBagLayout for Center part but setSize for C didn't worked well.
I make the parts in Containers (java.awt.Container) - in them I add the content of each part and then add the Container to the upper level.
Upvotes: 2
Views: 9400
Reputation: 1377
This answer is too little and WAY too late,
(maybe this method did not exist at the time of asking of this question)
just like getPreferredSize
, there is also a setPreferredSize
method which takes a Dimension
object.
By default, your layout will ignore your components sizes (which you may have set using setSize
), instead it will use the preferred sizes.
By using setPreferredSize
, you will be able to override the default preferred sizes of the component
Upvotes: 0
Reputation: 11
I had a similar problem, with a status tool bar at the bottom containing a number of other components. My problem was that it would get taller. So what I did was to override the maximum size setting the maximum height to be the minimum height.
JPanel status = new JPanel( new SpringLayout() ) {
@Override
public Dimension getMaximumSize() {
Dimension max = super.getMaximumSize();
Dimension min = getMinimumSize();
return new Dimension( max.width, min.height );
}
};
Upvotes: 1
Reputation: 161
I hope my answer can help you in some way. From experience with setting JPanel or JFrame size, I have always used setPreferredSize(new Dimension(WIDTH,HEIGHT));
Upvotes: -2
Reputation: 2626
In general, GridBagLayout ignores the values you set for controls with setSize
, instead it asks the controls for their preferred size (by calling getPreferredSize
) and uses that for calculating the overall layout. Simply setting that preferred size yourself is not recommended, since most controls tend to recalculate those values whenever a layout is triggered, so you will have a hard time getting them to "stick".
If you really want to make sure the UI element C has a certain size, implement it as a custom class deriving from a suitable base (JPanel, for example) and override the getPreferredSize
method to make it return the size you want/need for that part of your UI.
Edit: Here's a little example for a wrapper that can contain another UI element and can be set to a fixed size (using the setSize
method which has been overridden), which should be respected by layout managers:
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JComponent;
import javax.swing.JPanel;
public class FixedSizeComponent extends JPanel {
private Dimension size;
private final JComponent content;
public FixedSizeComponent(JComponent content) {
super(new BorderLayout());
this.content = content;
super.add(content, BorderLayout.CENTER);
}
@Override
public void setSize(Dimension d) {
size = d;
}
@Override
public void setSize(int width, int height) {
size = new Dimension(width, height);
}
@Override
public Dimension getSize() {
if (size != null) return size;
return content.getSize();
}
@Override
public Dimension getSize(Dimension rv) {
if (size != null) {
if (rv == null) rv = new Dimension();
rv.height = size.height;
rv.width = size.width;
return rv;
}
return content.getSize(rv);
}
@Override
public Dimension getPreferredSize() {
if (size != null) return size;
return content.getPreferredSize();
}
@Override
public Dimension getMaximumSize() {
if (size != null) return size;
return content.getMaximumSize();
}
@Override
public Dimension getMinimumSize() {
if (size != null) return size;
return content.getMinimumSize();
}
}
Upvotes: 2
Reputation: 201
The simplest way: use BorderLayout for the contentPane (which already is) - Upper panel goes to North - Footer panel goes to South - Panels A and B goes into a Panel ab with GridLayout(2,1) - Panel ab and C goes into a Panel abc with GridLayout(1,2) - Panel abc goes into the Center And setPrefferedSize() of your A, B, C
Upvotes: 2