GUI Help - Using JScrollPanes in JPanels

I created a JLabel that contains specific text. Then I created a JScrollPane, passing this JLabel into the constructor. Then I added this scrollpane into a JPanel. When the JLabel text goes off the screen, I would like to have scrollbars on the panel. However, this does not work as intended. Can anyone help?

[316 byte] By [imgnakikurbta] at [2007-11-27 4:44:25]
# 1
r= new JLabel();JPanel i= new JPanel();jsp = new JScrollPane(r);//jsp.setSize(100, 100);i.add(jsp);
imgnakikurbta at 2007-7-12 9:56:25 > top of Java-index,Java Essentials,Java Programming...
# 2
what's happening instead? do you need to setPreferredSize for a component or two?
petes1234a at 2007-7-12 9:56:25 > top of Java-index,Java Essentials,Java Programming...
# 3

You have to tell it to be a vertical scrollbar first. Or horizontal if that's what you want.

For example,

myJScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);

for a vertical scrollbar. Replace all instances of /vertical/i with horizontal for a horizontal scrollbar. Refer to the API to see the other policies you can set.

ktm5124a at 2007-7-12 9:56:25 > top of Java-index,Java Essentials,Java Programming...
# 4

> I created a JLabel that contains specific text. Then

> I created a JScrollPane, passing this JLabel into the

> constructor. Then I added this scrollpane into a

> JPanel. When the JLabel text goes off the screen, I

> would like to have scrollbars on the panel. However,

> this does not work as intended. Can anyone help?

You need to create a [url=http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE)

that demonstrates the incorrect behaviour, because we can't guess exactly what you are doing based on the information provided.

cotton.ma at 2007-7-12 9:56:25 > top of Java-index,Java Essentials,Java Programming...
# 5

> You need to create a

> [url=http://homepage1.nifty.com/algafield/sscce.html]S

> hort, Self Contained, Compilable and Executable,

> Example Program[/url] (SSCCE)

> that demonstrates the incorrect behaviour, because we

> can't guess exactly what you are doing based on the

> information provided.

Something tells me that was copy/pasted =P

ktm5124a at 2007-7-12 9:56:25 > top of Java-index,Java Essentials,Java Programming...
# 6

> > You need to create a

> >

> [url=http://homepage1.nifty.com/algafield/sscce.html]S

>

> > hort, Self Contained, Compilable and Executable,

> > Example Program[/url] (SSCCE)

> > that demonstrates the incorrect behaviour, because

> we

> > can't guess exactly what you are doing based on

> the

> > information provided.

>

> Something tells me that was copy/pasted =P

Heaven's to betsy. That's never happened and I have no idea what you are talking about.

cotton.ma at 2007-7-12 9:56:25 > top of Java-index,Java Essentials,Java Programming...
# 7

Swing related questions should be posted in the Swing forum.

> When the JLabel text goes off the screen, I would like to have scrollbars on the panel

But you added the label to the scrollpane, so when the preferred size of the label is greater than the size of the scrollpane, scrollbars will appear automatically.

If you want scrollbars on the panel then you add the label to the panel and the panel to the scrollpane.

camickra at 2007-7-12 9:56:25 > top of Java-index,Java Essentials,Java Programming...