gpt4 book ai didi

java - TableColumn setPreferredWidth 不起作用

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:06:24 25 4
gpt4 key购买 nike

我有一个包含许多列的 JTable。我想要调整特定列的大小。我希望通过使用 setPreferredWidth,列将调整到该大小,或者内容的大小,这样不会发生截断,并让其余列占用剩余空间,但是,所有列,包括我调整大小的列,均等地分割了表格的所有空间;好像 setPreferredWidth 什么都没做。实际上,我希望能够设置列的宽度并将其缩小到该大小而不截断内容(我是不是强调得太多了?)以这样一种方式所有列尚未调整大小的填充剩余空间。使用 setMaxWidth 会截断内容(我有没有提到我不喜欢那样?)如何调整/缩小列的大小而不截断它并且它不做绝对没有?这是有问题的代码:

for (int i = 0, x = 0; i < table.getColumnModel().getColumnCount(); i++)
if ((x = model.getColumnWidth(i)) > -1)
table.getColumnModel().getColumn(i).setPreferredWidth(x);

表格在一个 JPanel (MyListPanel - BorderLayout) 中,它在另一个 JPanel (GridBagLayout) 中添加:

new GridBagConstraints(0, 3, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(2, 0, 0, 2), 0, 0))

编辑:这是我的 JPanel 子类的构造函数:

public MyListPanel(boolean showHeader, String title, ColData...columns) {
super(new BorderLayout());
model = new MyListTableModel(columns);
table = new JTable(model);

table.addFocusListener(this);

add(table);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

setTitle(title);

if (showHeader)
add(table.getTableHeader(), BorderLayout.NORTH);

for (int i = 0, x = 0; i < table.getColumnModel().getColumnCount(); i++)
if ((x = model.getColumnWidth(i)) > -1)
table.getColumnModel().getColumn(i).setPreferredWidth(x);

setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
}

和 MyListTableModel.ColData:

public static class ColData {
private int index;
private String title;
private int width;
public ColData(int _index, String _title, int _width) { index = _index; title = _title; width = _width; }
}

最佳答案

包括:

table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

关于java - TableColumn setPreferredWidth 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7782609/

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