Vertical scrollbars when needed ...

Heya, this questions should perhaps be added to the newbie forum, but anyhow, I have a textarea, which I want to have vertical scrollbars when needed.

In theTextArea constructor I can use the SCROLLBARS_VERTICAL_ONLY option, but that will give the textarea a permanent vertical scrollbar, not when needed.

And theScrollPane(ScrollPane.SCROLLBARS_AS_NEEDED) constructor will give the textarea both vertical and horizontal scrollbars ..

There must be a way to do this .. I just can't find it. :)

[543 byte] By [stenkross] at [2007-9-26 1:47:49]
# 1
use this:myScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);myScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
artntek at 2007-6-29 2:47:20 > top of Java-index,Desktop,Core GUI APIs...
# 2
Oops - wrong forum - sorry, that's Swing ;-)
artntek at 2007-6-29 2:47:20 > top of Java-index,Desktop,Core GUI APIs...
# 3

I tried this:

TextArea text = new TextArea("", 0, 0, TextArea.SCROLLBARS_NONE);

JScrollPane textcontainer = new JScrollPane();

textcontainer.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

textcontainer.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);

textcontainer.add(text);

add(textcontainer);

I tried to fill the TextArea with text, but no scrollbar showed up .. any bright ideas ?

stenkross at 2007-6-29 2:47:20 > top of Java-index,Desktop,Core GUI APIs...
# 4

Well if you *can* use Swing instead of AWT, then this is how to do it, but please note that (a) you shouldn't mix swing & awt components - use one or the other exclusively, and (b) if this is for an applet, and you use Swing, then you will have compatibility problems (users will need the Java plugin from Sun to view it)

That said, here we go:JTextArea text = new JTextArea("", 20, 30);

JScrollPane textcontainer = new JScrollPane(text);

textcontainer.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

textcontainer.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);

textcontainer.setPreferredSize(new Dimension(300,400)); //or whatever

add(textcontainer);

let me know how that works

artntek at 2007-6-29 2:47:20 > top of Java-index,Desktop,Core GUI APIs...
# 5
Yepp, that works fine, thanx alot :)
stenkross at 2007-6-29 2:47:20 > top of Java-index,Desktop,Core GUI APIs...