java - 当我滚动鼠标时,图形从我设置的表格中删除了文本字段和下拉列表。请帮助某人。提前致谢

标签 java javafx-2

没有滚动条一切都很好,但是如果有更多行并在 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/

相关文章:

java - 将文本文件附加到 StringBuffer 后,有没有办法恢复换行符?

java - 使用 CGLIB 异常设置属性值

java - RotateTransition 当前角度 javafx2

java - 如何将 byte[] 转换为 Byte[] 和其他方式?

java - 从 DB2 获取超过 49 条记录的数据需要更多时间

JavaFX - 为什么将节点多次添加到一个 Pane 或添加到不同的 Pane 会导致错误?

java - 我应该什么时候创建类以避免庞大的 main 方法?

user-interface - 从 Controller 内关闭阶段

reflection - 按名称检索属性

java - 如何将字节数组写入 Java 中的文件?