File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes JscrollPane with JTabbedPane for NullLayout Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JscrollPane with JTabbedPane for NullLayout" Watch "JscrollPane with JTabbedPane for NullLayout" New topic
Author

JscrollPane with JTabbedPane for NullLayout

sreenath reddy
Ranch Hand

Joined: Sep 21, 2003
Posts: 415
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

Joined: Sep 16, 2005
Posts: 1780
I never setLayout(null), but try this instead of panel.setBounds:

panel.setPreferredSize(new Dimension(965,150));


There is no emoticon for what I am feeling!
sreenath reddy
Ranch Hand

Joined: Sep 21, 2003
Posts: 415
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

Joined: Sep 16, 2005
Posts: 1780
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

Joined: Sep 21, 2003
Posts: 415
Thanks a lot jeff
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JscrollPane with JTabbedPane for NullLayout