Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rendering a POJO with JavaFX 2's Combo Box without overriding the toString() method

Tags:

javafx

I have a list of employees whose names I need to render on a combo box for the user to select. The following code renders the names on the dropdown list, but when I select a name, the combo's displayed text contains the full POJO's identity, a string like "src.org.entities.Employee@449ac7ce"

cboEmployees.setCellFactory(new Callback<ListView<Employee>, ListCell<Employee>>()
    {
        @Override
        public ListCell<Employee> call(ListView<Employee> p)
        {
            return new ListCell<Employee>()
            {
                @Override
                protected void updateItem(Employee item, boolean empty) {
                    super.updateItem(item, empty);

                    if (item != null)
                    {
                        setText(item.getName());
                    }
                }
            };
        }
    });

Is there a way to make the displayed text renders the selected name as well without overriding the POJO's toString() method?

like image 897
etercap Avatar asked Nov 13 '12 20:11

etercap


1 Answers

Invoke setButtonCell on your ComboBox, providing an appropriate ListCell renderer implementation to display the employee name.

For example:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.util.Callback;

public class BasicComboBoxSample extends Application {
    public static void main(String[] args) { launch(args); }

    @Override public void start(Stage stage) {
        final Employee john = new Employee("John");
        final Employee jill = new Employee("Jill");
        final Employee jack = new Employee("Jack");

        final ComboBox<Employee> cboEmployees = new ComboBox();
        cboEmployees.setButtonCell(new EmployeeListCell());
        cboEmployees.setCellFactory(new Callback<ListView<Employee>, ListCell<Employee>>() {
            @Override public ListCell<Employee> call(ListView<Employee> p) {
                return new EmployeeListCell();
            }
        });
        cboEmployees.getItems().addAll(john, jill, jack);
        cboEmployees.setValue(jill);

        final StackPane layout = new StackPane();
        layout.getChildren().add(cboEmployees);
        layout.setStyle("-fx-background-color: cornsilk; -fx-padding: 15;");
        stage.setScene(new Scene(layout));
        stage.show();
    }    

    class Employee {
        public Employee(String name) { this.name = name; }
        private String name;
        public String getName() { return name; }
    }

    class EmployeeListCell extends ListCell<Employee> {
        @Override protected void updateItem(Employee item, boolean empty) {
            super.updateItem(item, empty);
            if (!empty && item != null) {
                setText(item.getName());
            } else {
                setText(null);
            }
        }
    }
}

Sample output:

combobox custom button sample

like image 196
jewelsea Avatar answered Nov 06 '22 03:11

jewelsea