• Post Reply Bookmark Topic Watch Topic
  • New Topic

layout managers and GUI

 
kenji mapes
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am developing a simple GUI

My derived class from from uses a BorderLayout. In the east, I create a panel to put in there. However, before hand, I create two panels to put into that Panel.

So the "east" panel is created, two more panels are created, and buttons are added to each of them, and they are added to said east panel. Then the east panel is added to the content pane.

However, my buttons get trunacted. Any chnages I do seem to change the CENTER panel as well which contains a JList.

Here's some of my code in question. (in testing phase and there is limited commenting and prog convention)

//create third JPanel

JPanel jpPanel3 = new JPanel();
jpPanel3.setLayout(new BorderLayout());
jpPanel3.setBackground(colorBrown);


//create JPanel to nest in third panel

JPanel jpPanel3a = new JPanel();
//jpPanel3.setLayout(new GridLayout());

//create titled border around this panel

Border panel3aBorder = new TitledBorder("Vehicle Actions");

jpPanel3a.setBorder(panel3aBorder);

//create buttons for JPanel 3a

JButton jbtAdd = new JButton("Add");
JButton jbtRemove = new JButton("Remove");
JButton jbtSell = new JButton("Sell");

//add the buttons to the Panel

jpPanel3a.add(jbtAdd);
jpPanel3a.add(jbtRemove);
jpPanel3a.add(jbtSell);

//add the panel to the NORTH sector of JPanel 3

jpPanel3.add(jpPanel3a, BorderLayout.CENTER);

//create seconf JPanel to nest in third panel

JPanel jpPanel3b = new JPanel();
jpPanel3.setLayout(new FlowLayout());

Border panel3bBorder = new TitledBorder("Car Lot Size");


jpPanel3b.setBorder(panel3bBorder);

JLabel jlcarLotSz = new JLabel();
jlcarLotSz.setText("Maximum # vehicles allowed: " + carLotSize);
jpPanel3b.add(jlcarLotSz);

jpPanel3.add(jpPanel3b, BorderLayout.SOUTH);

//add the third JPanel to the Frame with
//the nested panels already added

getContentPane().add(jpPanel3,BorderLayout.EAST);

Any advice, input, and/or anecdotes would be greatly appreciated. Thanks!
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving this to the Swing forum, where the GUI experts hang out...
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
kenji mapes
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Craig, much appreciated. I'll take a look at this and see what I can make of it.

Thanks again.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
However, my buttons get trunacted. Any chnages I do seem to change the CENTER panel as well which contains a JList.


This is exactly how BorderLayout has been documented, I don't know what you expect of it. The CENTER area will absorb any extra space and the sides will be truncated as necessary. Furthermore, since you add buttons to two panels within the panel you add to the EAST, how your GUI appears is very much dependent upon the layouts of those two panels and the layout of the panel you added them to.

Any advice, input, and/or anecdotes would be greatly appreciated. Thanks!


Since you haven't asked a specific question or explained what behavior you want it's hard to offer any concrete advice. However there's a few things to note:

1. You set panel3a to CENTER even though you document it as being NORTH.

2. You change the layout of panel3 to a new FlowLayout instead of setting the layout of panel3b.

3. The entire panel3 being added to the EAST sector of a BorderLayout means they will be truncated as soon as there's not enough space to go around.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!