views:

49

answers:

1

I have sever JPanels which have to be ordered vertically. For that I want to fix the width of the JPanels. Because if they are too short in comparison with the windows width, they will go horizontally (one after another) and I do not want it. At the moment the width of the JPanel is not constant because the width of the JRadioButton (included into JPabel) is not constant. How can I make the width of the JRadioButton constant? At the moment it is not constant because of the label (which can be different).

+1  A: 

If the aim is laying out panels in a particular fashion, you must be using a LayoutManager, rather than thinking of resizing indivudual components just for the layout. By default swing uses FlowLayout, and thats why your panels are falling one after another as long as there is space in the "line" (and then they would flow to the next line)

Try using GridBagLayout, its not as complex as may tutorials/articles say. The one or two hours you spend learning this will be a great help. It would help you to arrange your components is a very flexible manner.

Btw, how are you developing your UIs? If you are using a IDE (Eclipse+plugins or NetBeans) then the IDE should help you do the laying out (say, via GroupLayout).

If you are trying to hand code the GUI, then GridBagLayout is the best way to get "in control".

Nivas
I am trying to hand code. I also tried to use the Grid Layout but as any other layout it adopt the size of the window and change proportions of its elements (relation between width and heights of included elements). For example, I also could use vertical ordering using border layout but then I have the same problem: it change proportion of the included elements.
Roman