2 Replies - 175 Views - Last Post: 15 November 2012 - 10:02 AM Rate Topic: -----

#1 Tree55Topz  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 84
  • Joined: 06-September 12

Sizing a JPanel using BorderLayout

Posted 15 November 2012 - 09:33 AM

Hello, quick question: Lets say you have a Jpanel main, set to a BorderLayout and you want to put another JPanel p2 containing a set of radio buttons with a gridLayout. Well I want to put this p2 on the main JPanel and I want it on the WEST side. well, after doing this, I noticed that my buttons were very spread apart and when i resize it they just keep getting bigger. How do i make it so my JPanel p2 containg the radio buttons only takes up a certain amount of space on the WEST side of the border layout? I assume it has something to do with set dimension(new Dimension... etc. Thank you!

Is This A Good Question/Topic? 0
  • +

Replies To: Sizing a JPanel using BorderLayout

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Sizing a JPanel using BorderLayout

Posted 15 November 2012 - 09:59 AM

View PostTree55Topz, on 15 November 2012 - 12:33 PM, said:

Hello, quick question: Lets say you have a Jpanel main, set to a BorderLayout and you want to put another JPanel p2 containing a set of radio buttons with a gridLayout. Well I want to put this p2 on the main JPanel and I want it on the WEST side. well, after doing this, I noticed that my buttons were very spread apart and when i resize it

This is the normal/expected behavior of a GridLayout
Simply put your Panel in a Box first
If your buttons are vertical:

Box box = Box,createVerticalBox();
box.add(the gridpanel of JButton);
box.add(Box.createVerticalGlue();
panel.add(box, BorderLayout.WEST);

if you want your JButton at the top. Id you want to center them add a verticalGlue at the top

Box box = Box,createVerticalBox();
box.add(Box.createVerticalGlue();
box.add(the gridpanel of JButton);
box.add(Box.createVerticalGlue();
Was This Post Helpful? 1
  • +
  • -

#3 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1379
  • View blog
  • Posts: 3,039
  • Joined: 05-April 11

Re: Sizing a JPanel using BorderLayout

Posted 15 November 2012 - 10:02 AM

GridLayout will always take all of the available space to it
Sure you can use other layoutmanagers to solve your problem?
Maybe a simple BoxLayout will be enough?
		JPanel west = new JPanel();
		west.setLayout(new BoxLayout(west, BoxLayout.Y_AXIS));
		west.add(new JRadioButton("Button"));
		west.add(new JRadioButton("Button"));
		west.add(new JRadioButton("Button"));
		
		add(west, BorderLayout.WEST);
		
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setSize(200, 200);
		setVisible(true);



I also believe that you can do some sneaky stuff by adding a wrapper container around your JPanel with the GridLayout
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1