我使用以下代码成功地将JTextField绑定到JTable中的选定行:
public static final void BindTableToFields(JTable Table, Object[] Fields) {
for (Object Field : Fields) {
BeanProperty<JTable, Object> tableBeanProperty;
String DesignCallID;
String PropertyName;
if (Field instanceof JTextField) {
BeanProperty<JTextField, String> BindingFieldProperty;
Binding<JTable, Object, JTextField, String> binding;
JTextField jTextField = (JTextField) Field;
DesignCallID = jTextField.getText();
PropertyName = "selectedElement." + GetCorrespondingColumn(Table, DesignCallID);
jTextField.setText("");
System.out.println("property name =" + PropertyName.trim());
tableBeanProperty = BeanProperty.create(PropertyName);
BindingFieldProperty = BeanProperty.create("text");
binding = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE,
Table,
tableBeanProperty,
jTextField,
BindingFieldProperty);
Converter<Object, String> old = binding.getConverter();
Converter<Object, String> converter = new Converter<Object, String>() {
@Override
public String convertForward(Object value) {
try {
if (value instanceof HashMap) {
HashMap hashMap = (HashMap) value;
System.out.println(hashMap);
// Object get = hashMap.get(GetCorrespondingColumn(Table, DesignCallID));
return (String) hashMap.get(GetCorrespondingColumn(Table, DesignCallID));
} else if (value instanceof String) {
return (String) value;
} else if (value instanceof Integer) {
Integer integer = (Integer) value;
return integer.toString();
} else if (value instanceof Float) {
Float float1 = (Float) value;
return float1.toString();
} else if (value instanceof Double) {
Double double1 = (Double) value;
return double1.toString();
} else if (value instanceof Date) {
Date date = (Date) value;
return date.toLocalDate().format(DateTimeFormatter.ISO_LOCAL_DATE);
}
MyLogger.Log_to_local(new Exception("Binding Class Not Handled" + value.getClass().getName()));
return "";
} catch (Exception e) {
e.printStackTrace();
MyLogger.Log_to_local(e);
}
return "impossible";
}
@Override
public Object convertReverse(String value) {
try {
return value;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
};
binding.setConverter(converter);
binding.bind();
} else
//
//
// other component as JPasswordField .....
然后它来了我尝试绑定JComboBox的部分我尝试了这两个版本:
if (Field instanceof JComboBox) {
//<editor-fold>
System.out.println("binding ");
JComboBox jComboBox = (JComboBox) Field;
BeanProperty<JComboBox, String> BindingFieldProperty;
Binding<JTable, Object, JComboBox, String> binding;
DesignCallID = jComboBox.getName();
PropertyName = "selectedElement." + GetCorrespondingColumn(Table, DesignCallID);
System.out.println("property name =" + PropertyName.trim());
tableBeanProperty = BeanProperty.create(PropertyName);
BindingFieldProperty = BeanProperty.create("selectedItem");
binding = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE,
Table,
tableBeanProperty,
jComboBox,
BindingFieldProperty);
Converter<Object, String> old = binding.getConverter();
Converter<Object, String> converter = new Converter<Object, String>() {
@Override
public String convertForward(Object value) {
try {
if (value instanceof HashMap) {
HashMap hashMap = (HashMap) value;
System.out.println(hashMap);
// Object get = hashMap.get(GetCorrespondingColumn(Table, DesignCallID));
return (String) hashMap.get(GetCorrespondingColumn(Table, DesignCallID));
} else if (value instanceof String) {
return (String) value;
} else if (value instanceof Integer) {
Integer integer = (Integer) value;
return integer.toString();
} else if (value instanceof Float) {
Float float1 = (Float) value;
return float1.toString();
} else if (value instanceof Double) {
Double double1 = (Double) value;
return double1.toString();
} else if (value instanceof Date) {
Date date = (Date) value;
return date.toLocalDate().format(DateTimeFormatter.ISO_LOCAL_DATE);
}
MyLogger.Log_to_local(new Exception("Binding Class Not Handled" + value.getClass().getName()));
return "";
} catch (Exception e) {
e.printStackTrace();
MyLogger.Log_to_local(e);
}
return "impossible";
}
@Override
public Object convertReverse(String value) {
try {
return value;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
};
binding.setConverter(converter);
binding.bind();
//</editor-fold>
}
,另一个版本是:
System.out.println("binding jcombobox");
JComboBox jComboBox = (JComboBox) Field;
BeanProperty<JComboBox, Integer> BindingFieldProperty;
Binding<JTable, Object, JComboBox, Integer> binding;
DesignCallID = jComboBox.getName();
PropertyName = "selectedElement." + GetCorrespondingColumn(Table, DesignCallID);
System.out.println("property name =" + PropertyName.trim());
tableBeanProperty = BeanProperty.create(PropertyName);
BindingFieldProperty = BeanProperty.create("selectedItem");
binding = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE,
Table,
tableBeanProperty,
jComboBox,
BindingFieldProperty);
Converter<Object, Integer> old = binding.getConverter();
Converter<Object, Integer> converter = new Converter<Object, Integer>() {
@Override
public Integer convertForward(Object value) {
try {
if (value instanceof HashMap) {
HashMap hashMap = (HashMap) value;
System.out.println(hashMap);
// Object get = hashMap.get(GetCorrespondingColumn(Table, DesignCallID));
return Integer.parseInt(""+hashMap.get(GetCorrespondingColumn(Table, DesignCallID)));
} else if (value instanceof String) {
return Integer.parseInt(""+value);
} else if (value instanceof Integer) {
Integer integer = (Integer) value;
return integer;
} else if (value instanceof Float) {
Float float1 = (Float) value;
return float1.intValue();
} else if (value instanceof Double) {
Double double1 = (Double) value;
return double1.intValue();
} else if (value instanceof Date) {
Date date = (Date) value;
MyLogger.Log_to_local(new Exception("Binding Class Not Handled" + value.getClass().getName()));
return 1;
}
MyLogger.Log_to_local(new Exception("Binding Class Not Handled" + value.getClass().getName()));
return 1;
} catch (Exception e) {
e.printStackTrace();
MyLogger.Log_to_local(e);
}
return 1;
}
@Override
public Object convertReverse(Integer value) {
try {
return value;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
};
binding.setConverter(converter);
binding.bind();
我不知道我错过了,它对JcomboBox不起作用! 那么请有任何错误或错过的逻辑告诉我,还有一个解决方案,通过代码将我的JComboBox绑定到我的JTable告诉我如何。 感谢
答案 0 :(得分:-1)
首先,IMO,您为代码提供了100多行代码,因此大多数人都不愿意回答这个问题。
回到您的问题,可能的答案是使用setCellEditor
方法。如下面的代码
//provided you have instatiate the table.
//then instantiate the `TableColumn` object to accept the column
TableColumn sportColumn = table.getColumnModel().getColumn(2);
...
//add the desired items to be selected by the user
JComboBox comboBox = new JComboBox();
comboBox.addItem("Snowboarding");
comboBox.addItem("Rowing");
comboBox.addItem("Chasing toddlers");
comboBox.addItem("Speed reading");
comboBox.addItem("Teaching high school");
comboBox.addItem("None");
//add the combo box into the column
sportColumn.setCellEditor(new DefaultCellEditor(comboBox));
这里是参考:https://docs.oracle.com/javase/tutorial/uiswing/components/table.html#combobox