mgarin / weblaf

WebLaF is a fully open-source Look & Feel and component library written in pure Java for cross-platform desktop Swing applications.
http://weblookandfeel.com
GNU General Public License v3.0
1.14k stars 235 forks source link

WebComboBox minimum width #585

Closed husker-dev closed 4 years ago

husker-dev commented 4 years ago

Minimum width of WebComboBox based on selected text of item. This breaks my GridLayout. I tried to use setMinimumWidth to change width but it didn't work for me.

image image

I want to cut the text like this:

image

husker-dev commented 4 years ago

I haven't read about JComboBox width. Here is solution:

combobox.setPreferredWidth(20);
mgarin commented 4 years ago

Just wanted to comment a bit on the original question -

Looking at your particular example - dockable frame size is hard set, so it simply gives free space it has to whatever is placed inside of it.

You're using a container with GridLayout that has the task of laying out your components which happen in GridLayout.layoutContainer method. It is fairly simple and straightforward, but it doesn't use minimum size of the components you place on it at all. In fact - it doesn't even use preferred size.

And the reason why your container gets outside of the visible bounds - you probably have a scroll pane on top of it. And scroll pane has a "bad habit" of respecting only preferred size of whatever is within the viewport. I mean, it can take any space it wants, so why respect minimum size in the first place?

So minimum size will not really affect anything in this particular case because it's simply not even used.

Preferred size will though, because it affects the GridLayout preferred size, which in turn affects the scroll pane viewport size.


And a few notes about minimum size overall -

Method setMinimumWidth might not work the way you are expecting it to, same goes for setMinimumHeight and even setMinimumSize that is available on any basic Swing component.

A lot of layouts often ignore minimum size and generally use preferred size instead. Some only use it in their own minimum size calculations (including GridLayout) which may make it seem like they ignore it as well, especially when the layout their container is placed into ignores the minimum size, or the container above it etc.

Often to make minimum size do anything at all - you would need the whole hierarchy to respect the minimum size value which is quite hard. And in the end it might only affect the minimum size of the Window your UI is placed on, but that depends on many things.

So regarding the minimum size - I do not generally recommend using it. As well as maximum size actually which has it's own problems.

But if you do and you find that it doesn't work the way you'd expect it to - I generally recommend testing the same UI layout under pure Swing conditions to see if the problem persists. If it does this is most probably not an issue particularly with WebLaF, but a nuance of how layouting works with minimum sizes of components in Swing.

Just in case - I'm certainly not trying to shift the blame here, WebLaF had it's own share of bugs and weirdness with layout sizes over the years (which I hopefully fixed), but minimum and maximum sizes in Swing are known to confuse a lot of people about what they actually do and in many cases they won't do what you'd expect them to.