JTable 列调整大小不起作用

发布于 2024-07-25 07:29:31 字数 302 浏览 2 评论 0原文

table.getTableHeader().setResizingAllowed(false);
column = table.getColumn(columns[0]);
column.setWidth(25);
column = table.getColumn(columns[1]);
column.setWidth(225);
column = table.getColumn(columns[2]);
column.setWidth(50);
table.doLayout()

这拒绝将列设置为其指定的宽度。 有什么理由吗?

table.getTableHeader().setResizingAllowed(false);
column = table.getColumn(columns[0]);
column.setWidth(25);
column = table.getColumn(columns[1]);
column.setWidth(225);
column = table.getColumn(columns[2]);
column.setWidth(50);
table.doLayout()

This refuses to set the columns to their specified widths. Any reason why?

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

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

发布评论

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

评论(3

心的憧憬 2024-08-01 07:29:31

JTable 的调整大小模式不是 AUTO_RESIZE_OFF 时,Swing 会忽略您设置的列宽。
不要使用 setWidth,而是使用 setPreferredWidth。 然后,在布置列时,表将使用该值。 如果您设置最小、最大和首选宽度,它应该适用于所有情况,但您的用户无法再自行更改列宽。 因此请谨慎使用。

API-文档

Swing ignores column widths set by you when resize mode of the JTable is not AUTO_RESIZE_OFF.
Instead of using setWidth, use setPreferredWidth. The table will then use this value when the columns are laid out. If you set minimum, maximum and preferred width, it should work for all occasions, but your users cannot change column width themselves anymore. So use this with caution.

The layout process is described in great detail in the API-Docs

梦里°也失望 2024-08-01 07:29:31

为了让这个工作适用于我的表,我覆盖了 setBounds 并在其中设置了首选宽度:

@Override
public void setBounds(int x, int y, int width, int height)
{
    super.setBounds(x, y, width, height);

    setPreferredColumnWidths(new double[] { 0.4, 0.4, 0.1, 0.1 });
}

我从这篇文章。

protected void setPreferredColumnWidths(double[] percentages)
{
    Dimension tableDim = getPreferredSize();

    double total = 0;

    for (int i = 0; i < getColumnModel().getColumnCount(); i++)
    {
        total += percentages[i];
    }

    for (int i = 0; i < getColumnModel().getColumnCount(); i++)
    {
        TableColumn column = getColumnModel().getColumn(i);
        column.setPreferredWidth(
             (int)(tableDim.width * (percentages[i] / total)));
    }
}

To get this working for my table I overrode setBounds and set the preferred widths in there:

@Override
public void setBounds(int x, int y, int width, int height)
{
    super.setBounds(x, y, width, height);

    setPreferredColumnWidths(new double[] { 0.4, 0.4, 0.1, 0.1 });
}

I got the implementation of setPreferredColumnWidths from this article.

protected void setPreferredColumnWidths(double[] percentages)
{
    Dimension tableDim = getPreferredSize();

    double total = 0;

    for (int i = 0; i < getColumnModel().getColumnCount(); i++)
    {
        total += percentages[i];
    }

    for (int i = 0; i < getColumnModel().getColumnCount(); i++)
    {
        TableColumn column = getColumnModel().getColumn(i);
        column.setPreferredWidth(
             (int)(tableDim.width * (percentages[i] / total)));
    }
}
怕倦 2024-08-01 07:29:31

我很长时间都无法让它工作,然后我终于发现我需要将 autoCreateColumnsFromModel 设置为 false。

编辑:忽略这一点,这是当我想要 tableDataChanged 时触发 tableStructureChanged 的​​结果(请参阅下面的讨论,并感谢 kleopatra 识别错误)

I could not get this working for ages, then I finally found that I needed to set autoCreateColumnsFromModel to false.

edit: ignore this, it was a consequence of firing tableStructureChanged when i wanted tableDataChanged (see discussion below, and thanks to kleopatra for identifying the mistake)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文