Java 如何为 JTable 设置标题?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2297991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 05:49:25  来源:igfitidea点击:

How to set header for JTable?

javaswing

提问by newbee

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.

我们想用上面的代码在 column_names 中设置带有列名称的标题,但它不起作用。尽管正在创建表,但标题不可见。

采纳答案by Fortega

To be able to see the header, you should put the table in a JScrollPane.

为了能够看到标题,您应该将表格放在 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):

或者,如果您真的不想要滚动窗格,则可以专门将 tableHeader 添加到面板中(但是:通常您不想要这种行为):

panel.add(table.getTableHeader(), BorderLayout.NORTH);
panel.add(table, BorderLayout.CENTER);

回答by camickr

Read the JTable API and follow the link to the Swing tutorial on "How to Use Tables" for a working example. The trick is to add the table to a JScrollPane.

阅读 JTable API 并按照有关“如何使用表”的 Swing 教程的链接获取工作示例。诀窍是将表添加到 JScrollPane。

回答by Reverend Gonzo

See here for more information about JTables and TableModels

有关 JTables 和 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:

JTable 标题仅在表格位于滚动窗格中时才会显示,这通常是您想要执行的操作。如果出于某种原因,您需要显示没有滚动窗格的表格,您可以执行以下操作:

panel.setLayout(new BorderLayout());
panel.add(table, BorderLayout.CENTER);
panel.add(table.getTableHeader(), BorderLayout.NORTH);

回答by Bharathiraja

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

实际上,Jtable 对象有一种方法,即print() 方法,用于将页眉和页脚作为参数传递给打印。这里的headerBox 是我在程序中创建的Jcheckbox,这里还有一些Jlabels。如果你不需要这意味着从这个代码中删除那些并运行程序