0

スクロールバーがなくてもすべて問題ありませんが、さらに行があり、テーブルビューでスクロールバーを使用し、上下にスクロールすると、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);
        }
    }
}
4

1 に答える 1

1

スクロール後に updateItem() が既存の唯一のコンボボックスを各セルに設定し、視覚的なマングリングを引き起こすため、anyMethod クラスのコンボボックスとテキストフィールドのメンバーを作成する必要があります。

于 2013-05-14T10:02:27.563 に答える