2

I am trying to add a JTable to a JTabbedPane. It comes up fine, however, the titles for the columns are not visible.

Here's the relavent bits and pieces of my code:

JTabbedPane jp = new JTabbedPane();
static JTable t1 = new JTable();
static MainFrame f = new MainFrame();
static DefaultTableModel model = new javax.swing.table.DefaultTableModel(); 

    f.scroll.add(jp);
    f.scroll.setViewportView(jp);
    jp.addTab("Tab 1", null, t1, "");
    t1.setModel(model);

    model.addColumn("Description");
    model.addColumn("Change");
    model.setRowCount(1);
    model.fireTableStructureChanged();

where f is a JFrame and f.scroll is a JScrollPane

Primm
  • 1,347
  • 4
  • 19
  • 32

2 Answers2

2
  1. add JTable to the JScrollPane then TableHeader should be visible, then add JScrollPane to the JTabedpane, otherwise have to add TableHeader programatically

  2. don't call model.fireTableStructureChanged();, this event is correctly implemented in the used DefaultTableModel

  3. better could be create columns, the add row, these two values put to the DefaultTableModel (Object or Vector)

  4. add this model to JTable, e.g. table = new JTable(myModel);

mKorbel
  • 109,525
  • 20
  • 134
  • 319
0

Instead of adding your JTabbedPane to the scroll pane, and then your table to the tabbedpane, just add your tabbedpane to your frame (f), and then add the scrollpane to tab 1 of that, then set the table to be the viewport of the scrollpane.

Structure:

JFrame {
    JTabbedPane {
        Tab 1 {
            JScrollPane {
                JTable
            }
        }
        Tab 2 {

        }
        tab 3 {

        }
    }
}
Alex Coleman
  • 7,216
  • 1
  • 22
  • 31