9

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.

kohlerfc
  • 134
  • 1
  • 1
  • 8
  • 3
    No, the renderer should not change the height of the table row. This should be done when data is added to the model. This means you would do the calculation when you create the table. Then you would use a TableModelListener to listen for changes to the data and then redo the calculation. – camickr Aug 11 '10 at 19:54
  • 1
    Thanks that's a much better solution as it creates separation between the the data and view layers. Also then i would only have to have code in one place and not worry about creating updating code every time i change the table layout. – kohlerfc Aug 11 '10 at 22:01
  • 3
    @camickr, data has nothing to do with views. How is it supposed to calculate the row of a height when adding data to a model? The model and the data must be independent of the view. **IS** indeed the view part of the design that should do calculations on how to fit some data in the whatever-widget is going to be displayed. I'm not saying that it should be the renderer, but for sure the model shouldn't. – Sebastian Feb 22 '12 at 09:01
  • 1
    `I'm not saying that it should be the renderer, but for sure the model shouldn't.` - I didn't say the model should do this. I suggested you should use a TableModelListener so you know when the data has changed, then you invoke the logic to recalculate the height. – camickr Jun 24 '14 at 14:11
  • As it took me 2 days to make this work and I tried - TableModelListener is not the solution. It is called when the model changes not the table GUI repaint. At the time of the model event is raised, the table GUI might (and from my experience never is) up to date with the model. I handled things just as it is done in the code above (my problem described here: http://stackoverflow.com/a/37158209/478765). I had 1 column JTable (as a substitute for totally misbehaving JList). This worked great. – ed22 May 11 '16 at 09:48

2 Answers2

6
table.setRowHeight(...);

If you need more help post your SSCCE.

camickr
  • 321,443
  • 19
  • 166
  • 288
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.

Paul Jowett
  • 6,513
  • 2
  • 24
  • 19