JTable 表头不显示问题 …

来源:互联网 发布:淘宝几件包邮怎么设置 编辑:程序博客网 时间:2024/06/05 16:55

JTable组件显示数据时,如果直接将其放置在Frame的contentPane中则表头一行会显示不出来,如果将其放置在JScrollPane中显示数据的话,表头会自动显示出来。

引用Sun的原话为:

It's easy to put a table in a scrollpane. You need just one or two lines of code:
JScrollPane scrollPane = new JScrollPane(table);table.setPreferredScrollableViewportSize(new Dimension(500, 70));
The scroll pane automatically gets the table's header, whichdisplays the column names, and puts it on top of the table. Evenwhen the user scrolls down, the column names remain visible at thetop of the viewing area. The scroll pane also tries to make itsviewing area the same as the table's preferred viewing size. Theprevious code snippet sets the table's preferred viewing size withthe setPreferredScrollableViewportSize method.

If you're using a table without a scroll pane, then you must getthe table header component and place it yourself. For example:

container.setLayout(new BorderLayout());container.add(table.getTableHeader(), BorderLayout.PAGE_START);container.add(table, BorderLayout.CENTER);
在使用时要注意!
0 0
原创粉丝点击