Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why will BoxLayout not allow me to change the width of a JButton but let me change the height?

Tags:

I'm trying to get the Layout of a JDialog of mine to fit a particular look that a program in which I'm porting to Java has, I've used several LayoutManagers before with great success yet for some reason I cannot seem to get this working at all. My goal is to have the Right (East) side of the JDialog contain a "Find Next" and "Cancel" button in a top-down order and then any extra space below so that the two buttons are always at the top of the JDialog, yet for some reason BoxLayout is continously ignoring any attempts at changing (this is where I'm lost) the width of a JButton. Code follows.

JButton findNext = new JButton("Find Next"); JButton cancel = new JButton("Cancel"); cancel.setPreferredSize(new Dimension((int)findNext.getPreferredSize().getWidth(),       (int)cancel.getPreferredSize().getHeight()));  JPanel example = new JPanel();   example.setLayout(new BoxLayout(example, BoxLayout.Y_AXIS));   example.add(findNext); example.add(cancel);   example.add(Box.createGlue());   

No matter what I try, cancel always retains it's normal size. I've tried setMinimumSize() and setMaximumSize() with the same parameters as setPreferredSize with no luck. I've even tried cancel.setPreferredSize(new Dimension(500, 500)); and the buttons height was the only thing adjusted, it STILL retained the default width it was given.

To clear up any questions, here is what it looks like (now that I've finished it) and you'll see that the "Find Next" and "Cancel" buttons are not the same size.

example image

like image 883
Brandon Buck Avatar asked Sep 11 '10 22:09

Brandon Buck


People also ask

How do I make a Jbutton smaller?

Simply delete the frame. setPreferredSize() and the buttons will be small (the frame size will be determined by the preferred sizes of inner components by calling frame.

How do you specify the orientation of a BoxLayout?

You simply substitute X for Y, height for width, and so on. Version note: Before JDK version 1.4, no constants existed for specifying the box layout's axis in a localizable way. Instead, you specified X_AXIS (left to right) or Y_AXIS (top to bottom) when creating the BoxLayout .

What is BoxLayout in Java?

public class BoxLayout extends Object implements LayoutManager2, Serializable. A layout manager that allows multiple components to be laid out either vertically or horizontally. The components will not wrap so, for example, a vertical arrangement of components will stay vertically arranged when the frame is resized.

Which layout is used to align fixed width components at the edges?

BorderLayout class implements a common layout style for top-level windows. Four fixed narrow, fixed-width components at edges and one large area in the center.


1 Answers

I know this is an old question but I don't really see a good explanation. So for the sake of searchers that stumble upon this I will add my two cents.

There are three methods associated with sizing components in Swing: setPreferredSize(), setMinimumSize(), and setMaximumSize(). However, the important point is that it is up to the particular layout manager being used as to whether or not it honors any of these methods.

For BoxLayout (the layout the original poster is using):

  • setMinimumSize() -- BoxLayout honors this
  • setMaximumSize() -- BoxLayout honors this
  • setPreferredSize() -- if X_AXIS is being used width is honored, if Y_AXIS is being used height is honored

The OP is using a Y_AXIS BoxLayout which is why only his height was being changed.

Update: I put together a page with this same information for all of the layout managers. Hopefully it can help some searchers out: http://thebadprogrammer.com/swing-layout-manager-sizing/

like image 126
Michael Avatar answered Dec 06 '22 03:12

Michael