如何将枚举值加载到 TableView Javafx 中的组合框
How to load Enumeration Values to Combobox in TableView Javafx
我想将枚举值添加到 Combobox 到 TableView (Javafx)。
我在网上尝试了多个代码,但是由于字符串到枚举的转换问题,唯一有效的代码在尝试添加编辑事件时停止了工作。
这是我的代码:
this.tv_ListUsers_cl_Etat.setCellValueFactory(new PropertyValueFactory<>("etat"));
this.tv_ListUsers_cl_Etat.setCellFactory(TextFieldTableCell.<Utilisateur, Utilisateur.Etat>forTableColumn());
tv_ListUsers_cl_Etat.setOnEditCommit(
(CellEditEvent<Utilisateur, Utilisateur.Etat> t) -> {
if (t.getNewValue().equals("actif")) {
((Utilisateur) t.getTableView().getItems().get(
t.getTablePosition().getRow())).setEtat(User.Etat.Actif);
} else {
((Utilisateur) t.getTableView().getItems().get(
t.getTablePosition().getRow())).setEtat(User.Etat.Bloque);
}
});
其中 tv_ListUsers_cl_Etat 是我要放置 Etat 值的列:
private TableColumn<Utilisateur, Utilisateur.Etat> tv_ListUsers_cl_Etat;
Utilisateur.Etat :
public enum Etat {
Actif,Bloque}
PS :此代码适用于 TextField 而不是 Combobox,即使这样也不再有效。
这是出现的错误的屏幕截图:
PS:我修改了我的代码:
this.tv_ListUsers_cl_Etat.setCellValueFactory(new PropertyValueFactory<>("etat"));
Utilisateur User = new Utilisateur();
this.tv_ListUsers_cl_Etat.setCellFactory(ComboBoxTableCell.<Utilisateur,Utilisateur.Etat>forTableColumn(User.getEtat()));
tv_ListUsers_cl_Etat.setOnEditCommit(
(CellEditEvent<Utilisateur, Utilisateur.Etat> t) -> {
if (t.getNewValue().equals(User.Etat.Actif)) {
((Utilisateur) t.getTableView().getItems().get(
t.getTablePosition().getRow())).setEtat(User.Etat.Actif);
} else {
((Utilisateur) t.getTableView().getItems().get(
t.getTablePosition().getRow())).setEtat(User.Etat.Bloque);
}
});
现在我明白了:
我的问题是:如何使用编辑事件在 Tableview 的组合框中加载 Utilisateur.Etat 的两个值?
此代码有效:
Etat etat = Etat.Actif ;
this.tv_ListUsers_cl_Etat.setCellFactory(ComboBoxTableCell.<Utilisateur,Etat>forTableColumn(etat.values()));
tv_ListUsers_cl_Etat.setOnEditCommit(
(CellEditEvent<Utilisateur, Etat> t) -> {
if (t.getNewValue().equals(Etat.Actif)) {
((Utilisateur) t.getTableView().getItems().get(
t.getTablePosition().getRow())).setEtat(Etat.Actif);
} else {
((Utilisateur) t.getTableView().getItems().get(
t.getTablePosition().getRow())).setEtat(Etat.Bloque);
}
});
我添加了 Etat.java 包含这个:
public enum Etat {
Actif,Bloque
}
我想将枚举值添加到 Combobox 到 TableView (Javafx)。 我在网上尝试了多个代码,但是由于字符串到枚举的转换问题,唯一有效的代码在尝试添加编辑事件时停止了工作。 这是我的代码:
this.tv_ListUsers_cl_Etat.setCellValueFactory(new PropertyValueFactory<>("etat"));
this.tv_ListUsers_cl_Etat.setCellFactory(TextFieldTableCell.<Utilisateur, Utilisateur.Etat>forTableColumn());
tv_ListUsers_cl_Etat.setOnEditCommit(
(CellEditEvent<Utilisateur, Utilisateur.Etat> t) -> {
if (t.getNewValue().equals("actif")) {
((Utilisateur) t.getTableView().getItems().get(
t.getTablePosition().getRow())).setEtat(User.Etat.Actif);
} else {
((Utilisateur) t.getTableView().getItems().get(
t.getTablePosition().getRow())).setEtat(User.Etat.Bloque);
}
});
其中 tv_ListUsers_cl_Etat 是我要放置 Etat 值的列:
private TableColumn<Utilisateur, Utilisateur.Etat> tv_ListUsers_cl_Etat;
Utilisateur.Etat :
public enum Etat {
Actif,Bloque}
PS :此代码适用于 TextField 而不是 Combobox,即使这样也不再有效。
这是出现的错误的屏幕截图:
PS:我修改了我的代码:
this.tv_ListUsers_cl_Etat.setCellValueFactory(new PropertyValueFactory<>("etat"));
Utilisateur User = new Utilisateur();
this.tv_ListUsers_cl_Etat.setCellFactory(ComboBoxTableCell.<Utilisateur,Utilisateur.Etat>forTableColumn(User.getEtat()));
tv_ListUsers_cl_Etat.setOnEditCommit(
(CellEditEvent<Utilisateur, Utilisateur.Etat> t) -> {
if (t.getNewValue().equals(User.Etat.Actif)) {
((Utilisateur) t.getTableView().getItems().get(
t.getTablePosition().getRow())).setEtat(User.Etat.Actif);
} else {
((Utilisateur) t.getTableView().getItems().get(
t.getTablePosition().getRow())).setEtat(User.Etat.Bloque);
}
});
现在我明白了:
我的问题是:如何使用编辑事件在 Tableview 的组合框中加载 Utilisateur.Etat 的两个值?
此代码有效:
Etat etat = Etat.Actif ;
this.tv_ListUsers_cl_Etat.setCellFactory(ComboBoxTableCell.<Utilisateur,Etat>forTableColumn(etat.values()));
tv_ListUsers_cl_Etat.setOnEditCommit(
(CellEditEvent<Utilisateur, Etat> t) -> {
if (t.getNewValue().equals(Etat.Actif)) {
((Utilisateur) t.getTableView().getItems().get(
t.getTablePosition().getRow())).setEtat(Etat.Actif);
} else {
((Utilisateur) t.getTableView().getItems().get(
t.getTablePosition().getRow())).setEtat(Etat.Bloque);
}
});
我添加了 Etat.java 包含这个:
public enum Etat {
Actif,Bloque
}