Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You have 2 approaches:<br> 1. Simply override the <code>toString()</code> method in your datamodel class. Example: </p> <pre><code>public class Demo extends Application { private final ObservableList&lt;Employee&gt; data = FXCollections.observableArrayList( new Employee("Azamat", 2200.15), new Employee("Veli", 1400.0), new Employee("Nurbek", 900.5)); @Override public void start(Stage primaryStage) { ComboBox&lt;Employee&gt; combobox = new ComboBox&lt;&gt;(data); combobox.getSelectionModel().selectFirst(); // Select first as default combobox.getSelectionModel().selectedItemProperty().addListener(new ChangeListener&lt;Employee&gt;() { @Override public void changed(ObservableValue&lt;? extends Employee&gt; arg0, Employee arg1, Employee arg2) { if (arg2 != null) { System.out.println("Selected employee: " + arg2.getName()); } } }); StackPane root = new StackPane(); root.getChildren().add(combobox); primaryStage.setScene(new Scene(root, 300, 250)); primaryStage.show(); } public static class Employee { private String name; private Double salary; @Override public String toString() { return name + " (sal:" + salary + ")"; } public Employee(String name, Double salary) { this.name = name; this.salary = salary; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Double getSalary() { return salary; } public void setSalary(Double salary) { this.salary = salary; } } public static void main(String[] args) { launch(args); } } </code></pre> <p>Note the overriden toString() of the Employee class. The combobox's key (actual value) will be the <code>Employee</code> instance and the display value is <code>employee.toString()</code> in this case.<br> 2. Second approach is to set cellFactory property of the combobox. </p> <pre><code>combobox.setCellFactory(new Callback&lt;ListView&lt;Employee&gt;, ListCell&lt;Employee&gt;&gt;() { @Override public ListCell&lt;Employee&gt; call(ListView&lt;Employee&gt; arg0) { return new ListCell&lt;Employee&gt;() { private final Button btn; { setContentDisplay(ContentDisplay.GRAPHIC_ONLY); btn = new Button(); } @Override protected void updateItem(Employee item, boolean empty) { super.updateItem(item, empty); if (item == null || empty) { setGraphic(null); } else { btn.setStyle(item.getSalary() &gt; 1000 ? "-fx-base:red" : "-fx-base: green"); btn.setText(item.getName() + "=" + item.getSalary()); setGraphic(btn); } } }; } }); </code></pre> <p>This approach gives more powerful control over cell rendering. You can not just format display value but also include any node (control) into cell (button in this case) and add some viewing logic (item.getSalary()?"":"") too. The actual value remains the same ie Employee instance.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload