http://blog.youkuaiyun.com/sting2005/archive/2007/03/13/1527953.aspx
JTable组件显示数据时,如果直接将其放置在Frame的contentPane中则表头一行会显示不出来,如果将其放置在JScrollPane中显示数据的话,表头会自动显示出来。
引用Sun的原话为:
It's easy to put a table in a scroll pane . You need just one or two lines of code:The scroll pane automatically gets the table's header, which displays the column names, and puts it on top of the table. Even when the user scrolls down, the column names remain visible at the top of the viewing area. The scroll pane also tries to make its viewing area the same as the table's preferred viewing size. The previous code snippet sets the table's preferred viewing size with theJScrollPane scrollPane = new JScrollPane(table); table.setPreferredScrollableViewportSize(new Dimension(500, 70));setPreferredScrollableViewportSizemethod.If you're using a table without a scroll pane, then you must get the 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);在使用时要注意!
本文介绍了在Java Swing中如何正确地配置JTable组件以确保表头始终可见的方法。通过将JTable放置在JScrollPane中,可以实现即使滚动表格内容时表头也保持固定在顶部的功能。此外,还提供了不使用滚动面板时手动设置表头的示例。
1367

被折叠的 条评论
为什么被折叠?



