简体   繁体   English

在Java中,如何为同一数据类型在两个不同的列中设置单元格编辑器?

[英]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.

相关问题 两个构造函数执行不同的操作但采用相同的数据类型 - Two constructors that do different things but take the same data type java 中的自定义表格单元格编辑器,列数据类型为 Float - Custom table cell editor in java with column data type Float Java Bean验证:如何指定相同类型但具有不同组的多个验证约束? - Java Bean Validation: How do I specify multiple validation constraints of the same type but with different groups? 我如何证明 Object.hashCode() 可以为 Java 中的两个不同对象生成相同的哈希码? - How do I prove that Object.hashCode() can produce same hash code for two different objects in Java? JAVA:如何添加两个声明相同名称但值不同的处理值 - JAVA: How do I add two processed values with same name declared but different values 在Java中,如何同时增加两个不同大小的数组(列表)的索引? - In java, how do I increase the index of two array(list)s of different sizes at the same time? Java:如何将两个变量设置为相同的值,然后更改一个而不更改另一个? - Java: How do I set two variables to the same value, then change one without changing the other? Java中两个不同类中的相同泛型类型 - Same generic type in two different classes in Java 你如何比较java中相同泛型类型的两个值? - How do you compare two values of the same generic type in java? 如何将具有相同键的两个 HashMap 中的值(不同数据类型)放入新的第三个 HashMap? - How do I put in the values (of different data types) from two HashMaps with the same keys into a new third HashMap?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM