Java JTable 不会显示列标题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2320812/
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
JTable won't show column headers
提问by John R Doner
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.
我有以下代码来实例化 JTable:该表提供了正确的行数和列数,但列顶部没有标题的迹象。
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.
将此与其他制表代码进行比较,我没有看到任何缺失的步骤,但必须缺少某些内容。
采纳答案by Erkan Haspulat
Put your JTable
inside a JScrollPane
. Try this:
把你的JTable
里面一个JScrollPane
. 尝试这个:
add(new JScrollPane(scrTbl));
回答by silver
The main difference between this answer and the accepted answer is the use of setViewportView()
instead of add()
.
此答案与接受的答案之间的主要区别是使用setViewportView()
代替add()
。
How to put JTable
in JScrollPane
using Eclipse IDE:
如何把JTable
在JScrollPane
使用Eclipse IDE:
- Create
JScrollPane
container via Design tab. - Stretch
JScrollPane
to desired size (applies to Absolute Layout). - Drag and drop
JTable
component on top ofJScrollPane
(Viewport area).
JScrollPane
通过设计选项卡创建容器。- 拉伸
JScrollPane
到所需大小(适用于绝对布局)。 - 将
JTable
组件拖放到JScrollPane
(视口区域)的顶部。
In Structure > Components, table
should be a child of scrollPane
.
在 Structure > Components 中,table
应该是scrollPane
.
The generated code would be something like this:
生成的代码将是这样的:
JScrollPane scrollPane = new JScrollPane();
...
JTable table = new JTable();
scrollPane.setViewportView(table);
回答by Tobias Sj?beck
As said in previous answers the 'normal' way is to add it to a JScrollPane, but sometimes you don't want it to scroll (don't ask me when:)). Then you can add the TableHeader yourself. Like this:
正如之前的答案中所说,“正常”方法是将它添加到 JScrollPane,但有时您不希望它滚动(不要问我什么时候:))。然后你可以自己添加TableHeader。像这样:
JPanel tablePanel = new JPanel(new BorderLayout());
JTable table = new JTable();
tablePanel.add(table, BorderLayout.CENTER);
tablePanel.add(table.getTableHeader(), BorderLayout.NORTH);
回答by chaithra_a
public table2() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 485, 218);
setTitle("jtable");
getContentPane().setLayout(null);
String data[][] = { { "Row1/1", "Row1/2", "Row1/3" },
{ "Row2/1", "Row2/2", "Row2/3" },
{ "Row3/1", "Row3/2", "Row3/3" },
{ "Row4/1", "Row4/2", "Row4/3" }, };
String header[] = { "Column 1", "Column 2", "Column 3" };
// Table
JTable table = new JTable(data,header);
// ScrollPane
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setBounds(36, 37, 407, 79);
getContentPane().add(scrollPane);
}
}
}
try this!!
尝试这个!!