获取ComboBoxTableCell的值
我使用此代码将ComboBoxCell放入名为“ComboTable”的表中。前两列的数据由下面的数据库连接给出获取ComboBoxTableCell的值,combobox,javafx,tableview,Combobox,Javafx,Tableview,我使用此代码将ComboBoxCell放入名为“ComboTable”的表中。前两列的数据由下面的数据库连接给出 ComboTable.setEditable(true); col1.setCellValueFactory(new PropertyValueFactory<model, String>("rCol1")); col2.setCellValueFactory(new PropertyValueFactory<model, Stri
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));
}});
如果是我的程序,我在这一部分的错误在哪里?还是以错误的方式获取值
顺便说一下,这是我的程序的输出:
*** 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 ***
我在这里点击了表格行->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);
}
}
包模型;
导入controller.main\u controller.ComboValues;
导入javafx.beans.property.ObjectProperty;
导入javafx.beans.property.SimpleObject属性;
导入javafx.beans.property.SimpleStringProperty;
导入javafx.beans.property.StringProperty;
公共类模型{
私有StringProperty rCol1=新的SimpleStringProperty();
私有StringProperty rCol2=新的SimpleStringProperty();
private ObjectProperty rCol3=新的SimpleObject属性();
公共模型(字符串sCol1、字符串sCol2、组合值sCol3){
setCol1(sCol1);
setCol2(sCol2);
设置组合值(sCol3);
}
公共最终字符串属性col1Property(){
返回此文件。rCol1;
}
公共最终字符串setCol1(){
返回此.col1Property().get();
}
公共最终无效集合col1(最终字符串col1){
this.col1Property().set(col1);
}
公共最终StringProperty col2Property(){
返回此文件。rCol2;
}
公共最终字符串setCol2(){
返回此.col2Property().get();
}
公共最终无效集合col2(最终字符串col2){
this.col1Property().set(col2);
}
公共最终对象属性组合值属性(){
返回此文件。rCol3;
}
公共最终ComboValues getComboValues(){
返回此.combovaluesProperty().get();
}
公共最终无效集合组合值(最终组合值rCol3){
此.combovaluesProperty().set(rCol3);
}
}
问题似乎是您没有为该列设置cellValueFactory
,因此没有与之关联的数据。因此,当您调用getCellData(…)
来查找单元格的值时,它(当然)返回null
。只需在映射到模型的列上设置一个cellValueFactory
,方法与使用默认cellFactory
对列所做的相同
以下是使用Oracle教程中的标准联系人表的示例:
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);
}
}
导入java.util.array;
导入javafx.application.application;
导入javafx.beans.property.ObjectProperty;
导入javafx.beans.property.SimpleObject属性;
导入javafx.beans.property.SimpleStringProperty;
导入javafx.beans.property.StringProperty;
导入javafx.collections.FXCollections;
导入javafx.collections.ObservableList;
导入javafx.scene.scene;
导入javafx.scene.control.TableColumn;
导入javafx.scene.control.TableView;
导入javafx.scene.control.cell.ComboBoxTableCell;
导入javafx.scene.layout.BorderPane;
导入javafx.stage.stage;
公共类TableViewWithComboColumn扩展了应用程序{
公共枚举类别{朋友、家人、工作、其他}
@凌驾
公共无效开始(阶段primaryStage){
TableView table=新TableView();
table.setEditable(true);
TableColumn firstNameCol=新的TableColumn(“名字”);
TableColumn lastNameCol=新的TableColumn(“姓氏”);
TableColumn emailCol=新的TableColumn(“电子邮件”);
TableColumn categoryCol=新的TableColumn(“类别”);
firstNameCol.setCellValueFactory(cellData->cellData.getValue().firstNameProperty());
//或firstNameCol.setCellValueFactory(新属性ValueFactory(“firstName”))等
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 tableData=createData();
表.设置项(表数据);
table.getSelectionModel().selectedItemProperty().addListener((obs、oldSelectedPerson、newSelectedPerson)->{
int tableIndex=tableData.indexOf(newSelectedPerson);
System.out.println(“索引:\t”+tableIndex);
System.out.println(categoryCol.getCellData(tableIndex));
//更好:只需从模型中获取数据:
System.out.println(newSelectedPerson.getCategory());
});
table.getColumns().addAll(Arrays.asList(
firstNameCol、lastNameCol、emailCol、categoryCol
));
设置场景(新场景(新边框窗格(表),800600));
primaryStage.show();
}
私有ObservableList createData(){
返回FXCollections.observableArrayList(
新人(“雅各布”、“史密斯”、“雅各布”。smith@example.com“、类别、家庭),
新人(“伊莎贝拉”、“约翰逊”、“伊莎贝拉”。johnson@example.com“、类别、朋友),
新人(“伊桑”、“威廉姆斯”、“伊桑”。williams@example.com“、类别、工作),
新人(“艾玛”、“琼斯”、“艾玛”。jones@example.com“、类别、工作),
新人(“迈克尔”、“布朗”、“迈克尔”。brown@example.com“,类别。其他)
);
}
公共静态类人员{
private final StringProperty firstName=新SimpleStringProperty();
private final StringProperty lastName=新的SimpleStringProperty();
private final StringProperty email=新SimpleStringProperty();
private final ObjectProperty category=new SimpleObjectProperty();
公众人物(stringfirstname、stringlastname、stringemail、Category){
setFirstName(firstName);
setLastName(lastName);
设置电子邮件(电子邮件);
集合类别(类别);
}
公共财产优先
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);
}
}