I am using BoxLayout on a JPanel with a Y-AXIS orientation. All the buttons have different text so the width of the buttons are different when layed out on the JPanel. Is there a way to force all the Buttons to be the width of the largest button on the JPanel? I know I could probably do a setMinimumWidth() but I don't want to try and determine the widest JButton before I know what all buttons I am going to be placing on the JPanel. In fact, some of the JButtons might even be dynamic. Is there a Layout that would be better to use? I looked at GridLayout but it forces the Buttons to be the height of the JPanel as well. I need them to be the normal height. Thanks.
Originally posted by maggie woo: hmm. have you tried gridlayout with a glue object on the bottom or top to take up the extra space?
No, but too much maintanence I think. If I add Buttons I don't want to have to go and adjust that glue to take up less space or whatever I have to do.
Joined: Dec 18, 2003
ah, but that's the beauty of it--you don't have to if you do it right. if the buttons are not being added dynamically, add the glue at the bottom using the grid.y +1 of the last component added, and make sure the weight.y of the glue component is at least 1.0 higher than the weight.y of all your other components. if you've got a dynamic set of buttons, for instance, that you want always to appear stacked toward the top of your box (with the glue at the bottom), just make sure the weight.y of the glue component is at least 1.0 higher than the weight.y of all your other components, AND if you know you won't be adding more than, say 15 components, make the grid.y of the glue component 20 or something reasonably high. well, it may not be elegant, but it seems to work... I have a dynamically created panel with the following method called when the components I want are finished being added:
Hi,Gregg The layout manager included in the JDK have not provide such feature, you can take a look the JOptionPane UI class button layout and JFileChooser UI class button layout, but they are not good enough. And I have notice the StringLayout have add some feature in Jdk1.5, maybe add this feature. You also take a look the jgoodies form layout, It have host in java.net, and free to use. Our company have release a layout suite, and it's commercial.
this may be no help at all but thought i would mention it. besides BoxLayout, there is also a Box class. i have found it useful before. other times GridLayout worked better. i confess i still have a lot to learn about GUI stuff :roll: