shift66
shift66

Reputation: 11958

How to make JComponent always fill parent event if parent is resizing or content of JComponent is changing?

I have a JCombobox and I'm adding some items at runtime. Some of them are very long and my Jcombobox is growing very long too. It's in a JPanel container with BoxLayout (PAGE_AXIS). I have no idea how to prevent growing...
I wonder there should be a property like overflow and in combobox item will be shown like "123..." if it's "123456789".

EDIT:

I read your answers, thank you very much. But the problem is that my JPanel can resize and I need to make it's children always fill parent. So I cant set preferred size because my JPanel's size can change at runtime.
And I also can't set maximum size or use setPrototypeDisplayValue() because of the same reason.

I tried to use other layouts (GroupLayout, BorderLayout) but the result is the same.

I can post code if you want but I think it's not necessary. Let me know if you need it anyway.
Just tried with JTextArea with single row instead of JCombobox. Result is the same. It's growing when I type.

Upvotes: 2

Views: 1562

Answers (3)

mKorbel
mKorbel

Reputation: 109823

Upvotes: 5

StanislavL
StanislavL

Reputation: 57421

There are multiple solutions for the problem. You can change layout to limit growing.

Or fix somehow max size of the combobox

Or add a custom renderer (extending default renderer) and set preferred size there.

Upvotes: 3

UVM
UVM

Reputation: 9914

You can try this :

myCombo.setPreferredSize(new Dimension(myCombo.getHeight(), myCombo.getWidth()))

Upvotes: 3

Related Questions