【问题标题】:JavaFX ComboBox setButtonCellJavaFX 组合框 setButtonCell
【发布时间】:2014-02-28 09:17:51
【问题描述】:

我需要有关设置组合框 buttonCell 的帮助。 我使用一个组合框来显示来自可观察列表的数据,该列表包含来自具有两列“Step”和“NextStep”的表中的数据(NextStep 包含插入在列 Step 中的一个项目);我需要做的是显示带有“Step”列表的组合框列表单元和带有相对“NextStep”的按钮单元。现在,我可以正确看到列表单元,但我的按钮单元始终是空的。

代码:

    // SET THE VALUE STEP TO THE LISTCELL 
    comboStatoSuccessivo.setCellFactory(new Callback<ListView<StatoEsiti>, ListCell<StatoEsiti>>() {
        @Override public ListCell<StatoEsiti> call(ListView<StatoEsiti> p) {
            return new ListCell<StatoEsiti>() { 
                    @Override
                    protected void updateItem(StatoEsiti t, boolean bln) {
                        super.updateItem(t, bln);
                        if(t != null){
                            setText(t.statoProperty().getValue());
                            System.out.println("SET PROPERTY " + t.statoProperty().getValue());
                        } else {
                            setText(null);
                        }    

                    }                       
            };
        }
    });


    // SET THE VALUE NEXTSTEP TO THE BUTTONCELL
    comboStatoSuccessivo.setButtonCell(new ListCell<StatoEsiti>() {
        @Override
        protected void updateItem(StatoEsiti t, boolean bln) {
            super.updateItem(t, bln); 
            if (t != null) { <<<<<<<<<<<<<<-------------ALWAYS NULL----WHY??????
                setText(t.statoSuccessivoProperty().getValue());
                System.out.println("SET PROPERTY BUTTONCELL " + t.statoSuccessivoProperty().getValue());
            } else {
                setText(null);
                System.out.println("SET PROPERTY BUTTONCELL NULL");
            }

        }
    });

提前致谢。

【问题讨论】:

    标签: combobox javafx


    【解决方案1】:

    我已使用以下演示 SSCCE 代码研究了您的用例。
    它按预期工作,就像从组合框的下拉菜单中选择项目时,按钮单元格会更新为相关的“下一步”:

    public class ComboDemo extends Application {
    
        @Override
        public void start(Stage primaryStage) {
            List<Person> list = new ArrayList<Person>();
            list.add(new Person("step 1212", 12));
            list.add(new Person("step 4545", 45));
            list.add(new Person("step 5656", 56));
            list.add(new Person("step 9090", 90));
    
            ComboBox<Person> comboBox = new ComboBox<>(FXCollections.observableList(list));
    
            comboBox.setCellFactory(new Callback<ListView<Person>, ListCell<Person>>() {
                @Override
                public ListCell<Person> call(ListView<Person> p) {
                    return new ListCell<Person>() {
                        @Override
                        protected void updateItem(Person t, boolean bln) {
                            super.updateItem(t, bln);
                            if (t != null) {
                                setText(t.getStepProperty().getValue());
                                System.out.println("SET PROPERTY " + t.getStepProperty().getValue());
                            } else {
                                setText(null);
                            }
                        }
                    };
                }
            });
    
            // SET THE VALUE NEXTSTEP TO THE BUTTONCELL
            comboBox.setButtonCell(new ListCell<Person>() {
                @Override
                protected void updateItem(Person t, boolean bln) {
                    super.updateItem(t, bln);
                    if (t != null) {
                        setText(t.getNextStepProperty().getValue().toString());
                        System.out.println("SET PROPERTY BUTTONCELL " + t.getNextStepProperty().getValue());
                    } else {
                        setText(null);
                        System.out.println("SET PROPERTY BUTTONCELL NULL");
                    }
    
                }
            });
    
            StackPane root = new StackPane();
            root.getChildren().add(comboBox);
    
            Scene scene = new Scene(root, 300, 250);
    
            primaryStage.setScene(scene);
            primaryStage.show();
        }
    
        public static class Person {
            private StringProperty stepProperty = new SimpleStringProperty();
            private IntegerProperty nextStepProperty = new SimpleIntegerProperty();
    
            public Person(String step, Integer nextStep) {
                this.stepProperty.setValue(step);
                this.nextStepProperty.setValue(nextStep);
            }
    
            public StringProperty getStepProperty() {
                return stepProperty;
            }
    
            public void setStepProperty(StringProperty stepProperty) {
                this.stepProperty = stepProperty;
            }
    
            public IntegerProperty getNextStepProperty() {
                return nextStepProperty;
            }
    
            public void setNextStepProperty(IntegerProperty nextStepProperty) {
                this.nextStepProperty = nextStepProperty;
            }
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    
    }
    

    和你的比较一下。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-23
      • 2017-02-08
      • 2015-03-06
      • 1970-01-01
      • 2013-10-01
      • 2020-03-17
      • 2017-01-30
      • 2016-10-28
      相关资源
      最近更新 更多