0

I want to add a scrollbar on the side of my Jpanel, itself inside a Jpanel. The scrollbar is displayed but there is no knob in it so I can't scroll.

public class PanelFluxSortant extends JPanel {
    public PanelFluxSortant(FluxSortant fs) {
        super(new BorderLayout());
        makeModel();
        parametres = DataManager.getInstance().getParametres();
        createComponents(fs);
        placeComponents();
        initBehaviour();
    }

    private void placeComponents() {
      SpringLayout layout = new SpringLayout();

      JPanel paneFS = new JPanel(layout);

      JScrollPane scrollPane = new JScrollPane(paneFS);    
      scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
      scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

      scrollPane.setPreferredSize(paneFS.getPreferredSize());
      paneFS.add( Box.createVerticalStrut(400) );
      add(scrollPane, BorderLayout.CENTER);
    }
}

On th first picture the scroll bar isn't needed : [scroll bar init

But here There is some element on the buttom of the rigtht panel. In order to see it I have to enlarge the panel, using the buttom right corner.

scroll bar needed

jayjaypg22
  • 1,641
  • 5
  • 22
  • 41
  • 1
    What is the preferred size of paneFS? – MasterBlaster Aug 26 '16 at 15:14
  • 2
    For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). @MasterBlaster that & ten other things would be easy for us to tell if the OP posted runnable code (as might be seen in an MCVE/SSCCE)! – Andrew Thompson Aug 26 '16 at 16:01
  • 2
    Possible duplicate of [*Scrollbars not appearing in JScrollPane*](http://stackoverflow.com/q/6641897/230513); if not, please edit your question to include a [mcve] that shows your revised approach. – trashgod Aug 26 '16 at 17:00
  • @trashgod I've added the solution of the wuestion tagged as a possible duplicate, but it doesn't work better : paneFS.add( Box.createVerticalStrut(400) );, you can see it in the code. – jayjaypg22 Aug 29 '16 at 09:56
  • @MasterBlaster : find in debug mode: paneFS.getPreferredSize() = Dimension[width=10,height=23] – jayjaypg22 Aug 29 '16 at 10:08
  • @AndrewThompson : It seems to me that the minimal, runnable code would be very long. That's why I've just included the code that seems to me important and necessary. But I will answer to every question. I can add code you think is missing too. – jayjaypg22 Aug 29 '16 at 10:12
  • 1
    *"That's why I've just included the code that seems to me important and necessary"* We often find the problem is in code not included. *"It seems to me that the minimal, runnable code would be very long."* It seems to me you're not thinking laterally. For example, it's likely it would be possible to recreate the problem using a loop to create 100 numbered (in the text) buttons. We don't want to see the actual application code, but an example that demonstrates the immediate problem. In fact, scrap the loop idea, try it with a single text area with 200 rows. Think **laterally.** – Andrew Thompson Aug 29 '16 at 10:30
  • @jayjaypg22: you're not using `BoxLayout`, so that exact change won''t work; you have to alter the preferred size of `paneFS`; please edit your question to include a [mcve] that shows your revised approach. – trashgod Aug 29 '16 at 14:00
  • @trashgod :I've tried to create just a JFrame containing a JPanel put in a JSCrollPane, but then I don't have the problem anymore. I've found the JSCrollPane dimension : Dimension(28,26). So I've changed the paneFS : paneFS.setPreferredSize(new Dimension(50,50)); In order to be larger and longer than the scrollPane. But the effects are the same : a scrollBar but no knob. – jayjaypg22 Aug 29 '16 at 14:14
  • This [example](http://stackoverflow.com/a/3518047/230513) may help guide you. – trashgod Aug 29 '16 at 14:22

1 Answers1

-1

I think its because there is nothing to scroll down for. Try to add controls into it and see if the knob appears.

  • "Scrollbars appear when the preferred size of the component added to the scollpane is greater than the size of the scroll pane."—[_loc cit._](http://stackoverflow.com/a/6641948/230513) – trashgod Aug 26 '16 at 17:00