JTable高度计算 [英] JTable height calculation
问题描述
解决方案
jtable.getHeight不包含标头元素
jtable.getHeight does not include header element
jtable.getRowHeight包含边距
jtable.getRowHeight include margin
该示例的实际高度为15px,而不是14px
and the example has 15px actual height, not 14px
更清晰的示例
RO 16
R1 16
R2 16
HEIGHT 48
INTERCELL java.awt.Dimension[width=1,height=1]
MARGIN 1
我有3行的JTable.每行为 16px .并且每行边距为1px. 所以我怀疑桌子的高度应该是16 + 1 + 16 + 1 + 16 = 50px
I have JTable with 3 rows. Each row is 16px. And each row margin 1px. So I suspect that table height should be 16 + 1 + 16 + 1 + 16 = 50px
但是getHeight返回48px
BUT getHeight returns 48px
我也做了streepshot.每行的高度实际上为 14px .
Also I did streenshot. Each row has 14px height actually.
怎么可能??
//headers for the table
String[] columns = new String[] {
"Id", "Name", "Hourly Rate", "Part Time"
};
//actual data for the table in a 2d array
Object[][] data = new Object[][] {
{1, "John", 40.0, false },
{2, "Rambo", 70.0, false },
{3, "Zorro", 60.0, true },
};
//create table with data
JTable table = new JTable(data, columns);
//add the table to the frame
this.add(table);
this.setTitle("Table Example");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setVisible(true);
System.out.println("RO " + table.getRowHeight(0));
System.out.println("R1 " + table.getRowHeight(1));
System.out.println("R2 " + table.getRowHeight(2));
System.out.println("HEIGHT " + table.getHeight());
System.out.println("INTERCELL " + table.getIntercellSpacing());
System.out.println("MARGIN " + table.getRowMargin());
}
原始问题
我在滚动窗格中有一个带有jtable的简单测试框架.
I have simple test frame with jtable in the scroll pane.
桌子的最小高度小于实际高度. 标头+行<实际尺寸
The table minimum height is less than actual height. header + rows < actual size
这是胡说八道.
val jtable = view.tableControl
val headerHeight = jtable.getTableHeader.getHeight
val rowsHeight = (0 until jtable.getRowCount()).foldLeft(0) { (heightAccumulator, counter) ⇒
println("Row " + counter + " height " + jtable.getRowHeight(counter))
jtable.getRowHeight(counter) + heightAccumulator
}
val rowsMargin = jtable.getRowMargin() * (jtable.getRowCount() - 1)
println("Actual height " + jtable.getHeight())
println("Header height " + headerHeight)
println("Margin height " + jtable.getRowMargin())
println("PreferredScrollableViewportSize" + jtable.getPreferredScrollableViewportSize())
println("Rows height " + rowsHeight)
println("Should be at least: " + (rowsHeight + headerHeight + rowsMargin))
我知道了
Row 0 height 16
Row 1 height 16
Row 2 height 16
Row 3 height 16
Row 4 height 16
Actual height 80
Header height 19
Margin height 1
PreferredScrollableViewportSizejava.awt.Dimension[width=450,height=400]
Rows height 80
Should be at least: 103
JTable放置在框架内部,没有任何滚动条.它完全可见,下面有很多空间.
The JTable lays inside the frame without any scroll bars. It is fully visible and there is a lot of space bellow.
如何?完全可见表的jtable.getHeight()
如何返回小于(行* rowHeight +标头)的高度?
How? How jtable.getHeight()
of fully visible table may returns height less then (rows * rowHeight + header)?
推荐答案
You can substitute your own implementation of Scrollable
to get a variety of effects. In particular, override getPreferredScrollableViewportSize()
and return a suitable multiple of the row height. Let N
be the number of rows after which you want the scrollbar to appear.
JTable table = new JTable(tableModel) {
@Override
public Dimension getPreferredScrollableViewportSize() {
return new Dimension(SOME_WIDTH, N * table.getRowHeight());
}
};
将表添加到滚动窗格:
JScrollPane sp = new JScrollPane(table);
将窗格添加到具有符合首选大小的布局的面板中:
Add the pane to a panel having a layout that respects preferred size:
JPanel p = new JPanel(new GridLayout());
p.add(sp);
具有多于N
行的表将显示滚动条,如此处所示.
A table having more than N
rows will display scroll bars, as shown here.
我只想在大型项目的[单元测试]中找到实际的表格高度.
I just want to find the actual table height [in the context of] unit tests from the huge project.
我不确定您的总身高计算是如何失败的,但是结果不可避免地会根据用户的平台,所选外观和外观而改变.感觉,以及组件的验证状态;建议在此处使用错误的方法.相反,尝试使用上面概述的方法,确保可见特定数量的行,以符合测试要求.
I don't know exactly how your total height calculation fails, but the result will inevitably change based on the user's platform, the chosen look & feel, and the component validation state; an erroneous approach is suggested here. Instead, attempt to meet the test requirement by ensuring that a paricular number of rows is visible using the approach outlined above.
这篇关于JTable高度计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!