I am trying to set preferred width for jscrollpane in borderlayout(WEST).but it is not working
Please look into my sample code
JTree tree=new JTree(root);
JScrollPane jsp=newJScrollPane(tree);
jsp.setBounds(0,0,200,100);
Jframe.add(jsp,BorderLayout.WEST);
but it is showing default width.
Alternatively I tried to set preferred size for JTree , it is working fine but jscrollpane is not working properly.Please help me in this.
It is the layout manager who determines the position of the component, based on the sizes of the component (minimum/maximum/preferred). So your call to
setBoundswill be ignored and instead theBorderLayoutwill determine where it places your component.Normally this mechanism works just fine. The problem with a
JScrollPaneis that its size hints might not be in sync with what you want as behavior (due to the fact the scroll pane can have scroll bars, so it can determine its own size).Solution: just call
setPreferredSizeon theJScrollPanebefore adding it. This is also done in the official tutorial and about the only time I can think of that it is acceptable to call this method.