craigmiller160
craigmiller160

Reputation: 6273

Restrict JSpinner size

So if JSpinner has a value that is very, very large (ie lots of characters), it'll expand its own size, and blow up the size of the panel it is in. I'm trying to prevent this, and ensure that the component doesn't get too large.

There's no way to know its preferred size ahead of time, because the panel sizes itself dynamically based on the size of the window and the size of all the components within it. However, the JSpinner is the only one that seems to blow up like this.

A JSpinner must be used in this situation, can't ditch it here. I've tried setting the number of columns on the underlying editor's text field, doesn't work.

I need to be able to have the JSpinner fill the width of the enclosing panel, and go no larger than that. Is there anything that I have the option of doing here?

Thanks.

Upvotes: 0

Views: 238

Answers (1)

camickr
camickr

Reputation: 324128

Override the getPreferredSize() method of the spinner.

I would first invoke super.getPreferredSize() to get the default value. Then you can override the width to some reasonable value.

Dimension d = super.getPreferredSize();
d.width = ???
return d;

You could make your width based on the FontMetrics of the spineer so that if the Font ever changes your width will rescale with the font.

Upvotes: 1

Related Questions