动态调整JFrame大小以适应JTable内容

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

Change JFrame size dynamically to JTable contents

问题

我又陷入了困境。

这次我在一个 JFrame 内部放置了一个 JTable

基本上,我希望在生成的表格下面没有“空白”。

当调整大小时,行的高度不会改变,但是由于某种原因,列的宽度会改变。是否有可能在下面没有白色空间?

动态调整JFrame大小以适应JTable内容

如果可能的话,我希望不要有滚动条,只显示完整的表格,并且移除空白,这样即使调整大小,也不会显示出来。

快速更新:我使用了 gridlayout 布局,有点奏效,但是我的标题字体比表格要大,导致单元格能够正确显示,但标题被截断显示为“Hea...”。

动态调整JFrame大小以适应JTable内容

英文:

I've got myself stuck again.

This time I have a JTable inside a JFrame.

I basically just want there to be no "white-space" below the table generated.

When resizing, the rows don't change height, but the columns change width for some reason. Is it at all possible to not have the white space below?

动态调整JFrame大小以适应JTable内容
I'd prefer not to have a scrollbar if it at all possible, and just show the entire table with the white space removed, so even when resized, it doesn't show up.

Quick Update: i used the gridlayout layout and it kind of worked, but my header has a bigger font than the table, resulting in the cells to be shown properly, but the headers being cutoff and displayed as "Hea..."

动态调整JFrame大小以适应JTable内容

答案1

得分: 1

> 是否有可能完全去除下方的空白

    JTable table = new JTable(model);
    table.setPreferredScrollableViewportSize(table.getPreferredSize());
    JScrollPane scrollPane = new JScrollPane(table);
    frame.add(scrollPane);
    frame.pack();
    frame.setVisible(true);
英文:

> Is it at all possible to not have the whitespace below?

JTable table = new JTable(model);
table.setPreferredScrollableViewportSize(table.getPreferredSize());
JScrollPane scrollPane = new JScrollPane( table );
frame.add(scrollPane);
frame.pack();
frame.setVisible( true );

huangapple
  • 本文由 发表于 2020年10月15日 02:11:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/64359188.html
匿名

发表评论

匿名网友

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

确定