4

I need to make JComponent's width to be 300 or less, but height should be variable (depending on content of JComponent).

Firstly, I tried to use

myComponent.setMaximumSize(300, 9999);

but I found that this method has absolutely no effect, and component still becomes expanded depending on its content (I can't see any effect, at least).

Then I tried to use

myComponent.setPreferredSize(300, (int)myComponent.getPreferredSize().getHeight());

Then, width becomes 300 and not less than 300. This isn't perfect, but acceptable. BUT, now, when component's content is changed, size isn't changed! This isn't acceptable.

I tried to find the way to affect only preferred width, and leave height to be "default", but I failed to find that way.

I tried to set height to 0 or -1: no effect.

How to achieve that?


UPD: please take a look at the picture: http://goo.gl/3Hp59 That's what I already implemented, but I used setPreferredSize(), which is bad, as I already realized, thank you.

Please help me find out the correct way:

Currently I use GridBagLayout. I need to make right column's width <= 300. (and left one, say, <= 200) How to achieve that using GridBagLayout, or what layout should I use instead?

Jonas Söderström
  • 4,856
  • 2
  • 36
  • 45
Dmitry Frank
  • 10,417
  • 10
  • 64
  • 114
  • 3
    don't even try to tweak the min/pref/max - providing reasonable sizing hints is the exclusive task of the component itself ([see a recent thread](http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi/7229519#7229519)). So implement them to report such reasonable values - and then choose a LayoutManager which dis/respects them as you to your requirements. – kleopatra Jan 07 '13 at 16:13
  • 1
    For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Jan 07 '13 at 16:40
  • @kleopatra, ok, I realized that I should not use setPreferredSize() etc, but please help me to find out the correct way: I updated my question, check it. – Dmitry Frank Jan 07 '13 at 18:07
  • @kleopatra, after all, thanks for getting me out from the wrong way. – Dmitry Frank Jan 08 '13 at 11:59

2 Answers2

3
SapuSeven
  • 1,473
  • 17
  • 30
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • 1
    Ok, I realized that I should not use setPreferredSize() etc, but please help me to find out the correct way: I updated my question, check it. – Dmitry Frank Jan 07 '13 at 18:07
3

Use GroupLayout. It has some the best controls for sizing. GroupLayout can look a bit intimidating, but the examples provided are excellent. Especially useful if you want to allow the user to resize the screen.

Alan Yackel
  • 500
  • 3
  • 6
  • Thank you very much, currenly i'm playing with it and it seems to be exactly what I need, really. More, your guess was correct: I want to allow the user to resize the screen, and I couldn't achieve needed behavior with `GridBagLayout`, but I can with `GroupLayout`. Thanks again! – Dmitry Frank Jan 08 '13 at 11:58