user3745357
user3745357

Reputation: 1

Using GridBagLayout JTables and JScrollPane don't work

Using GridBagLayout, I tried to create a table and when I do so, it creates a table with no heading row so I add a JScrollPane. The issue is the JScrollPane causes the header to show, but all other rows not to show.

     Object rowData[][] = { { "1", "Row1-Column2"},
             { "2", "Row2-Column2"} };
     Object columnNames[] = { "#", "Column Two"};

        forms = new JTable(rowData, columnNames);

        forms.getColumnModel().getColumn(0).setPreferredWidth(27);
        forms.getColumnModel().getColumn(1).setPreferredWidth(450);
        forms.getColumnModel().getColumn(0).setResizable(false);
        forms.getColumnModel().getColumn(1).setResizable(false);
        forms.putClientProperty("JTable.autoStartsEdit", Boolean.FALSE);

        JScrollPane fScrollpane = new JScrollPane(forms, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        fScrollpane.setPreferredSize(new Dimension(500, 110));
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 2;
        c.gridwidth = 2;
        pane.add(fScrollpane, c);

Upvotes: 0

Views: 89

Answers (1)

camickr
camickr

Reputation: 324207

I don't see a problem with the basic code except you should not hardcode the preferred size of a component.

Instead of using:

//fScrollpane.setPreferredSize(new Dimension(500, 110));

Try using:

forms.setPreferredScrollableViewportSize(forms.getPreferredSize());

If that doesn't help then post a proper SSCCE that demonstrates the problem.

Upvotes: 2

Related Questions