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.
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.
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.
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.
Put your JTable
inside a JScrollPane
. Try this:
add(new JScrollPane(scrTbl));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With