Javafx 如何在tableview(FXML)中显示字符串而不是整数
我的数据库表中有一个存储整数的字段。为了使程序更加用户友好,我希望在TableView和ComboBox中将数据表示为字符串,而不是整数。例如,它应该显示“不支持”而不是0,它应该显示“支持”而不是1。这是我的一个领域:Javafx 如何在tableview(FXML)中显示字符串而不是整数,javafx,combobox,tableview,fxml,Javafx,Combobox,Tableview,Fxml,我的数据库表中有一个存储整数的字段。为了使程序更加用户友好,我希望在TableView和ComboBox中将数据表示为字符串,而不是整数。例如,它应该显示“不支持”而不是0,它应该显示“支持”而不是1。这是我的一个领域: private final SimpleIntegerProperty status = new ReadOnlyIntegerWrapper(); public int getStatus() { return status.get(); } public void
private final SimpleIntegerProperty status = new ReadOnlyIntegerWrapper();
public int getStatus() {
return status.get();
}
public void setStatus(int val)
{
status.set(val);
}
我使用FXML成功地检索数据并将数据注入TableView,但它显示0或1;如何在Java中实现这一点
我需要一个组合框解决方案,以便用户可以选择有意义的字符串而不是数字。创建您的
StringConverter
,它可以将每个状态转换为您想要的表达式
public class StatusStringConverter extends StringConverter<Integer> {
// Manage selectable options as Integer here
final public static ObservableList<Integer> OPTIONS = FXCollections.observableArrayList(0, 1);
@Override
public String toString(Integer value) {
switch (value) {
case 0: return "unsupported";
case 1: return "supported";
default: return "-";
}
}
@Override
public Integer fromString(String string) {
return null;
}
}
正是我想要的。thnx
table.setEditable(true);
column.setEditable(true);
column.setCellValueFactory(new PropertyValueFactory<>("status"));
column.setCellFactory(ComboBoxTableCell.forTableColumn(
new StatusStringConverter(), StatusStringConverter.OPTIONS));