2

I am using JavaFX, Hibernate, Spring in my project.

And i need to fill the combobox with my object values. In my combobox i need to show only title value from my model.

My model class:

public class Sector extends Identifier {
    private String title;

    private List<Stage> stageList;

    public List<Stage> getStageList() {
        return stageList;
    }

    public void setStageList(List<Stage> stageList) {
        this.stageList = stageList;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    @Override
    public String toString() {
        return "Sector{" +
                "id='" + getId() + '\'' +
                "title='" + title + '\'' +
                ", stageList=" + stageList +
                '}';
    }
}

and

public class Stage extends Identifier {
    private String name;

    private Station firstStation;

    private Station secondStation;

    private List<CommunicationDistance> communicationDistanceList;

    public Stage() {
    }

    public Stage(String name, Station firstStation, Station secondStation) {
        this.name = name;
        this.firstStation = firstStation;
        this.secondStation = secondStation;
    }

    public List<CommunicationDistance> getCommunicationDistanceList() {
        return communicationDistanceList;
    }

    public void setCommunicationDistanceList(List<CommunicationDistance> communicationDistanceList) {
        this.communicationDistanceList = communicationDistanceList;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Station getFirstStation() {
        return firstStation;
    }

    public void setFirstStation(Station firstStation) {
        this.firstStation = firstStation;
    }

    public Station getSecondStation() {
        return secondStation;
    }

    public void setSecondStation(Station secondStation) {
        this.secondStation = secondStation;
    }

    @Override
    public String toString() {
        return "Stage{" +
                "id='" + getId() + '\'' +
                "name='" + name + '\'' +
                ", firstStation=" + firstStation +
                ", secondStation=" + secondStation +
                ", communicationDistanceList=" + communicationDistanceList +
                '}';
    }

In my controller there is some method-listener for combobox to make some other manipulations with this data: (about cell factory i read from this question and also from here)

   @FXML
        public void currentSectorSelected(ActionEvent actionEvent) {
            ObservableList<Stage> observableList = FXCollections.observableArrayList(((Sector) sector.getSelectionModel().getSelectedItem()).getStageList());
            stage.setItems(observableList);
            stage.getSelectionModel().selectFirst();

            stage.setCellFactory(new Callback<ListView<Stage>, ListCell<Stage>>() {
                @Override
                public ListCell<Stage> call(ListView<Stage> param) {

                    return new ListCell<Stage>(){
                        @Override
                        public void updateItem(Stage item, boolean empty){
                            super.updateItem(item, empty);
                            if(!empty) {
                                setText(item.getName());
                                setGraphic(null);
                            } else {
                                setText(null);
                            }
                        }
                    };
                }
            });
        }

All is working, but in my comboboxes values shown like these: enter image description here

This are my correct objects but, still i can't understand how to format my combobox to show only title field from my Sector and other objects? Can you show some valid/correct example to format my combobox output?

Edit 1: In my init method i am simply added the list of my objects to combobox. I am not sure that this is correct way, but if i want to validate this data after combobox value is chosen - i must to set a full object in combobox:

  @FXML
        public ComboBox sectorComboBox;

    @Override
        public void initialize(URL location, ResourceBundle resources) {
            sectorComboBox.setItems(FXCollections.observableArrayList(sectorService.listAll()));
        }
Community
  • 1
  • 1
Alexey Shabramov
  • 730
  • 1
  • 16
  • 37
  • 1
    Have you debugged it? setText(item.getName()); Here a String is returned and the combobox simply displays that String, that means that the problem is not in your combobox but "getName()" returns the wrong value -> Model is maybe not set properly. – DVarga Apr 08 '16 at 07:42
  • Hi DVarga. Please, look at my new edit. – Alexey Shabramov Apr 08 '16 at 07:53
  • 1
    You were right. There was a problem in the saved data, not in combobox. With ItachiUchiha variant and your advice - i have solved this issue. Thank's! – Alexey Shabramov Apr 08 '16 at 08:20

1 Answers1

7

You should use a StringConverter instead of overriding the ListCell. This is a more elegant way of achieving the same result.

StringConverter<Sector> converter = new StringConverter<Sector>() {
    @Override
    public String toString(Sector object) {
        return object.getTitle();
    }

    @Override
    public Sector fromString(String string) {
        return null;
    }
};

MCVE

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.StringConverter;

public class ComboBoxConverter extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {

        ComboBox<Sector> comboBox = new ComboBox<>();
        StringConverter<Sector> converter = new StringConverter<Sector>() {
            @Override
            public String toString(Sector object) {
                return object.getTitle();
            }

            @Override
            public Sector fromString(String string) {
                return null;
            }
        };
        comboBox.setConverter(converter);

        comboBox.setItems(FXCollections.observableArrayList(new Sector("Title1", 24), new Sector("Title2", 50)));
        comboBox.getSelectionModel().selectFirst();
        VBox box = new VBox(comboBox);
        box.setAlignment(Pos.CENTER);
        Scene scene = new Scene(box, 200, 200);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

The sector class is a simple POJO with two fields.

public class Sector {
    private String title;
    private double area;

    public Sector(String title, double area) {
        this.title = title;
        this.area = area;
    }

    public double getArea() {
        return area;
    }

    public void setArea(double area) {
        this.area = area;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}
ItachiUchiha
  • 36,135
  • 10
  • 122
  • 176