简体   繁体   中英

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) ?

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(???);

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);
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM