jtree not properly scrolled in jscrollpane upon expansion of nodes

allmer used Ask the Experts™
Hi Experts,
I have a JTree in a JScrollPane (see code).
While the projectTree seems to work fine, the resultTree does not.
Please check the bottom left part of the image.
What can possibly cause this?
I am using Netbeans 6.5 and just use the designer to add elements.

Any ideas?

jScrollPane1.setMaximumSize(new java.awt.Dimension(200, 23));
        jScrollPane1.setMinimumSize(new java.awt.Dimension(200, 23));
        jScrollPane1.setPreferredSize(new java.awt.Dimension(200, 23));

        projectTree.setMaximumSize(new java.awt.Dimension(2000, 2000));
        projectTree.setMinimumSize(new java.awt.Dimension(150, 300));
        projectTree.setPreferredSize(new java.awt.Dimension(150, 300));
        projectTree.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
        projectTree.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
            public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {


        jScrollPane3.setMaximumSize(new java.awt.Dimension(200, 23));
        jScrollPane3.setMinimumSize(new java.awt.Dimension(200, 23));
        jScrollPane3.setPreferredSize(new java.awt.Dimension(200, 23));

        resultTree.setMaximumSize(new java.awt.Dimension(2000, 2000));
        resultTree.setMinimumSize(new java.awt.Dimension(150, 300));
        resultTree.setPreferredSize(new java.awt.Dimension(150, 300));
        resultTree.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
            public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {

Open in new window

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Is it because you've set a maximum size for the ScrollPane?

Remember, that the maximum size of a ScrollPane is the maximum size of the pane, not just the bit of it that is viewed through the scrollable viewport.

Perhaps the maximum size for you upper pane is large enough not to interfere, but the lower one is restricting the amount of results shown.

Hope this helps, and regards.


I set the maximum size to 32000 now but there was no change in behavior.
I also adjusted other values for sizes in scrollpane3 and resulttree afterwards but .. no effect.

What else could I do?
Then maybe the preferred size? Try commenting out all size constraints (min,max and preferred) for both the JTrees and JScrollPanes and re-introduce them one at at time.

I suspect it is one the preferred size of your JTree that is causing the JScrollPane to reduce its size. Yes, I've convinced myself now. Comment out the two


statements, that should help.  If it does, ask yourself whether you really want to constrain the size of these trees.


Netbeans is a little bit funny about these details and doesn't allow me to actually edit the generated code.
I have to set some values for those sizes in the editor.
Are there any values that would tell Netbeans not to generate lines for those properties??

Anyway, I used a text editor to comment out those lines and apparently Netbeans can live with that.
So you are absolutely right and just getting rid of all those entries (commenting them out), solves the problem.
Great. I guess you're experiencing one of the drawbacks that accompany the obvious benefits of using a GUI builder. Netbeans just isn't intelligent enough to configure the layout of components precisely without a lot of human guidance.

> Are there any values that would tell Netbeans not to generate lines for those properties?

From memory (I defected to Eclipse some time ago) it is only when you explicitly give a dimension (with the GUI editor) or resize a component with the mouse, that the code is inserted. In any case, it is almost inevitable that some manual intervention is needed during the design of any GUI. Just be grateful you've not have to do it all manually!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial