JTable won't show column headers

John R Doner picture John R Doner · Feb 23, 2010 · Viewed 110.8k times · Source

I have the following code to instantiate a JTable: the table comes up with the right number of rows and columns, but there is no sign of the titles atop the columns.

public Panel1()
{
    int  nmbrRows;

    setLayout(null);
    setBackground(Color.magenta);
    Vector colHdrs;

    //create column headers

    colHdrs = new Vector(10);
    colHdrs.addElement(new String("Ticker"));

    // more statements like the above to establish all col. titles       

    nmbrRows = 25;
    DefaultTableModel tblModel = new DefaultTableModel(nmbrRows, colHdrs.size());
    tblModel.setColumnIdentifiers(colHdrs);

    scrTbl = new JTable(tblModel);
    scrTbl.setBounds(25, 50, 950, 600);
    scrTbl.setBackground(Color.gray);
    scrTbl.setRowHeight(23);    
    add(scrTbl);

//rest of constructor
...

}

Comparing this to other table-making code, I don't see any missing steps, but something must be absent.

Answer

Erkan Haspulat picture Erkan Haspulat · Feb 23, 2010

Put your JTable inside a JScrollPane. Try this:

add(new JScrollPane(scrTbl));