JTable 不会显示列标题
我有以下代码来实例化 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
...
}
与其他制表代码相比,我没有看到任何缺失的步骤,但肯定缺少某些东西。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将您的
JTable
放入JScrollPane
中。试试这个:Put your
JTable
inside aJScrollPane
. Try this:这个答案和接受的答案之间的主要区别是使用
setViewportView ()
而不是add()
。如何使用 Eclipse IDE 将
JTable
放入JScrollPane
中:JScrollPane
容器。JScrollPane
拉伸至所需大小(适用于绝对布局)。JTable
组件拖放到JScrollPane
(视口区域)顶部。在结构>组件,
table
应该是scrollPane
的子组件。生成的代码将如下所示:
The main difference between this answer and the accepted answer is the use of
setViewportView()
instead ofadd()
.How to put
JTable
inJScrollPane
using Eclipse IDE:JScrollPane
container via Design tab.JScrollPane
to desired size (applies to Absolute Layout).JTable
component on top ofJScrollPane
(Viewport area).In Structure > Components,
table
should be a child ofscrollPane
.The generated code would be something like this:
正如前面的答案中所述,“正常”方法是将其添加到 JScrollPane,但有时您不希望它滚动(不要问我何时:))。然后你可以自己添加TableHeader。像这样:
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:
试试
这个!!
}
try this!!