【问题标题】:JTable binding Avoiding Default ValueJTable 绑定避免默认值
【发布时间】:2012-05-01 18:13:31
【问题描述】:

我正在使用 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

任何可以提出解决此问题的更好方法的人,它将非常完整,并在此先感谢。

【问题讨论】:

    标签: java swing data-binding jtable


    【解决方案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;
    }
    

    【讨论】:

    • 此解决方案的唯一问题是 API 中的默认值将不复存在,因为它将被 null 覆盖,即使在需要默认值的地方也是如此。
    【解决方案2】:

    我假设这个问题陈述的第一列是空白的

    您可以覆盖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;
    }
    

    【讨论】:

      【解决方案3】:

      我使用 Jtable beans 绑定并使用 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...
      

      @Override

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

      【讨论】:

      • 我不认为这是你想要的方式......我建议在你使用它的地方扩展 BindingTableModel - 否则你坚持在任何其他情况下你可能想要使用 BindingTableModel也会得到这种行为。一般来说,去更改第三方 jar 的来源通常是个坏主意。 (同样,接受答案是一种很好的做法......)
      • 嘿,私有最终类 BindingTableModel 扩展 ListBindingManager 实现 TableModel 。是JTableBinding里面BidingTableModel的结构,那怎么可能扩展BindingTableModel类呢?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-21
      • 2022-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多