TableColumn setPreferredWidth 不起作用

发布于 2024-12-10 07:37:55 字数 1815 浏览 0 评论 0原文

我有一个包含许多列的 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; }
}

I have a JTable with a number of columns. I want a particular column to resize. What I was hoping was by using setPreferredWidth, the column would resize to that size, or the size of the contents such that no truncation occurred and let the rest of the columns take the remaining space, but instead, all of the columns, including the one I resized, equally split all of the space of the table; as if setPreferredWidth did nothing at all. In effect, I want to be able to set the width of a column and have it shrink to that size without truncating content (have I stressed that too much yet?) in such a way that all columns that have not been resized fill the remaining space. Using setMaxWidth truncates content (did I mention I didn't like that?) How do I resize/shrink a column without it truncating and without it doing absolutely nothing? Here is the offending code:

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

The table is in a JPanel (MyListPanel - BorderLayout) which is in another JPanel (GridBagLayout) added with:

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

EDIT: This is the constructor for my subclass of 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));
}

And 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; }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

街道布景 2024-12-17 07:37:55

包括 :

table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

Include :

table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
飞烟轻若梦 2024-12-17 07:37:55

尽管在这里尝试了其他两个答案,但我遇到了类似的问题。就我而言,有时宽度会被正确设置,而有时却不会。我发现问题是由于我试图在更改表模型后立即设置列宽而引起的。我发现在 SwingUtilities.invokeLater() 内部设置列宽可以解决问题。 IE

SwingUtilities.invokeLater(new Runnable(){

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

I had a similar problem despite trying the two other answers here. In my case, some times the width would get set correctly while other times, it would not. I found the problem was caused because I was trying to set the column widths immediately after changing my table model. I found setting the column widths inside of SwingUtilities.invokeLater() did the trick. I.E.

SwingUtilities.invokeLater(new Runnable(){

    @Override
    public void run() {
         int width = 100;  
         for (int column = 1; column < table.getColumnCount(); column++) {
             columnModel.getColumn(column).setPreferredWidth(width);
         }
    }
}
没企图 2024-12-17 07:37:55

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

I know this is a bit late so is mostly for future readers, but I had the same problem and solved it by setting both the column's preferred width and maximum width to the same value.

┈┾☆殇 2024-12-17 07:37:55

我遇到了同样的问题并阅读了@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
   }
}

I had the same issue and reading @Jay Askren answer, the opposite worked for me. I had to set the width in the method where the table was being created.

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
   }
}
末が日狂欢 2024-12-17 07:37:55

设置 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] );
        }

Set all three of setMinWidth, setMaxWidth and setPreferredWidth, where 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] );
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文