TableColumn setPreferredWidth不起作用

7
我有一个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又位于另一个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; }
}
5个回答

8

包含:

table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

5

尽管我尝试了这里提供的两个答案,但我仍然遇到了类似的问题。在我的情况下,有时宽度会被正确设置,而有时则不会。我发现问题的原因是我尝试在更改表格模型后立即设置列宽。我发现在SwingUtilities.invokeLater()中设置列宽就可以解决这个问题。即:

SwingUtilities.invokeLater(new Runnable(){

    @Override
    public void run() {
         int width = 100;  
         for (int column = 1; column < table.getColumnCount(); column++) {
             columnModel.getColumn(column).setPreferredWidth(width);
         }
    }
}

我会点赞你,因为这似乎可能已经解决了我的问题,但是我已经辞职了。 - Ethan Reesor

3

我知道这有点晚了,所以主要是给未来的读者,但我遇到了同样的问题,并通过将列的首选宽度最大宽度设置为相同的值来解决它。


1

我遇到了同样的问题,并且阅读了@Jay Askren的回答后,相反的方法对我起作用了。我不得不在创建表格的方法中设置宽度。

public class Example{
    //..stuff
    private JTable myTable;

    public Example{
    //..stuff
    myTable = AnotherClass.getTable();
    myTable .getColumnModel().getColumn(0).setPreferredWidth(100);//not here
    }

}

public class AnotherClass{
    public static JTable getTable(){
    JTable table= new JTable();
    table.setModel(anyModel);
    table.getColumnModel().getColumn(0).setPreferredWidth(100);//Here!
    return table
   }
}

0

设置setMinWidth、setMaxWidth和setPreferredWidth三个属性,其中minimum_width <= preferred_width <= maximum_width。

        int[] minColSizes       = { 15,  55,  75,  50,  50,  50 };
        int[] preferredColSizes = { 25,  75, 125, 100, 100, 100 };
        int[] maxSizes          = { 50, 100, 200, 200, 200, 200 };
        for ( int index = 0; index < preferredColSizes.length; index++ )
        {
            myTable.getColumnModel().getColumn(index).setMinWidth ( minColSizes[index] );
            myTable.getColumnModel().getColumn(index).setMaxWidth ( preferredColSizes[index] + 100 );
            myTable.getColumnModel().getColumn(index).setPreferredWidth ( maxSizes[index] );
        }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接