File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes JScrollPane inside JTabbedPane 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 inside JTabbedPane" Watch "JScrollPane inside JTabbedPane" New topic

JScrollPane inside JTabbedPane

Alex Kravets
Ranch Hand

Joined: Jan 24, 2001
Posts: 476
When I put JScrollPane inside JTabbedPane the screen looks messed up. All I see is very small JScrollPane in the center of the viewable are. If I replace JScrollPane with a regular JPanel, everything works fine. What I want to accomplish is to make JScrollPane behave as it should inside JTabbedPane, so that the whole space occupied by JTabbedPane's viewable are is scrollable by JScrollPane.
Here is part of code:
JPanel scrollPanel = new JPanel();
scrollPanel.setPreferredSize(new Dimension(500,500));
//JScrollPane scrollPane = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZON
JScrollPane scrollPane = new JScrollPane();
scrollPane.setPreferredSize(new Dimension(500,500));
gbc.gridx = 0;
gbc.gridy = 19;
gbc.anchor = GridBagConstraints.WEST;
screen.add(scrollPane); **

** screen is my JTabbedPane().

All right brain, you don't like me and I don't like you, but let's just do this one thing so I can get back to killing you with beer.<br /> <br />- Homer Simpson
Greg Brouelette
Ranch Hand

Joined: Jan 23, 2002
Posts: 144
My gut feeling is that the problem is with the gridbag layout rather than with your scroll pane. Just for fun, try doing what you need to do using a BorderLayout or a GridLayout(1,1) and see if the scroll pane shows up correctly.
After a very long project where I was forced to use the DirtBagLayou. . .excuse me . . . I meant "GridBagLayout" layout manager I've come to believe that there are very few times when a GridBagLayout is needed. With very few exceptions I've been able to do everything I need with BorderLayout, GridLayout, and FlowLayout by putting panels inside panels.

For a good Prime, call:<br />29819592777931214269172453467810429868925511217482600306406141434158089
Alex Kravets
Ranch Hand

Joined: Jan 24, 2001
Posts: 476
actually, there was a problem with how I was putting everything together. This is what I got from :
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TabbedPaneDemo extends JPanel {
public TabbedPaneDemo() {
ImageIcon icon = new ImageIcon("images/middle.gif");
JTabbedPane tabbedPane = new JTabbedPane();
Component panel1 = makeTextPanel("Blah");
tabbedPane.addTab("One", icon, panel1, "Does nothing");

Component panel4 = makePanel();
tabbedPane.addTab("Two", icon, panel4, "Does nothing at all");
//Add the tabbed pane to this panel.
setLayout(new GridLayout(1, 1));
protected Component makeTextPanel(String text) {
JPanel panel = new JPanel(false);
JLabel filler = new JLabel(text);
panel.setLayout(new GridLayout(1, 1));
return panel;
protected Component makePanel() {
JPanel panel = new JPanel(false);
JEditorPane jEdit = new JEditorPane();
JScrollPane jScrollPane1 = new JScrollPane();
jScrollPane1.getViewport().add(jEdit, null);
panel.add(jScrollPane1, null);
panel.setLayout(new GridLayout(1, 1));
return panel;
public static void main(String[] args) {
JFrame frame = new JFrame("TabbedPaneDemo");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
frame.getContentPane().add(new TabbedPaneDemo(),
frame.setSize(400, 125);
I tried it, works for me.
I agree. Here's the link:
subject: JScrollPane inside JTabbedPane
It's not a secret anymore!