Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Using Absolute Layout and not getting Scroll Bar  RSS feed

 
Shivashankar Gurumurthy
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using my own custom layout using insets and spacing to display a dynamic object to the screen. I am not getting scroll bar when i use my own layout. Any pointers available?

public Sample(String displayName) {

frame = new JFrame("DisplayObjectTree");
Insets frameInsets = frame.getInsets();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
menuPanel = new JPanel();
menuPanel.setLayout(null);
insets = menuPanel.getInsets();
verticalPane = new JScrollPane(menuPanel,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
frame.getContentPane().add(verticalPane);
frame.pack();
frame.setVisible(true);


}


I am parsing the dynamic object and adding them to the menupanel using the insets obtained from panel. In turn menu panel is going into scroll pane which is going into frame.

The problem is when i set the layout to null to override the default settings of panel scroll bar is not coming.
 
Brian Cole
Author
Ranch Hand
Posts: 949
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Shivashankar Gurumurthy:
The problem is when i set the layout to null to override the default settings of panel scroll bar is not coming.


You have more problems than just the scroll bar not showing up, don't you? For example, isn't frame.pack() resulting in a frame so small you can barely see it?

I don't recommend setting the layout manager to null, but if you're going to do it you have to manually do what the layout manager usually does for you. In this case it is setting the size and location of menuPanel, plus either setting menuPanel's preferred size or somehow informing verticalPane how big menuPanel wants to be. (Better, I think, to just use a layout manager that suits you.)
 
Shivashankar Gurumurthy
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks. I am facing the issues that you have told me
 
Rob Spoor
Sheriff
Posts: 21090
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JScrollBar uses the preferred size of the component insideit to determine how large the scroll bars should be, and if they should be displayed.

Usually, the layout manager handles this using the preferredLayoutSize method. This can be overriden by explicitly setting the preferred size of the component.

So either you have to set the preferred size, or use a custom layout manager that calculates it for you.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!