我有 JTable
,其中包含 Excel 文件中的日期。我对其进行了过滤,并将过滤后的数据插入到 JTable
中。
这是带有 JTable
的类的代码:
package convert;
public class Table extends JFrame{
private static final long serialVersionUID = 1L;
public Table(Vector<Vector<Object>> data){
super("Converted Table");
Vector<Object> head = new Vector<Object>();
head.clear();
head.add("Supplier");
head.add("Invoice number");
head.add("Arrival date at CC");
head.add("Part number");
head.add("Shipment quantity");
head.add("Shipment CBM");
head.add("Shipment weight");
head.add("Container Type");
head.add("Container/truck number");
head.add("Current Date");
head.add("Days in CC");
DefaultTableModel model = new DefaultTableModel(data,head);
System.out.println(head);
System.out.println(data.size());
JPanel panel = new JPanel();
getContentPane().add(panel, BorderLayout.NORTH);
JScrollPane scrollPane_1 = new JScrollPane();
scrollPane_1.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
panel.add(scrollPane_1);
JTable table = new JTable();
table.setColumnSelectionAllowed(true);
table.setCellSelectionEnabled(true);
scrollPane_1.setViewportView(table);
table.setModel(model);
for(int i=0;i<11;i++)
{
table.getColumnModel().getColumn(i).setPreferredWidth(230);
table.getColumnModel().getColumn(i).setWidth(230);
}
}
}
最佳答案
要获得正确的列宽,您需要关闭自动调整大小
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
您还应该删除该行
table.getColumnModel().getColumn(i).setWidth(230);
setPreferredWidth(230) 就足够了。
关于java - 无法为 JTable 提供首选大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24009948/