[英]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 ... 这是我的第一个问题,因为我是Java中的小菜鸟,所以如果这是一个小问题,我深表歉意,但是我找不到任何有关它的信息...
My problem is: I have a Java swing form with a Jtable. 我的问题是:我有一个带有Jtable的Java swing表单。 I have populated the table with a tableModel and used a tableRenderer to display a I wanted. 我已经用tableModel填充了表,并使用tableRenderer来显示我想要的。
The table has 3 columns: 该表有3列:
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. 我已经成功地为日期类型设置了编辑器(jCalendar) ,但是我希望第1列具有JCalendar(万能),但是在第2列上,我试图插入JSpinner来引入时间。
Is there a way to have different cell editors for the samer data type (in my case is Date) ? 有没有办法为相同的数据类型使用不同的单元格编辑器(在我的情况下是Date) ?
Is there a way to have different cell editors for the samer data type (in my case is Date) ? 有没有办法为相同的数据类型使用不同的单元格编辑器(在我的情况下是Date)?
You add the editor to a specific column of the TableColumnModel
: 您将编辑器添加到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. 您可以在自定义TableRenderer
重写方法getTableCellRendererComponent
,然后通过检查列号可以选择要返回的组件。
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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.