2014-02-28 83 views
2

我需要幫助有關設置的組合框扣式。 我使用的組合框,顯示從包含一個表有兩列數據可觀察到的列表數據,「步驟」和「NextStep自動」(NextStep自動包含一個項目在列步驟插入);我需要做的是,以顯示與「步驟」,並與相對的「NextStep自動的」的扣式列表中的組合框listcell元素裏面。現在,我可以正確地看到listcell,但是我的buttoncell總是空的。JavaFX的組合框setButtonCell

代碼:

// 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"); 
      } 

     } 
    }); 

在此先感謝。

回答

0

我還看着你的用例下面的演示代碼SSCCE。
它工作正常,當從組合框的dropmenu所選擇的項目是扣式與相關「NEXTSTEP」更新,如:

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); 
    } 

} 

與你進行比較。