0

First of all, i searched the whole day, tried many solutions, but no one worked..

I want to have line wrapping in my jtable.. so I made my own cellrenderer... but my JTable is laggy while scrolling etc...

Here is my cellrenderer:

public class LineWrapCellRenderer extends JTextArea implements TableCellRenderer{


public LineWrapCellRenderer(){


      this.setWrapStyleWord(true);            
        this.setLineWrap(true);   
        this.setOpaque(true);


}
 public Component getTableCellRendererComponent(
            JTable table,
            Object value,
            boolean isSelected,
            boolean hasFocus,
            int row,
            int column) {


     if (value != null) {
            setText(value.toString());
          } else {
            setText("");
          }

        return this;
    }

}

I've set the renderer with

setDefaultRenderer(String.class, CellRenderer);

My tablemodel looks like this:

return new DefaultTableModel(data, columnNames){

    Object tempRetVal = new Object();
    Object RetVal = new Object();


     @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
            return false;
        }



    @Override
    public Class<?> getColumnClass(int columnIndex) {

        tempRetVal = Object.class;
        RetVal = Object.class;


        if(getRowCount() > 0){

            for(int i = 0; i<getRowCount();i++){
                tempRetVal =  getValueAt(i, columnIndex);



            if(tempRetVal!=null){
                RetVal = tempRetVal;
            }
            }
        }
        return RetVal.getClass();
    }

};

thanks in advance

  • Your lags from `getColumnClass()` method , why do you always need to loop through rows? – alex2410 Mar 12 '15 at 15:21
  • Thanks for the reply. I want to get the data type of the column. But sometimes the first cell is empty.. so I loop through the whole rows of a column to get the data type.. – Maxipianoplayer Mar 12 '15 at 15:26
  • Do you store different types in one column? If data have same type set column classes when create model. – alex2410 Mar 12 '15 at 15:30
  • No, I dont store different types in one column. But the tablemodel is created with a Resultset, dynamically.. the probelm is, if I just ckeck the first row of a column, it is maybe empty, so Object.class will be returned and my renderer wont be used, althoug the column is from type string and other rows of that column contain strings – Maxipianoplayer Mar 12 '15 at 15:33
  • Seems [that](http://stackoverflow.com/a/10625471/2894369) helps you. – alex2410 Mar 12 '15 at 15:36
  • I already made it like that :D – Maxipianoplayer Mar 12 '15 at 15:43
  • So,you can get column types from resultSet Metadata, and you needn't to get it from rows – alex2410 Mar 12 '15 at 15:46
  • Thanks, i changed it, but it is stll laggy... the resutlset is about 1000 rows big.. is that a problem to have so much textareas in the jtable? – Maxipianoplayer Mar 12 '15 at 15:56
  • `is that a problem to have so much textareas in the jtable?` - There is only one text area. That is the reason for using a renderer. It is easy to test. Just remove your custom renderer and see what scrolling is like using the default renderer (which is a JLabel). – camickr Mar 12 '15 at 15:58
  • 2
    Creating a text area renderer is not that simple. You also need to manage the size of the cell. If you want to display all the text, then you need to set the height of each row in the table. Or if you only want to display 2 lines of text and then let then user scroll you can't just use a renderer because the renderer is not a real component so scrollbars won't work. Anyway, here is an old solution that may or may not help: http://www.javaspecialists.eu/archive/Issue106.html – camickr Mar 12 '15 at 16:06
  • When I comment `setDefaultRenderer(String.class, CellRenderer);` It isnt laggy any more – Maxipianoplayer Mar 12 '15 at 16:12

0 Answers0