Connecting Tech Pros Worldwide Forums | Help | Site Map

Adding a scrollbar to a JScrollPane

Member
 
Join Date: Nov 2008
Posts: 61
#1: Nov 25 '08
I'm trying to add a verticle scroll bar to my scrollPane so it can go up and down the list as needed what sort of lines do I need to add to get this to work.

Expand|Select|Wrap|Line Numbers
  1.    private void refreshScrollPane(int panel)
  2.    {
  3.        if(panel == 1)
  4.        {
  5.            directoryPanelA.remove(scrollPane1);
  6.            invalidate();
  7.            scrollPane1 = new JScrollPane(directoryAList);
  8.            directoryPanelA.add(scrollPane1);
  9.        }
  10.        if(panel == 2)
  11.        {
  12.            directoryPanelB.remove(scrollPane2);
  13.            invalidate();
  14.            scrollPane2 = new JScrollPane(directoryBList);
  15.            directoryPanelB.add(scrollPane2);
  16.        }
  17.    }

JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#2: Nov 25 '08

re: Adding a scrollbar to a JScrollPane


My guess is that you're abusing a JScrollPane, i.e. that thing already implements scrollbars, either when needed or always visibile. I don't think I understand your question.

kind regards,

Jos
Member
 
Join Date: Nov 2008
Posts: 61
#3: Nov 25 '08

re: Adding a scrollbar to a JScrollPane


I'm not sure what you do not understand here. I'll try and be a little more specific (not used to typing out my questions I guess). Ok so I have a scrollpane which holds a JList.. this code refreshes the scroll pane removes the scrollPane component adds the list to it then re-adds the scrollPane to the component. I want it to have a scroll bar on the right hand side that is verticle that allows me to scroll through the jlist that's in the scrollpane. Wow I hope that explained it a little more haha thanks.

Expand|Select|Wrap|Line Numbers
  1.    private void refreshScrollPane(int panel)
  2.    {
  3.        if(panel == 1)
  4.        {
  5.            directoryPanelA.remove(scrollPane1);
  6.            invalidate();
  7.            scrollPane1 = new JScrollPane(directoryAList);
  8.            directoryPanelA.add(scrollPane1);
  9.        }
  10.        if(panel == 2)
  11.        {
  12.            directoryPanelB.remove(scrollPane2);
  13.            invalidate();
  14.            scrollPane2 = new JScrollPane(directoryBList);
  15.            directoryPanelB.add(scrollPane2);
  16.        }
  17.    }
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#4: Nov 25 '08

re: Adding a scrollbar to a JScrollPane


Quote:

Originally Posted by chanshaw View Post

I'm not sure what you do not understand here. I'll try and be a little more specific (not used to typing out my questions I guess). Ok so I have a scrollpane which holds a JList.. this code refreshes the scroll pane removes the scrollPane component adds the list to it then re-adds the scrollPane to the component. I want it to have a scroll bar on the right hand side that is verticle that allows me to scroll through the jlist that's in the scrollpane. Wow I hope that explained it a little more haha thanks.

Again, you don't have to take care of that; just add your JList to a JScrollPane, add that pane to, say, the content panel of a JFrame and you're in business. The JScrollPane takes care of those scroll bars.

kind regards,

Jos
Member
 
Join Date: Nov 2008
Posts: 61
#5: Nov 25 '08

re: Adding a scrollbar to a JScrollPane


Ok perfect, thanks for the information.
Reply