Reputation: 271
With below example code:
String column_names[]= {"Serial Number","Medicine Name","Dose","Frequency"};
table_model=new DefaultTableModel(column_names,3);
table=new JTable(table_model);
We want to set header with names of columns as in column_names with the above code but it is not working. Header is not visible though table is getting created.
Upvotes: 13
Views: 68373
Reputation: 2019
MessageFormat header = null;
if (this.headerBox.isSelected())
{
header = new MessageFormat(gradesLabel.toString());
}
MessageFormat footer = null;
if (this.footerBox.isSelected())
{
footer = new MessageFormat(this.footerField.getText());
}
boolean fitWidth = this.fitWidthBox.isSelected();
boolean showPrintDialog = this.showPrintDialogBox.isSelected();
boolean interactive = this.interactiveBox.isSelected();
JTable.PrintMode mode = fitWidth ? JTable.PrintMode.FIT_WIDTH :
JTable.PrintMode.NORMAL;
try
{
boolean complete = this.gradesTable.print(mode, header, footer,
showPrintDialog, null,
interactive, null);
if (complete)
{
JOptionPane.showMessageDialog(this,
"Printing Complete",
"Printing Result",
1);
}
else
JOptionPane.showMessageDialog(this,
"Printing Cancelled",
"Printing Result",
1);
}
catch (PrinterException pe)
{
JOptionPane.showMessageDialog(this,
"Printing Failed: " + pe.getMessage(),
"Printing Result",
0);
}
Actually the Jtable object has one method, which is print() menthod, which is used to pass the header and footer as parameter to print Here headerBox is Jcheckbox which one i created in my program and also here some Jlabels are also there. If you dont need that means remove those from this code and run the program
Upvotes: 1
Reputation: 40871
See here for more information about JTables and TableModels
JTable Headers only get shown when the Table is in a scroll pane, which is usually what you want to do anyway. If for some reason, you need to show a table without a scroll pane, you can do:
panel.setLayout(new BorderLayout());
panel.add(table, BorderLayout.CENTER);
panel.add(table.getTableHeader(), BorderLayout.NORTH);
Upvotes: 2
Reputation: 19702
To be able to see the header, you should put the table in a JScrollPane.
panel.add(new JScrollPane(table));
Or you could specifically add the tableHeader to your panel if you really don't want a scrollpane (but: normally you don't want this behaviour):
panel.add(table.getTableHeader(), BorderLayout.NORTH);
panel.add(table, BorderLayout.CENTER);
Upvotes: 34