JTable的最后一列被缩小。

huangapple go评论78阅读模式
英文:

Last column of JTable is getting shrunk

问题

如果JTable小于面板大小,我想要使用AUTO_RESIZE_ALL_COLUMNS

如果表格有更多的列,那么我想要保持所有列的最大宽度,并带有滚动条。

但是当我尝试使用下面的代码时,最后一列会缩小。
我想知道是否有遗漏的地方。

// 根据最大宽度(标题和内容)调整列
TableColumnAdjuster tca = new TableColumnAdjuster(table);
tca.adjustColumns();

// 如果列被缩小,通过双击来调整列到最大宽度
new ResizeColumnListener(table);

JScrollPane sp = new JScrollPane(table, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);  

table.getParent().addComponentListener(new ComponentAdapter() {
    @Override
    public void componentResized(final ComponentEvent e) {
        if (table.getPreferredSize().width < table.getParent().getWidth()) {
            table.setAutoResizeMode(table.AUTO_RESIZE_ALL_COLUMNS);
        } else {
            table.setAutoResizeMode(table.AUTO_RESIZE_OFF);
        }
    }
});
英文:

If the JTable is less than the panel size I want to use AUTO_RESIZE_ALL_COLUMNS.

If the table has more columns then I want to keep all the columns to the maximum width with a scrollbar.

But when I try to use the below code, the last column is getting shrunk.
I want to know if I'm missing something.

//Adjust columns according to max width(header,Content)
TableColumnAdjuster tca = new TableColumnAdjuster(table);
tca.adjustColumns();

//Adjust the column to max width with double click if the column is shrinked
new ResizeColumnListner(table);

JScrollPane sp = new JScrollPane(table,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS , ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);  

table.getParent().addComponentListener(new ComponentAdapter() {
    @Override
    public void componentResized(final ComponentEvent e) {
        if (table.getPreferredSize().width &lt; table.getParent().getWidth()) {
            table.setAutoResizeMode(table.AUTO_RESIZE_ALL_COLUMNS);
        } else {
            table.setAutoResizeMode(table.AUTO_RESIZE_OFF);
        }
    }
});

答案1

得分: 1

表格将根据模式值自动调整大小。有5种模式:

AUTO_RESIZE_OFF: 完全不自动调整列宽。当列的宽度总和超过视口的宽度时,使用水平滚动条来容纳列。如果JTable没有包含在JScrollPane中,这可能会导致表格的部分不可见。

AUTO_RESIZE_NEXT_COLUMN: 仅使用调整列后面的列。这会导致相邻单元格之间的“边界”或分隔线可以独立调整。

AUTO_RESIZE_SUBSEQUENT_COLUMNS: 使用在调整的列后的所有列来吸收变化。这是默认行为。

AUTO_RESIZE_LAST_COLUMN: 仅自动调整最后一列的大小。如果最后一列的边界阻止分配所需的大小,则将最后一列的宽度设置为适当的限制,不再进行调整。

AUTO_RESIZE_ALL_COLUMNS: 在JTable中将增量分布在所有列之间,包括正在调整的列。

从你的问题中,不清楚你如何将JScrollPane对象添加到框架窗口的内容窗格中。这里有一篇名为Exploring Swing's Table Component的文章,可以阅读并了解所有组件如何一起工作。

英文:

The table will be auto resized depending mode value. There are 5 modes:

AUTO_RESIZE_OFF: Don't automatically adjust the column's widths at all. Use a horizontal scrollbar to accomodate the columns when their sum exceeds the width of the Viewport. If the JTable is not enclosed in a JScrollPane this may leave parts of the table invisible.

AUTO_RESIZE_NEXT_COLUMN: Use just the column after the resizing column. This results in the "boundary" or divider between adjacent cells being independently adjustable.

AUTO_RESIZE_SUBSEQUENT_COLUMNS: Use all columns after the one being adjusted to absorb the changes. This is the default behavior.

AUTO_RESIZE_LAST_COLUMN: Automatically adjust the size of the last column only. If the bounds of the last column prevent the desired size from being allocated, set the width of the last column to the appropriate limit and make no further adjustments.

AUTO_RESIZE_ALL_COLUMNS: Spread the delta amongst all the columns in the JTable, including the one that is being adjusted.

From your question , it is not clear how you adding the JScrollPane object to the frame window's content pane. Here is an Exploring Swing's Table Component article to read and get understanding how everything works together.

huangapple
  • 本文由 发表于 2020年8月2日 22:02:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/63216889.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定