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!
Sizing a JPanel using BorderLayout
Page 1 of 12 Replies - 87 Views - Last Post: 15 November 2012 - 10:02 AM
Replies To: Sizing a JPanel using BorderLayout
#2
Re: Sizing a JPanel using BorderLayout
Posted 15 November 2012 - 09:59 AM
Tree55Topz, 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();
#3
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?
I also believe that you can do some sneaky stuff by adding a wrapper container around your JPanel with the GridLayout
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
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote




|