Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JTable won't show column headers

Tags:

java

swing

jtable

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.

like image 414
John R Doner Avatar asked Feb 23 '10 18:02

John R Doner


People also ask

How to hide column header in JTable?

We can hide the table header of a JTable by unchecking the JCheckBox and show the table header of a JTable by clicking the JCheckBox.

How to hide JTable column in java?

To hide a column (or more) in a JTable, do not give the column name. To get the hidden data, you must use the TableModel.

How to add JTable in JScrollPane in java?

To create a scrollable JTable component we have to use a JScrollPane as the container of the JTable . Besides, that we also need to set the table auto resize mode to JTable. AUTO_RESIZE_OFF so that a horizontal scroll bar displayed by the scroll pane when needed.


1 Answers

Put your JTable inside a JScrollPane. Try this:

add(new JScrollPane(scrTbl)); 
like image 200
Erkan Haspulat Avatar answered Oct 13 '22 05:10

Erkan Haspulat