gpt4 book ai didi

java - 关于 JScrollPane 中 JTable 的列宽

转载 作者:搜寻专家 更新时间:2023-11-01 04:06:58 25 4
gpt4 key购买 nike

我想在 JScrollPane 中设置 JTable 的每一列,宽度固定,但我不能用 infoTable.getColumnModel().getColumn(0).setPreferredWidth(10);我使用 JScrollPane 的默认布局,并在 j2se 1.4 中添加带有 scrollPane 的 setViewPortView(infoTable) 的表格。我怎样才能完成这个?这是我的代码

private javax.swing.JButton jButton1;
private javax.swing.JScrollPane jScrollPane1;
public TableFrame() {

jScrollPane1 = new javax.swing.JScrollPane();
jButton1 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jScrollPane1.setBorder(javax.swing.BorderFactory.createLineBorder(
new java.awt.Color(0, 0, 0)));

jButton1.setText("jButton1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

this.setSize(new Dimension(600, 600));
this.jScrollPane1.setSize(500, 500);
this.getContentPane().add(this.jScrollPane1, BorderLayout.CENTER);
this.getContentPane().add(this.jButton1, BorderLayout.WEST);
table = new JTable(new javax.swing.table.DefaultTableModel(100, 2));
this.jScrollPane1.setViewportView(table);
table.getColumnModel().getColumn(0).setWidth(10);
}

附言。感谢您的回复我需要可以拖动列来调整大小,所以我无法设置最大和最小大小

最佳答案

同时设置列的最小和最大宽度,与使用 setPreferredWidth 设置的相同。

infoTable.getColumnModel().getColumn(0).setMinWidth(10);
infoTable.getColumnModel().getColumn(0).setMaxWidth(10);
infoTable.getColumnModel().getColumn(0).setPreferredWidth(10);

关于java - 关于 JScrollPane 中 JTable 的列宽,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3448030/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com