JTable change the row height dynamically [duplicat

2019-04-21 20:03发布

问题:

This question already has an answer here:

  • Auto adjust the height of rows in a JTable 3 answers

i am having trouble changing the height of the my rows dynamically, is there a method i need to overload?

--Edit--

Sorry for the short post it was my first ....My problems was really to do with changing the row height depending on the content. So what i have done so far is made a inner class that implements TabelCellRenderer.

This is what i am doing at the moment for my row height calculations.

  private static class TextAreaRenderer extends JTextPane implements TableCellRenderer
  {


  public Component getTableCellRendererComponent(JTable table, Object value,
                                                 boolean isSelected,
                                                 boolean hasFocus, int row,
                                                 int column)
  {
      /* Setup Code here */

      this.setText(((String)value).getEntityName());
      int height = new Double(this.getPreferredSize().getHeight()).intValue();
      if (table.getRowHeight(row) < height)
          table.setRowHeight(row, height);

      /* some more code */

      return this;
  }

}

Would this be the correct way to do this ? Thanks.

回答1:

table.setRowHeight(...);

If you need more help post your SSCCE.



回答2:

You simply call setRowHeight(row, height);. For example:

JTable t = new JTable(5, 5);
t.setRowHeight(2, 30);

will set the 3rd row to 30 high.

Is your question more complicated? You haven't said much about the problem.