JScrollPane中添加JTable不能显示表头 5
JScrollPanetablePane=newJScrollPane();tablePane.setViewportView(table);tablePane.setR...
JScrollPane tablePane = new JScrollPane();
tablePane.setViewportView(table);
tablePane.setRowHeaderView(table);
tablePane.setPreferredSize(new Dimension(950, 450));
this.setPreferredSize(new Dimension(1100, 500));
this.setLayout(new BorderLayout());
this.add(BorderLayout.CENTER, tablePane);
这是一部分code,在网上搜了类似的问题,大家都说将JTable添加到JScrollPane中,且不要直接用add,不要将layout设置为null。三点都符合了,可是表头还是不显示。请问是什么原因呢?谢谢大家!
问题解决了。
// tablePane.setRowHeaderView(table); 展开
tablePane.setViewportView(table);
tablePane.setRowHeaderView(table);
tablePane.setPreferredSize(new Dimension(950, 450));
this.setPreferredSize(new Dimension(1100, 500));
this.setLayout(new BorderLayout());
this.add(BorderLayout.CENTER, tablePane);
这是一部分code,在网上搜了类似的问题,大家都说将JTable添加到JScrollPane中,且不要直接用add,不要将layout设置为null。三点都符合了,可是表头还是不显示。请问是什么原因呢?谢谢大家!
问题解决了。
// tablePane.setRowHeaderView(table); 展开
1个回答
2015-06-02 · 知道合伙人互联网行家
关注
展开全部
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 scroll户孩膏绞薇悸疙溪躬娄s 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);在使用时要注意!
引用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 scroll户孩膏绞薇悸疙溪躬娄s 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);在使用时要注意!
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询