1

I have a JTree embedded in a JScrollPane. When the JTree grows bigger than the display can show, the remainder of the tree is located at the bottom outside of the visible range. I expected the JScrollPane to start displaying vertical scrollbars if this happens so that you can scroll the remainder into view. The vertical scrollbars do not appear at all if the JTree is expanded and doesn't fit. Here is how it looks like:

I have both, the horizontal and vertical scrollbarPolicy set to as needed. I also tried embedding the JScrollPane containing the JTree into a JPanel but it didn't help. I'm using the UI designer in IntelliJ IDEA to build the GUI by the way.

Any help on making the vertical scrollbars behave as expected? Forcing them to be shown at all times also doesn't work: They cannot be dragged down regardless.

BullyWiiPlaza
  • 17,329
  • 10
  • 113
  • 185
  • 1
    You don't need to do anything special. All you need to do is add the JTree to the viewport of the scrollpane. The scrollbars will appear automatically when required. If it doesn't work then it is because you have added extra code, probably by using setPreferredSize(). Read the section from the Swing tutorial on [How to Use Trees](https://docs.oracle.com/javase/tutorial/uiswing/components/tree.html) for a working example to get you started. – camickr Mar 26 '18 at 16:56
  • @camickr: Adding `fileBrowserTreeScrollPane.setViewportView(fileBrowserTree);` doesn't work but the designer seems to be setting the `preferredSize` to a default of 350 width and 300 height. Overwriting it with `fileBrowserTree.setPreferredSize(null);` in addition doesn't help either. – BullyWiiPlaza Mar 26 '18 at 17:01
  • Again, download the demo code from the tutorial and test it. Then compare what is different and fix it. I never use an IDE to generate code for this reason. I want to be in full control. – camickr Mar 26 '18 at 19:55
  • 1
    For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Mar 26 '18 at 22:51

1 Answers1

1

not use preferredSize. in UI designer choos JTree element, find property preferredSize and in context menu select Restore default value

enter image description here

pwipo
  • 463
  • 3
  • 7