File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes BoxLayout - JButton size Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "BoxLayout - JButton size" Watch "BoxLayout - JButton size" New topic
Author

BoxLayout - JButton size

Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

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.


GenRocket - Experts at Building Test Data
maggie woo
Ranch Hand

Joined: Dec 18, 2003
Posts: 35
hmm. have you tried gridlayout with a glue object on the bottom or top to take up the extra space?
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

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.
maggie woo
Ranch Hand

Joined: Dec 18, 2003
Posts: 35
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:
Fuqiang Zhao
Greenhorn

Joined: Jan 07, 2004
Posts: 24
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.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

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:


SCJP
Visit my download page
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: BoxLayout - JButton size