如何制作可滚动的JTable [英] How to make a scrollable JTable
问题描述
问题1):我有一个JPanel ps,其布局为空; JScrolPane scrollPane和JTable表. 我已将表添加到scrollPane,然后将scrollPane添加到Jpanel ps.以下是我这样做的代码:-
Question 1) I have a JPanel ps, which has a null layout; a JScrolPane scrollPane, and a JTable table. I have added the table to the scrollPane, and then added the scrollPane to the Jpanel ps. The following is my code for doing that:-
ps = new JPanel();
ps.setLayout(null);
ps.setSize(1000,600);
scrollPane = new JScrollPane();
scrollPane.setBounds(10, 119, 975, 300);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
ps.add(scrollPane);
table = new JTable(data, columns);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
scrollPane.setViewportView(table);
假定不创建带有水平滚动条的scrollPane,并且当列数以无法在单个屏幕上处理的方式增加时,滚动条将有助于显示内容.请参阅"AUTO_RESIZE_OFF".
It is supposed t create a scrollPane with horizontal scrollbars, and when the number of columns increases in such a way that, it cannot be handled on a single screen, the scrollbar helps to display the contents. See "AUTO_RESIZE_OFF".
但是发生的是,所有列的大小是固定的(我不知道有多少个字符.它们的宽度都是相同的.).因此,无法完全查看列的名称.
But what happens is that, the the size of all the columns is fixed (I don't know how many characters. The width of all of them is same.). The name of the columns is thus not fully viewable.
我的问题是,是否可以增加显示的列的宽度,或者显示所有具有自己宽度的列?另外,如何启用滚动功能?
My question is, is it possible to increase the width of the column shown, or to show all the columns with their own width ? Also, how to enable scrolling ?
推荐答案
- 是的.请参见
TableColumn
.通常,我过去所做的工作是使用ResultSetMeta
数据来构建列模型.请查看如何使用表以获取更多信息 - 是的,但是
JTable
可能会有些麻烦.看看打印和
- Yes. See
TableColumn
. Generally what I've done in the past is used theResultSetMeta
data to build the column model. Take a look at How to use Tables for more information - Yes, but
JTable
can be a little trouble some. Take a look at Printing and Printing a JTable. There are some great examples on SO if you have the time to look. - It's always better to filter the data at the database end. Once you have a set of results, of you wanted to filter that set, you could use its
ResultSetMetaData
to determine which columns you want to group by. Equally, if you don't have results yet, you could use theDatabaseMetaData
to determine the columns of the tables in the database
这篇关于如何制作可滚动的JTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!