• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JscrollPane with JTabbedPane for NullLayout

 
sreenath reddy
Ranch Hand
Posts: 415
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I am having problems in getting the scrollpane in a tabbed pane with each tab being a JPanel with Null layout .

In detail , I am having a JTabbedPane where each Tab is a JPanel with layout set to null using panel.setLayout(null) and all the components are added using the Bounds ...and i am wrapping the each panel in a JscrollPane . But the scroll pane is not coming . But the same works fine when i add the components to the panel using a borderlayout instead of null layout

here is the code

JPanel toPanel = new JPanel();
toPanel.setLayout(new BorderLayout());

JTabbedPane pane = new JTabbedPane();

JPanel panel = new JPanel();
// panel.setLayout(new GridBagLayout());(Works fine if i use this)
panel.setLayout(null);
panel.setBounds(0,0,965,150);
JLabel label = new JLabel("asjfdashjsfjfgasfsasjfdashjsfjfgasfsasjfdashjsfjfgasfsasjfdashjsfjfgasfsasjfdashjsfjfgasfsasjfdashjsfjfgasfsasjfdashjsfjfgasfs");
label.setBounds(10,10,831,81);
panel.add(label);
JTextField field = new JTextField(10);
field.setBounds(861,10,94,18);
panel.add(field);

JScrollPane scroll = new JScrollPane(panel);
pane.add(scroll, "test");
toPanel.add(pane);


Can any one let me know how this can be avoided ?? I must use null layout only as the bounds will come from some other application
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I never setLayout(null), but try this instead of panel.setBounds:

panel.setPreferredSize(new Dimension(965,150));
 
sreenath reddy
Ranch Hand
Posts: 415
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi jeff

thanks a lot , it worked but if you don't mind can you let me know how this one line made a such huge difference ??
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you add components to a container that has a layout manager, the job of that layout manager is to set the size and location (the bounds) of those components. If you set them yourself by calling the components' setSize/setLocation/setBounds method, those calls may have no effect because the layout manager will be calling those methods again, later in the layout process. On the other hand, if you want to *influence* the way the layout manager sets those components' sizes you call either call their setPreferredSize / setMinimumSize / setMaximumSize methods or subclass and override getPreferredSize / getMinimumSize / getMaximumSize methods. This is because the layout manager *may* query those properties in the layout process.

In your case, "panel" was placed inside JScrollPane, which you can think of a specialized container with a distinguished child component, the one placed in the center (the "viewport"). When a JScrollPane is asked for its preferred size, it will in turn ask its viewport component what its preferred size is. That's why it is important for that component to have a sensible preferred size. Usually this comes for free for container components with layout managers, but you fired yours...
[ December 20, 2005: Message edited by: Jeff Albrechtsen ]
 
sreenath reddy
Ranch Hand
Posts: 415
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot jeff
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic