获取 ComboBoxTableCell 的值
Get value of a ComboBoxTableCell
我使用此代码将一个 ComboBoxCell 放入我的 Table 命名为 "ComboTable"。前两列的数据由此代码下方的数据库连接给出。
ComboTable.setEditable(true);
col1.setCellValueFactory(new PropertyValueFactory<model, String>("rCol1"));
col2.setCellValueFactory(new PropertyValueFactory<model, String>("rCol2"));
col3.setCellValueFactory(new PropertyValueFactory<model, String>("rCol3"));
col3.setCellFactory(ComboBoxTableCell.forTableColumn(cbValues));
// ChangeListener
ComboTable.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Object>() {
@Override
public void changed (ObservableValue<?> oValue, Object oldValue, Object newValue) {
tableIndexProperty.set(tableData.indexOf(newValue));
tableIndex = (tableData.indexOf(newValue));
System.out.println("Index:\t" + tableIndex);
System.out.println(col3.getCellData(tableIndex));
}});
所以现在我想通过使用更改侦听器在 table 中设置值后接收值。在我为组合框tablecell:
选择一个值后,我认为这段代码给了我值
System.out.println(col3.getCellData(tableIndex));
如果是我的程序,我在这部分哪里出错了,还是获取值的方式不对?
顺便说一句,这是我的程序的输出:
*** Loaded Oracle-Driver ***
*** Connected with Database ***
Index of Databaserow: 1
Daten1: one Daten2: two
Index of Databaserow: 2
Daten1: tree Daten2: four
*** Database data saved to Observable List named 'data' ***
*** Table Items setted ***
我点击了此处的 table 行 -> ChangeListener:
Index: 1
null
Index: 0
null
Index: 1
null
Index: 0
null
型号:
package model;
import controller.main_controller.ComboValues;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
public class model {
private StringProperty rCol1 = new SimpleStringProperty();
private StringProperty rCol2 = new SimpleStringProperty();
private ObjectProperty<ComboValues> rCol3 = new SimpleObjectProperty<>();
public model(String sCol1, String sCol2, ComboValues sCol3) {
setCol1(sCol1);
setCol2(sCol2);
setComboValues(sCol3);
}
public final StringProperty col1Property() {
return this.rCol1;
}
public final String setCol1() {
return this.col1Property().get();
}
public final void setCol1(final String col1) {
this.col1Property().set(col1);
}
public final StringProperty col2Property() {
return this.rCol2;
}
public final String setCol2() {
return this.col2Property().get();
}
public final void setCol2(final String col2) {
this.col1Property().set(col2);
}
public final ObjectProperty<ComboValues> combovaluesProperty() {
return this.rCol3;
}
public final ComboValues getComboValues() {
return this.combovaluesProperty().get();
}
public final void setComboValues(final ComboValues rCol3) {
this.combovaluesProperty().set(rCol3);
}
}
问题似乎是您尚未为该列设置 cellValueFactory
,因此没有与之关联的数据。因此,当您调用 getCellData(...)
以查找单元格的值时,它(当然)是 returns null
。只需在映射到模型中的列上设置一个 cellValueFactory
,就像使用默认值 cellFactory
对列所做的一样。
这是一个示例,使用 Oracle 教程中的标准联系人 table:
import java.util.Arrays;
import javafx.application.Application;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.ComboBoxTableCell;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class TableViewWithComboColumn extends Application {
public enum Category { Friends, Family, Work, Other }
@Override
public void start(Stage primaryStage) {
TableView<Person> table = new TableView<>();
table.setEditable(true);
TableColumn<Person, String> firstNameCol = new TableColumn<>("First name");
TableColumn<Person, String> lastNameCol = new TableColumn<>("Last name");
TableColumn<Person, String> emailCol = new TableColumn<>("Email");
TableColumn<Person, Category> categoryCol = new TableColumn<>("Category");
firstNameCol.setCellValueFactory(cellData -> cellData.getValue().firstNameProperty());
// or firstNameCol.setCellValueFactory(new PropertyValueFactory<>("firstName")); etc
lastNameCol.setCellValueFactory(cellData -> cellData.getValue().lastNameProperty());
emailCol.setCellValueFactory(cellData -> cellData.getValue().emailProperty());
categoryCol.setCellValueFactory(cellData -> cellData.getValue().categoryProperty());
categoryCol.setCellFactory(ComboBoxTableCell.forTableColumn(Category.values()));
ObservableList<Person> tableData = createData();
table.setItems(tableData);
table.getSelectionModel().selectedItemProperty().addListener((obs, oldSelectedPerson, newSelectedPerson) -> {
int tableIndex = tableData.indexOf(newSelectedPerson) ;
System.out.println("Index:\t" + tableIndex);
System.out.println(categoryCol.getCellData(tableIndex));
// better: just get the data from the model:
System.out.println(newSelectedPerson.getCategory());
});
table.getColumns().addAll(Arrays.asList(
firstNameCol, lastNameCol, emailCol, categoryCol
));
primaryStage.setScene(new Scene(new BorderPane(table), 800, 600));
primaryStage.show();
}
private ObservableList<Person> createData() {
return FXCollections.observableArrayList(
new Person("Jacob", "Smith", "jacob.smith@example.com", Category.Family),
new Person("Isabella", "Johnson", "isabella.johnson@example.com", Category.Friends),
new Person("Ethan", "Williams", "ethan.williams@example.com", Category.Work),
new Person("Emma", "Jones", "emma.jones@example.com", Category.Work),
new Person("Michael", "Brown", "michael.brown@example.com", Category.Other)
);
}
public static class Person {
private final StringProperty firstName = new SimpleStringProperty();
private final StringProperty lastName = new SimpleStringProperty();
private final StringProperty email = new SimpleStringProperty() ;
private final ObjectProperty<Category> category = new SimpleObjectProperty<>();
public Person(String firstName, String lastName, String email, Category category) {
setFirstName(firstName);
setLastName(lastName);
setEmail(email);
setCategory(category);
}
public final StringProperty firstNameProperty() {
return this.firstName;
}
public final String getFirstName() {
return this.firstNameProperty().get();
}
public final void setFirstName(final String firstName) {
this.firstNameProperty().set(firstName);
}
public final StringProperty lastNameProperty() {
return this.lastName;
}
public final String getLastName() {
return this.lastNameProperty().get();
}
public final void setLastName(final String lastName) {
this.lastNameProperty().set(lastName);
}
public final StringProperty emailProperty() {
return this.email;
}
public final String getEmail() {
return this.emailProperty().get();
}
public final void setEmail(final String email) {
this.emailProperty().set(email);
}
public final ObjectProperty<Category> categoryProperty() {
return this.category;
}
public final Category getCategory() {
return this.categoryProperty().get();
}
public final void setCategory(final Category category) {
this.categoryProperty().set(category);
}
}
public static void main(String[] args) {
launch(args);
}
}
我使用此代码将一个 ComboBoxCell 放入我的 Table 命名为 "ComboTable"。前两列的数据由此代码下方的数据库连接给出。
ComboTable.setEditable(true);
col1.setCellValueFactory(new PropertyValueFactory<model, String>("rCol1"));
col2.setCellValueFactory(new PropertyValueFactory<model, String>("rCol2"));
col3.setCellValueFactory(new PropertyValueFactory<model, String>("rCol3"));
col3.setCellFactory(ComboBoxTableCell.forTableColumn(cbValues));
// ChangeListener
ComboTable.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Object>() {
@Override
public void changed (ObservableValue<?> oValue, Object oldValue, Object newValue) {
tableIndexProperty.set(tableData.indexOf(newValue));
tableIndex = (tableData.indexOf(newValue));
System.out.println("Index:\t" + tableIndex);
System.out.println(col3.getCellData(tableIndex));
}});
所以现在我想通过使用更改侦听器在 table 中设置值后接收值。在我为组合框tablecell:
选择一个值后,我认为这段代码给了我值System.out.println(col3.getCellData(tableIndex));
如果是我的程序,我在这部分哪里出错了,还是获取值的方式不对?
顺便说一句,这是我的程序的输出:
*** Loaded Oracle-Driver ***
*** Connected with Database ***
Index of Databaserow: 1
Daten1: one Daten2: two
Index of Databaserow: 2
Daten1: tree Daten2: four
*** Database data saved to Observable List named 'data' ***
*** Table Items setted ***
我点击了此处的 table 行 -> ChangeListener:
Index: 1
null
Index: 0
null
Index: 1
null
Index: 0
null
型号:
package model;
import controller.main_controller.ComboValues;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
public class model {
private StringProperty rCol1 = new SimpleStringProperty();
private StringProperty rCol2 = new SimpleStringProperty();
private ObjectProperty<ComboValues> rCol3 = new SimpleObjectProperty<>();
public model(String sCol1, String sCol2, ComboValues sCol3) {
setCol1(sCol1);
setCol2(sCol2);
setComboValues(sCol3);
}
public final StringProperty col1Property() {
return this.rCol1;
}
public final String setCol1() {
return this.col1Property().get();
}
public final void setCol1(final String col1) {
this.col1Property().set(col1);
}
public final StringProperty col2Property() {
return this.rCol2;
}
public final String setCol2() {
return this.col2Property().get();
}
public final void setCol2(final String col2) {
this.col1Property().set(col2);
}
public final ObjectProperty<ComboValues> combovaluesProperty() {
return this.rCol3;
}
public final ComboValues getComboValues() {
return this.combovaluesProperty().get();
}
public final void setComboValues(final ComboValues rCol3) {
this.combovaluesProperty().set(rCol3);
}
}
问题似乎是您尚未为该列设置 cellValueFactory
,因此没有与之关联的数据。因此,当您调用 getCellData(...)
以查找单元格的值时,它(当然)是 returns null
。只需在映射到模型中的列上设置一个 cellValueFactory
,就像使用默认值 cellFactory
对列所做的一样。
这是一个示例,使用 Oracle 教程中的标准联系人 table:
import java.util.Arrays;
import javafx.application.Application;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.ComboBoxTableCell;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class TableViewWithComboColumn extends Application {
public enum Category { Friends, Family, Work, Other }
@Override
public void start(Stage primaryStage) {
TableView<Person> table = new TableView<>();
table.setEditable(true);
TableColumn<Person, String> firstNameCol = new TableColumn<>("First name");
TableColumn<Person, String> lastNameCol = new TableColumn<>("Last name");
TableColumn<Person, String> emailCol = new TableColumn<>("Email");
TableColumn<Person, Category> categoryCol = new TableColumn<>("Category");
firstNameCol.setCellValueFactory(cellData -> cellData.getValue().firstNameProperty());
// or firstNameCol.setCellValueFactory(new PropertyValueFactory<>("firstName")); etc
lastNameCol.setCellValueFactory(cellData -> cellData.getValue().lastNameProperty());
emailCol.setCellValueFactory(cellData -> cellData.getValue().emailProperty());
categoryCol.setCellValueFactory(cellData -> cellData.getValue().categoryProperty());
categoryCol.setCellFactory(ComboBoxTableCell.forTableColumn(Category.values()));
ObservableList<Person> tableData = createData();
table.setItems(tableData);
table.getSelectionModel().selectedItemProperty().addListener((obs, oldSelectedPerson, newSelectedPerson) -> {
int tableIndex = tableData.indexOf(newSelectedPerson) ;
System.out.println("Index:\t" + tableIndex);
System.out.println(categoryCol.getCellData(tableIndex));
// better: just get the data from the model:
System.out.println(newSelectedPerson.getCategory());
});
table.getColumns().addAll(Arrays.asList(
firstNameCol, lastNameCol, emailCol, categoryCol
));
primaryStage.setScene(new Scene(new BorderPane(table), 800, 600));
primaryStage.show();
}
private ObservableList<Person> createData() {
return FXCollections.observableArrayList(
new Person("Jacob", "Smith", "jacob.smith@example.com", Category.Family),
new Person("Isabella", "Johnson", "isabella.johnson@example.com", Category.Friends),
new Person("Ethan", "Williams", "ethan.williams@example.com", Category.Work),
new Person("Emma", "Jones", "emma.jones@example.com", Category.Work),
new Person("Michael", "Brown", "michael.brown@example.com", Category.Other)
);
}
public static class Person {
private final StringProperty firstName = new SimpleStringProperty();
private final StringProperty lastName = new SimpleStringProperty();
private final StringProperty email = new SimpleStringProperty() ;
private final ObjectProperty<Category> category = new SimpleObjectProperty<>();
public Person(String firstName, String lastName, String email, Category category) {
setFirstName(firstName);
setLastName(lastName);
setEmail(email);
setCategory(category);
}
public final StringProperty firstNameProperty() {
return this.firstName;
}
public final String getFirstName() {
return this.firstNameProperty().get();
}
public final void setFirstName(final String firstName) {
this.firstNameProperty().set(firstName);
}
public final StringProperty lastNameProperty() {
return this.lastName;
}
public final String getLastName() {
return this.lastNameProperty().get();
}
public final void setLastName(final String lastName) {
this.lastNameProperty().set(lastName);
}
public final StringProperty emailProperty() {
return this.email;
}
public final String getEmail() {
return this.emailProperty().get();
}
public final void setEmail(final String email) {
this.emailProperty().set(email);
}
public final ObjectProperty<Category> categoryProperty() {
return this.category;
}
public final Category getCategory() {
return this.categoryProperty().get();
}
public final void setCategory(final Category category) {
this.categoryProperty().set(category);
}
}
public static void main(String[] args) {
launch(args);
}
}