关于JTable不能显示表头的问题(个人小结)
来源:互联网 发布:力港网络笔试题 编辑:程序博客网 时间:2024/06/05 07:39
好久不写了,最近又联系熟悉起了Swing的东西,遇到问题:
JTable 不能正确显示标题,关于这个,官方文档里面有说:
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:
JScrollPane scrollPane = new JScrollPane(table);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
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 the setPreferredScrollableViewportSize method.
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);在使用时要注意!
所以说使用JTable显示数据时,要将Table添加在JScrollPane里面;
但是我的代码里面还是显示不出来,开始检查原因:
原来:如下添加table不行:
scrollPanel.add(table);
应该改为:
scrollPanel.setViewportView(table);//解决显示表头问题:必须用这个
但是,这样似乎还是不行,在检查,在尝试,终于:
将scrollPane的setLayout(null);注释掉,再试,Ok;
&&&&&&&&&&&&&&&&&&&&&&&&&&&&
这里,针对我的问题总结三点::
1、用JScrollPane承接Table,不用JPanel等;
2、用JScrollPane的构造函数构建,或者用scrollPanel.setViewportView(table);
不用scrollPanel.add(table);
3、承接Table的父容器的布局管理器不能设置为null,其实,对于JScrollPane的布局,有专门的
scroll的布局,也可以用,简便的话,不设置布局;不要Null;
至此,Table和表头都可以正常显示了。
问题比较个体化,有用的可以参考,有好方法希望提意见;
- 关于JTable不能显示表头的问题(个人小结)
- JTable不能正常显示表头
- [Swing]JTable 表头显示问题
- 关于java代码jtable不显示表头的解决
- 解决Swing JTable表头无法显示的问题
- Jtable表头换行显示
- JTable 表头不显示问题 …
- 关于Netbeans的JTable显示数据的问题
- JTable的表头合并
- JTable的表头合并
- JTable的表头合并
- JTable(swing)不显示表头
- JTable中怎样显示表头
- JTable不显示的问题
- Showing JTable Header Without Using JScrollPane java中的JTable不显示表头的原因
- 解决了在ScrollPane中加Jtable不能显示的问题
- 把JTable的表头去掉
- 关于链表头插尾插的个人图解
- 对ecmascript 中 prototype的理解
- org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the sessio
- SQL Server 使用Log Explorer恢复日志方法 转自CSDN论坛
- org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the sessio
- linux 调整系统时间
- 关于JTable不能显示表头的问题(个人小结)
- dl的使用
- C#四种排序算法
- 英语经典语句
- 拉伸图形形成solid实例
- JDBC批量删除☞方法一
- 《三国演义》读后感1
- 解决桌面图标无法拖动的方法
- 【小结】在httpd.conf 中配置VirtualHost