palexdev / MaterialFX

A library of material components for JavaFX
GNU Lesser General Public License v3.0
1.21k stars 122 forks source link

How to add an image in MFXComboBox? #288

Closed dictory99 closed 1 year ago

dictory99 commented 1 year ago

Hello!

Is it possible to add an image in MFXComboBox?

I mean like this:

combobox_with_icon

Many thanks!

mahfoud09 commented 1 year ago

public class MFXComboBoxCellWithImage extends MFXComboBoxCell {

public MFXComboBoxCellWithImage<T> combo, T data) {
    super(combo, data);
    render(getData());
}

@Override
protected void render(T data) {
    super.render(data);
    ImageView image = new ........
    super.getChildren().add(0, image);
}

}

yourCombo.setCellFactory(data -> new MFXComboBoxCellForm<>(yourCombo, data));

stale[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] commented 1 year ago

Closed for inactivity.

lsequeiros commented 3 months ago

I had the same question. I reviewed component code and I noted that data attribute is not setted, so getData() is going to return null always. I setted the data attributte in a inhereted class and then invoke render method overritten.

private static class FieldsCellList(MFXComboBox<Fields> box, Fields field) {
      super(box, field);
      setData(field);
      this.render(getData());
    }

    @Override
    protected void render(Fields field) {
      super.render(field);
      // This condition is for the call in the `initialize` method invoked by the constructor.
      if(getData() == null) return;
      Label text = new Label(getData().getDisplayName());
      super.getChildren().setAll(field);
    }
}

I think that data attributte should be setted in the original constructor.