0

I have a GridView displaying custom GridCell that I have created using FXML. I want this GridCells to be selectable and I need to listen to selection changes.

So far, I have tried the solutions provided here How to implement selection of Nodes in JavaFX without luck

  //Here is the code of My Custom GridCell
  public class IconGridCell extends GridCell<IconItem> implements SelectableGridCell{
     @FXML
     public AnchorPane gridCell;
     @FXML
     public FontIcon fontIcon;
     private FXMLLoader mLLoader;

    @Override
    protected void updateItem(IconItem iconItem, boolean empty) {
      super.updateItem(iconItem, empty);
         if (empty || iconItem == null) {
            setText(null);
            setGraphic(null);
         } else {
              if (mLLoader == null) {
                  mLLoader = new FXMLLoader(getClass().getResource("/cell.fxml"));
                  mLLoader.setController(this);
           try {
              mLLoader.load();
           } catch (IOException e) {
               e.printStackTrace();
           }
        }
        fontIcon.setIconLiteral(iconItem.getIcon().getIconLiteral());
        setText(null);
        setGraphic(gridCell);
    }
  }

I want to be able to listen the selection changes so that I can update the UI accordingly

c0der
  • 18,467
  • 6
  • 33
  • 65
  • selection "change" of what? Unrelated: don't load the fxml in updateItem (that's called often!), instead do so once in its constructor – kleopatra Jan 19 '19 at 10:51
  • @kleopatra I need a SelectionModel implementation like in ListView so that I can monitor which GridCell has been selected (clicked) – Pio Alfred Jan 19 '19 at 10:59
  • hmm .. doesn't the grid has a selectionModel? And a gridCell is just a cell which has a selectedProperty? – kleopatra Jan 19 '19 at 11:01
  • @kleopatra It does not support. You can read it here https://bitbucket.org/controlsfx/controlsfx/issues/4/add-a-multipleselectionmodel-to-gridview – Pio Alfred Jan 19 '19 at 11:08
  • okay, then you have to implement your own, nothing anybody here is likely to do for you ;) – kleopatra Jan 19 '19 at 11:11
  • To note, as ControlsFX is now hosted on GitHub, here's the GitHub link for the same issue: https://github.com/controlsfx/controlsfx/issues/4 – Slaw Jan 19 '19 at 12:24

0 Answers0