如何为JTable设置标头? [英] How to set header for JTable?

查看:100
本文介绍了如何为JTable设置标头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用以下示例代码:

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.

推荐答案

为了能够看到标题,您应该将表格放入JScrollPane。

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

panel.add(new JScrollPane(table));

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

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);

这篇关于如何为JTable设置标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆