java 如何在 JTable 中获得水平滚动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16355931/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get horizontal scroll in JTable
提问by Shahjahan KK
I have 2 issue that i need to ask, 1st how to get the horizontal scroll for my JTable, currently it shrink all the columns which is not readable at all. I have 20 columns to show in a Table.
我有两个问题需要问,第一个问题是如何为我的 JTable 获取水平滚动,目前它缩小了所有根本不可读的列。我有 20 列要显示在表格中。
2nd, how to adjust my table height width maximum to my screen size.
2,如何将我的表格高度宽度最大调整为我的屏幕尺寸。
my current code is
我目前的代码是
Container cp = getContentPane();
ConnectionDB con = new ConnectionDB();
tableModel = con.getRecord(DEFAULT_QUERY);
table = new JTable(tableModel);
table.setPreferredScrollableViewportSize(new Dimension(900, 500));
table.setFont(new Font("Tahoma", Font.PLAIN, 12));
scrollPane = new JScrollPane(table);
centerPanel.add(scrollPane, BorderLayout.CENTER);
centerPanel.setBorder(BorderFactory.createTitledBorder("Books:"));
cp.add("Center", centerPanel);
Kindly help me in it, Thanks in Advance.
请帮助我,提前致谢。
回答by das Keks
To prevent that all columns are resized to the ScrollPane
size you can disable the auto resize:
为防止所有列调整ScrollPane
大小,您可以禁用自动调整大小:
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
Adding your ScrollPane
to the center of the BorderLayout
should set the maximum size to the screen size, because normally the JFrame can't become bigger.
将您添加ScrollPane
到的中心BorderLayout
应该将最大尺寸设置为屏幕尺寸,因为通常 JFrame 不能变大。
To set the size of the ScrollPane
s ViewPort to the screen size you can use the awt Toolkit
.
要将ScrollPane
s ViewPort的大小设置为屏幕大小,您可以使用 awt Toolkit
。
table.setPreferredScrollableViewportSize(Toolkit.getDefaultToolkit().getScreenSize());
回答by Vignesh Vino
Add your JTable
inside JScrollPane
and set for scrollbars.
添加您的JTable
内部JScrollPane
并设置滚动条。
new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
Code sample for JTable
to fit the screen size
JTable
适合屏幕尺寸的代码示例
public class TestJTableFitsScreenWidth {
public static void main(String... args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame f = new JFrame();
DefaultTableModel dtm = new DefaultTableModel(
new String[][]{
new String[] {"A", "B", "D"},
new String[] {"D", "E", "F"}
},
new String[] {"col1", "col2", "col3"});
JTable table = new JTable(dtm);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
f.getContentPane().setLayout(new BorderLayout());
f.getContentPane().add(table, BorderLayout.CENTER);
f.setVisible(true);
f.pack();
}
});
}
}
回答by Priyanka
This may help you.
这可能对你有帮助。
Dimension d = new Dimension(900, 800);
tbl_transaction.setAutoResizeMode(tbl_transaction.AUTO_RESIZE_OFF);
tbl_transaction.setPreferredSize(d);
tbl_transaction.setPreferredScrollableViewportSize(d);