1

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 ?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
whiteElephant
  • 263
  • 1
  • 6
  • 21

2 Answers2

5

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.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • plus 1 to for being a GC and giving the same answer. I sore it pop up as well. Thanks – whiteElephant May 20 '13 at 03:24
  • Hi Andrew, just a quick question. Do you know how I would be able to insert into the same grids again from another method? So at the moment I have the items names but latter on I would like to add some more information below the names.. I have made the variable static and tried centerPanel.add(new JLabel"Test") ; andcenterPanel.add(new JLabel"Test",1) just to see if I could and they didn't work. First add simply added a fourth column.. Any ideas? Edit: I guess one way could be to add 3 JPanel Properties add those to the grid, then edit those... – whiteElephant May 20 '13 at 03:48
  • *"just a quick question."* That's (part of) what SO is about. Why not ask a new question, and add the [tag:static] tag? – Andrew Thompson May 20 '13 at 03:51
2

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.

camickr
  • 321,443
  • 19
  • 166
  • 288