Carlos Sousa
Carlos Sousa

Reputation: 53

In Java, how do I set a cell Editor in two different columns for the same data type?

This is my first question here, As I am a litle noob in Java, So I apologise if this is a trivial question, But I was unable to find any sort of information about it ...

My problem is: I have a Java swing form with a Jtable. I have populated the table with a tableModel and used a tableRenderer to display a I wanted.

The table has 3 columns:

col-0 = Object

col-1 = Date (just date)

col-2 = Date (just time).

I have sucessfully set the Editor (jCalendar) for the date types, but I want column-1 to have a JCalendar (wich is ok), but on column-2, I was trying to insert a JSpinner for introducing the time.

Is there a way to have different cell editors for the samer data type (in my case is Date) ?

Upvotes: 1

Views: 731

Answers (2)

camickr
camickr

Reputation: 324207

Is there a way to have different cell editors for the samer data type (in my case is Date) ?

You add the editor to a specific column of the TableColumnModel:

table.getColumnModel().getColumn(???).setCellEditor(???);

Upvotes: 1

storm87
storm87

Reputation: 59

You can override the method getTableCellRendererComponent in your custom TableRenderer, then, by checking the column number you can choose which component to return.

public Component getTableCellRendererComponent(JTable table,
                                               Object value,
                                               boolean isSelected,
                                               boolean hasFocus,
                                               int row,
                                               int column)
{
  if(column == 1){
    return new JCalendar();
  }else if(column == 2){
    return new JSpinner(2015,07,31);
  }else{
    return super.getTableCellRendererComponent(table,value,isSelected,hasFocus,row,column);
  }
}

Upvotes: 0

Related Questions