Adding a scrollbar to a JScrollPane

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chanshaw
    New Member
    • Nov 2008
    • 67

    Adding a scrollbar to a JScrollPane

    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.

    Code:
       private void refreshScrollPane(int panel)
       {
           if(panel == 1)
           {
               directoryPanelA.remove(scrollPane1);
               invalidate();
               scrollPane1 = new JScrollPane(directoryAList);
               directoryPanelA.add(scrollPane1);
           }
           if(panel == 2)
           {
               directoryPanelB.remove(scrollPane2);
               invalidate();
               scrollPane2 = new JScrollPane(directoryBList);
               directoryPanelB.add(scrollPane2);
           }
       }
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    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

    Comment

    • chanshaw
      New Member
      • Nov 2008
      • 67

      #3
      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.

      Code:
         private void refreshScrollPane(int panel)
         {
             if(panel == 1)
             {
                 directoryPanelA.remove(scrollPane1);
                 invalidate();
                 scrollPane1 = new JScrollPane(directoryAList);
                 directoryPanelA.add(scrollPane1);
             }
             if(panel == 2)
             {
                 directoryPanelB.remove(scrollPane2);
                 invalidate();
                 scrollPane2 = new JScrollPane(directoryBList);
                 directoryPanelB.add(scrollPane2);
             }
         }

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by chanshaw
        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

        Comment

        • chanshaw
          New Member
          • Nov 2008
          • 67

          #5
          Ok perfect, thanks for the information.

          Comment

          Working...