JTable绑定避免默认值

时间:2012-05-01 18:13:31

标签: java swing data-binding jtable

我使用Java bean绑定绑定Jtable,其中api给出整数或浮点值的默认值,如0或0.0,如下所示。我想避免使用相应的默认值并将单元格设置为空,除非最后一个细胞值。

1        WW     88.0        88.0      1021021       340.0       
4        TT     55.0        55.0      1021021       340.0       
5        PP     66.0        66.0      1021021       340.0

                0            0          0           1020

2        gg     66.0        66.0      1021022       320.0       
3        LL     658.0       652.0     1021022       320.0

               0            0          0             640

并且该表应该看起来像..

1        WW     88.0        88.0      1021021       340.0       
4        TT     55.0        55.0      1021021       340.0       
5        PP     66.0        66.0      1021021       340.0

                                                    1020

2        gg     66.0        66.0      1021022       320.0       
3        LL     658.0       652.0     1021022       320.0

                                                     640

任何人都可以建议更好的方法来解决这个问题,这将是非常充分的,并提前感谢。

3 个答案:

答案 0 :(得分:1)

我建议这应该在TableModel中完成,特别是使用getValueAt(int row, int column)方法。类似的东西:

public Object getValueAt(int rowIndex, int columnIndex){
  Object cellValue = // get your values out of your Beans...
  if (cellValue==0 && columnIndex!=LAST_COLUMN_INDEX){
    return null;
  }
  return cellValue;
}

答案 1 :(得分:1)

我假设此问题陈述的第一列为空白

您可以覆盖TableModel getValueAt(int row, int column)方法。

@Override
public Object getValueAt(int row, int column){
  Object value = super.getValueAt(row, column);//Or get it from the Vector defined
  if(column == 2) {//Demo for the third column do same for other columns
    //Check the value in the first column if it is coming null
    if (null == getValueAt(row, 0) || getValueAt(row, 0) == ""){
      return null; // null means blank cell
    }
  }
  return value;
}

答案 2 :(得分:0)

我正在使用Jtable bean绑定并使用beansbinding-1.2.1.jar api进行自动绑定。我下载了beansbinding-1.2.1.jar源代码并在类中进行了相关更改

/org.jdesktop.swingbinding.JTableBinding.java

containing the class BindingTableModel.java which implements the TableModel and I overridden the method as per the suggestions of above two friends and thanks to all...

@覆盖

public Object getValueAt(int row, int column) {
            Object value = valueAt(row, column);

            if (value != null
                    && (value.toString().equals("0") || value.toString()
                            .equals("0.0")|| value.toString().equals("default"))) {
                return null;
            }

            return value;
        }
相关问题