Solution
jtable.getHeight does not include header element
jtable.getRowHeight include margin
and the example has 15px actual height, not 14px
More clear example
RO 16
R1 16
R2 16
HEIGHT 48
INTERCELL java.awt.Dimension[width=1,height=1]
MARGIN 1
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
BUT getHeight returns 48px
Also I did streenshot. Each row has 14px height actually.
How how is it possible????
//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());
}
Original question
I have simple test frame with jtable in the scroll pane.
The table minimum height is less than actual height. header + rows < actual size
It is nonsense.
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))
and I got
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
The JTable lays inside the frame without any scroll bars. It is fully visible and there is a lot of space bellow.
How? How jtable.getHeight()
of fully visible table may returns height less then (rows * rowHeight + header)?