Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JScrollPane doesn't resize when inside a panel

843804May 16 2005 — edited May 16 2005
Hello, I have a JTable in a JScrollPane and I want the table to use a significant portion of the window when the user increases the size of the window.
In my actual application, I have a lot of buttons above and below the table, and I've put these in additional JPanels for proper organization and formatting.

The problem is that the JScrollTable does not resize when it is in a panel, even if I carefully adjust the constraints. (I'm using GridBagLayout here, but that doesn't matter, I've tried others)

Please point me in the right direction, perhaps I'm just missing a specific variable...

The code I have supplied is a very simple demonstration of the problem, modified from one of Sun's tutorials. Running the code quickly gives you an idea of what the problem is. If you don't include the second JPanel (which is the solution I have in comments below (try that too)), then it works, but this solution will not work in my situation.
/*
 * SimpleTableDemo.java is a 1.4 application that requires no other files.
 */
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class SimpleTableDemo extends JPanel {

    public SimpleTableDemo() {
        super(new GridBagLayout());
        final JTable table = new JTable(data, columnNames);
        table.setPreferredScrollableViewportSize(new Dimension(250, 70));
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        
        JPanel p = new JPanel();
        JScrollPane sp = new JScrollPane(table);
        JLabel title = new JLabel("Some title perhaps.");
        
        GridBagConstraints c = new GridBagConstraints();
        
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 0;
        c.weightx = 1;
        c.weighty = 0;
        add(title,c);
        c.fill = GridBagConstraints.BOTH;
        c.weightx = 1;
        c.weighty = 1;
        c.gridy = 1;
        //These lines are the cause of the problem.
        p.add(sp,c); //I have a scroll pane in a panel.
        add(p, c);   // and that panel is in the larger panel.
        
        //Comment the above 2 lines and Uncomment the next line to see what I want it to look like.
        //add(sp, c);
    }


    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    private static void createAndShowGUI() {
        //Make sure we have nice window decorations.
        JFrame.setDefaultLookAndFeelDecorated(true);

        //Create and set up the window.
        JFrame frame = new JFrame("SimpleTableDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create and set up the content pane.
        SimpleTableDemo newContentPane = new SimpleTableDemo();
        newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
    

    private static final String[] columnNames = {"First Name",
                            "Last Name",
                            "Sport",
                            "# of Years",
                            "Vegetarian"};

    private static final  Object[][] data = {
        {"Mary", "Campione",
         "Snowboarding", new Integer(5), new Boolean(false)},
        {"Alison", "Huml",
         "Rowing", new Integer(3), new Boolean(true)},
        {"Kathy", "Walrath",
         "Knitting", new Integer(2), new Boolean(false)},
        {"Sharon", "Zakhour",
         "Speed reading", new Integer(20), new Boolean(true)},
        {"Philip", "Milne",
         "Pool", new Integer(10), new Boolean(false)}
    };

}
Thanks in advance,
Andy Korth
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 13 2005
Added on May 16 2005
3 comments
47 views