this.table = new JTable(new DefaultTableModel(this.getRowData(),this.getColumns()));
this.table.setPreferredSize(new Dimension(1500,320));
// this.table.setPreferredScrollableViewportSize(new Dimension(1500,320));
JScrollPane scrollPane = new JScrollPane();
scrollPane.getViewport().add(table);
scrollPane.setBounds(new Rectangle(10,250,800,350));
table的preferredSize已经走出了JScrollPane的大小,可是为什么JScrollPane还不显示滚动条呢?
你是这样加的?
试试这样呢this.table = new JTable(new DefaultTableModel(this.getRowData(),this.getColumns()));
this.table.setPreferredSize(new Dimension(1500,320));
JScrollPane scrollPane = new JScrollPane(this.table); scrollPane.setBounds(new Rectangle(10,250,800,350));
// Create a table with 10 rows and 5 columns
JTable table = new JTable(10, 5);// Make the table vertically scrollable
JScrollPane scrollPane = new JScrollPane(table);
By default, a table is created with auto resize enabled. This means that if the user changes the width of the table, the columns automatically expand or shrink so that all the columns are entirely visible. In this mode, there is no need for a horizontal scrollbar. In order to get horizontal scrolling, auto resize must be disabled.
COPY
// Disable auto resizing to make the table horizontal scrollable
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
引自:http://www.exampledepot.com/egs/javax.swing.table/Hscroll.html解决了,可是现在出现了另一个奇怪的现象:我设置PreferredSize()后再getPreferredSize(),结果竟然不是我设置的那个了,为什么呢?