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?
r= new JLabel();JPanel i= new JPanel();jsp = new JScrollPane(r);//jsp.setSize(100, 100);i.add(jsp);
what's happening instead? do you need to setPreferredSize for a component or two?
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.
> 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.
> 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
> > 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.
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.