2016-11-22 1 views
0

J'ai Listview setted comme CheckBoxListCell.Now je dois ajouter un colorpicker dans chaque ligne pour changer la couleur de chaque ListCell. Je ne sais pas où je peux placer ceci dans le CheckBoxListCell.javafx: comment ajouter un colorpicker à un ListView?

ListView my_list = new ListView();  
my_list.setCellFactory(CheckBoxListCell.forListView(new Callback<Item, ObservableValue<Boolean>>() { 
       @Override 
       public ObservableValue<Boolean> call(Item item) { 
        return item.onProperty(); 
       } 
      })); 

public class Item { 
     private final StringProperty name = new SimpleStringProperty(); 
     private final BooleanProperty on = new SimpleBooleanProperty(); 

     public Item(String name, boolean on) { 
      setName(name); 
      setOn(on); 
     } 

     public final StringProperty nameProperty() { 
      return this.name; 
     } 

     public final String getName() { 
      return this.nameProperty().get(); 
     } 

     public final void setName(final String name) { 
      this.nameProperty().set(name); 
     } 

     public final BooleanProperty onProperty() { 
      return this.on; 
     } 

     public final boolean isOn() { 
      return this.onProperty().get(); 
     } 

     public final void setOn(final boolean on) { 
      this.onProperty().set(on); 
     } 

     @Override 
     public String toString() { 
      return getName(); 
     } 

    } 

Répondre

1

J'ai récemment créé une vue similaire. Voici le code:

//options is populated with some strings 
options = FXCollections.observableArrayList(); 
listView = new ListView<String>(); 
listView.setPrefHeight(390); 

listView.setItems(options); 
listView.getStyleClass().add("scs-listview-dialog"); 

setListViewFactory(); 

listView.getSelectionModel().select(0); 
... 
... 
    private void setListViewFactory() 
{ 
    listView.setCellFactory(
      (final ListView<String> p) -> new ListCell<String>() 
      { 
       @Override 
       protected void updateItem(final String item, final boolean empty) 
       { 
        super.updateItem(item, empty); 
        if (item != null) 
        { 
         Platform.runLater(() -> 
         { 
          this.setGraphic(makeGridPane(item)); 
         }); 
        } 
       } 
      }); 
} 

private GridPane makeGridPane(final String item) 
{ 
    final GridPane gridPane = new GridPane(); 
    final ColumnConstraints col1 = new ColumnConstraints(); 
    final ColumnConstraints col2 = new ColumnConstraints(); 
    col1.setHalignment(HPos.LEFT); 
    col1.setMinWidth(105); 
    col2.setHalignment(HPos.RIGHT); 
    col2.setMinWidth(105); 
    gridPane.getColumnConstraints().addAll(col1, col2); 

    final HBox spacer = new HBox(); 
    spacer.setMinWidth(10); 

    final Label optionLabel = new Label(item); 

    ColorPicker colorPicker = new ColorPicker(); 
    colorPicker.getStyleClass().add("colorPicker"); 

    colorPicker.setOnMouseClicked((e) -> 
    { 
     listView.getSelectionModel().select(item); 
    }); 

    colorPicker.setOnAction((ActionEvent t) -> 
    { 
     Color color = colorPicker.getValue(); 

     String selectedItem = listView.getSelectionModel().getSelectedItem(); 
     PyDevThemes pyDevThemeItem = PyDevThemes.valueByListItem(selectedItem); 

     getCurrentThemeValuesMap().put(pyDevThemeItem.getId(), colorToRGB(color)); 
     updateLabelColoring(selectedItem, color); 
    }); 

    GridPane.setConstraints(optionLabel, 0, 0); 
    GridPane.setConstraints(colorPicker, 1, 0); 
    gridPane.getChildren().addAll(optionLabel, colorPicker); 
    return gridPane; 
} 

Preview of code snippet