我使用此代码将ComboBoxCell放入名为" 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));
}});
所以现在我想通过使用更改侦听器在表中设置值后接收值。我认为这个代码在我为组合单体选择一个值后给了我值:
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 ***
我点击这里的表格行 - &gt;的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);
}
}
答案 0 :(得分:2)
问题似乎是您没有为该列设置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);
}
}