Reputation: 263
I am adding objects dynamically to panels then adding to the my border layout. I need the West, Center and East all to be equal size. This is what I have so far:
static int width = 300;//Screen width
static int height = width / 16 * 9;//Screen height
static int scale = 3;
private JFrame frame;
//player is an object from a Player Class will an arrayList of Items..
public void LoadPlayer(Player player){
int count = 1;
for (Items i : player.getAllItems()){
JPanel jp= new JPanel();
JLabel jlItem= new JLabel(i.getName());
BorderLayout bl = (BorderLayout) (mainPanel.getLayout()) ;
jp.add(jlItem);
jp.setBorder(BorderFactory.createLineBorder(Color.BLACK));
if (bl.getLayoutComponent(BorderLayout.WEST) == null){
mainPanel.add(jp,BorderLayout.WEST);
jp.setSize(frame.getWidth()/3, height);
System.out.println("adding item " + count+" to west panel");
}
else if (bl.getLayoutComponent(BorderLayout.CENTER) == null){
jp.setSize(frame.getWidth()/3, height);
mainPanel.add(jp,BorderLayout.CENTER);
System.out.println("adding item " + count+" to center panel");
}
else if (bl.getLayoutComponent(BorderLayout.EAST) == null){
mainPanel.add(jp.BorderLayout.EAST);
jp.setSize(frame.getWidth()/3, height);
System.out.println("adding item" + count+" to east panel");
}
count++;
}
}
I was hopeful this would work but it didn't. I've done a bit of searching and can't seem to find anything that says you can or can't set the size of the WEST
, CENTER
and EAST
panels.
Does anyone know how to do this ?
Upvotes: 1
Views: 5354
Reputation: 324197
You never use the setSize() method on a component. That is the job of the layout manager.
If you want the panels to be the same size you can use a GridLayout.
Upvotes: 2
Reputation: 168845
I need the West, Center and East all to be equal size..
A single row GridLayout
in the BorderLayout.CENTER
of a nested layout will achieve that.
Upvotes: 5