JTable高度计算

时间:2015-12-07 14:31:32

标签: java swing jtable

解决方案

jtable.getHeight不包含标题元素

jtable.getRowHeight包含保证金

,示例的实际高度为15px,而不是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

但是getHeight返回48px

我也做过streenshot。每行实际上都有 14px 高度。

怎么可能????

Source code

Visual representation

    //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测试框架。

表格最小高度小于实际高度。 标题+行<实际大小

这是无稽之谈。

  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位于框架内部,没有任何滚动条。它完全可见,下面还有很多空间。

如何?完全可见表的jtable.getHeight()如何返回高度(行* rowHeight + header)?

2 个答案:

答案 0 :(得分:2)

您可以替换自己的Scrollable实现来获得各种效果。特别是,覆盖N并返回行高的合适倍数。让JTable table = new JTable(tableModel) { @Override public Dimension getPreferredScrollableViewportSize() { return new Dimension(SOME_WIDTH, N * table.getRowHeight()); } }; 为您希望滚动条显示的行数。

JScrollPane sp = new JScrollPane(table);

将表格添加到滚动窗格:

JPanel p = new JPanel(new GridLayout());
p.add(sp);

将窗格添加到具有符合首选大小的布局的面板:

N

行数超过namevector <- colnames(Mature_Hep)[which(Reccurance == "Recurrance")] namevector2 <- colnames(Mature_Hep)[which(Reccurance == "No recurrance")] shortm <- matrix_cpm_spike_norm_mature_liver.m shortm$Recurrence_Status <- NA shortm$Recurrence_Status[(shortm$var2 %in% namevector)] <- "Recurrance" shortm$Recurrence_Status[(shortm$var2 %in% namevector2)] <- "No recurrance" 的表格会显示滚动条,如here所示。

  

我只是想从巨大的项目中找到[单元测试的实际表格高度]。

我不确切知道你的总身高计算是如何失败的,但结果将不可避免地根据用户的平台,所选择的外观&amp;感觉,以及组件验证状态;建议采用错误的方法here。相反,尝试通过使用上面概述的方法确保可以看到特定数量的来满足测试要求。

答案 1 :(得分:2)

  

如何?完全可见表的jtable.getHeight()如何返回高度(行* rowHeight + header)?

JTable有一个默认的首选大小,在创建表时会对其进行硬编码,因为通常会将表添加到JScrollPane的视口中。正如您所看到的那样,首选的视口大小要比一个占用额外空间的小型表所需要的大得多。

如果您有一个小桌子,并且希望它以最小尺寸完全显示,那么您可以使用以下代码:

JTable table = new JTable(...);
table.setPreferredScrollableViewportSize(table.getPreferredSize());
JScrollPane scrollPane = new JScrollPane( table );