没有滚动条一切都很好,但是如果有更多行并在 TableView 中使用滚动条,并且我向下或向上滚动它,文本字段的图形会自动删除。
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);
}
}
}
最佳答案
您应该使组合框和文本字段成为anyMethod类的成员,因为滚动后updateItem()将唯一现有的组合框设置到每个单元格,这会导致视觉困惑。
关于java - 当我滚动鼠标时,图形从我设置的表格中删除了文本字段和下拉列表。请帮助某人。提前致谢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16536744/