Java 避免默认值的JTable绑定
我正在使用Java beans绑定Jtable,其中api为整数或浮点值(如0或0.0)提供默认值,如下所示。我希望避免使用相应的默认值,并将单元格设置为空,但最后一个单元格值除外Java 避免默认值的JTable绑定,java,swing,data-binding,jtable,Java,Swing,Data Binding,Jtable,我正在使用Java beans绑定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 6
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
任何人如果能提出更好的方法来解决这个问题,都将不胜感激 我建议在
TableModel
中执行此操作,尤其是使用getValueAt(int行,int列)
方法。比如:
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;
}
我假设此问题陈述的第一列为空 您可以重写
TableModel
getValueAt(int行,int列)
方法
@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;
}
我使用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;
}
这个解决方案唯一的问题是API中的默认值将不再存在,因为它将被null覆盖,即使在需要默认值的地方也是如此。我认为这不是您想要的方式。。。我建议您在使用BindingTableModel的地方扩展它,否则您会坚持认为您可能希望使用BindingTableModel的任何其他情况也会出现这种行为。一般来说,改变第三方jar的源代码通常是个坏主意。(同样,接受答案也是一种很好的做法…)嘿,私有最终类BindingTableModel扩展了ListBindingManager实现了TableModel。BidingTableModel的结构是否在JTableBinding中,那么如何扩展BindingTableModel类。?