没有滚动条一切都很好但是如果有更多的行并在表格视图中使用滚动条,我向下或向上滚动它会自动删除 Textfield 的图形。
public class ComboBoxRefresh extends Application {
TableColumn answerTypeCol;
TableColumn answerCol;
ObservableList<String> answerSelectList;
ComboBox comboBox;
TextField textField;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(final Stage primaryStage) {
primaryStage.setTitle("Table Cell With Multiple Components");
TableView<AnswerOption> table = new TableView<AnswerOption>();
table.setEditable(true);
final ObservableList<AnswerOption> data =
FXCollections.observableArrayList(
new AnswerOption("A", "Multiple Choice"),
new AnswerOption("1", "Free Text"),
new AnswerOption("78", "Free Text"),
new AnswerOption("D", "Multiple Choice"),
new AnswerOption("A", "Multiple Choice"),
new AnswerOption("2", "Free Text"),
new AnswerOption("24", "Free Text"),
new AnswerOption("D", "Multiple Choice"),
new AnswerOption("A", "Multiple Choice"),
new AnswerOption("7", "Free Text"),
new AnswerOption("123", "Free Text"),
new AnswerOption("D", "Multiple Choice"),
new AnswerOption("A", "Multiple Choice"),
new AnswerOption("3", "Free Text"),
new AnswerOption("123", "Free Text"),
new AnswerOption("D", "Multiple Choice"),
new AnswerOption("A", "Multiple Choice"),
new AnswerOption("5", "Free Text"),
new AnswerOption("123", "Free Text"),
new AnswerOption("D", "Multiple Choice")
);
GridPane gridpane = new GridPane();
gridpane.setPadding(new Insets(5));
gridpane.setHgap(5);
gridpane.setVgap(5);
answerSelectList = FXCollections.observableArrayList("A", "B", "C", "D", "INVALID_ANSWER", "NO_ANSWER");
answerCol = new TableColumn();
answerCol.setText("Answers");
answerCol.setMinWidth(210);
answerCol.setEditable(true);
answerCol.setCellValueFactory(new PropertyValueFactory("answers"));
answerCol.setCellFactory( new Callback<TableColumn<String, String>, TableCell<String, String>>() {
@Override
public TableCell<String, String> call(TableColumn<String, String> arg0) {
return new anyMethod();
}
});
answerTypeCol = new TableColumn();
answerTypeCol.setText("Answers Type");
answerTypeCol.setMinWidth(210);
answerTypeCol.setEditable(true);
answerTypeCol.setCellValueFactory(new PropertyValueFactory("answersType"));
table.setItems(data);
table.getColumns().addAll(answerCol, answerTypeCol);
StackPane root = new StackPane();
Scene scene =new Scene(root, 500, 550);
gridpane.add(table, 1, 5,1,20 );
root.getChildren().addAll(gridpane);
primaryStage.setScene(scene);
primaryStage.show();
}
private class anyMethod extends TableCell <String, String>{
public anyMethod(){
comboBox = new ComboBox();
textField = new TextField();
comboBox.setItems(answerSelectList);
}
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setText(null);
setGraphic(null);
System.out.println("In empty");
} else {
if( getTableView().getColumns().get(1).getCellData(getIndex()).toString().startsWith("M")){
System.out.println("Making ComboBox");
setGraphic(comboBox);
}
else{
setGraphic(textField);
}
}
}
}
public static class AnswerOption {
private final SimpleStringProperty answers;
private final SimpleStringProperty answersType;
private AnswerOption(String answers, String answersType) {
this.answers = new SimpleStringProperty(answers);
this.answersType = new SimpleStringProperty(answersType);
}
public String getAnswers() {
return answers.get();
}
public void setAnswers(String answers) {
this.answers.set(answers);
}
public String getAnswersType() {
return answersType.get();
}
public void setAnswersType(String answersType) {
this.answersType.set(answersType);
}
}
}