Forum Stats

  • 3,837,511 Users
  • 2,262,265 Discussions
  • 7,900,308 Comments

Discussions

ComboBox selected item display

User_CDX3F
User_CDX3F Member Posts: 70 Blue Ribbon
edited Sep 20, 2013 5:35PM in JavaFX 2.0 and Later

I am showing a combobox and the value comes from a list of user defined value objects. I overrode the cellfactory method to show a particular member's value as the text in the combobox say for example (first name field of the employee object). Now when the user selects a value from the combobox,  the combobox text shows the toString representation's value of the objects, instead of the field (ristName) which I set in cellfactory method. Any help to show the first name will be of great help or is it desined that way. The reason why I am asking is when the same combo box is used else where to show the first name, last name, then we have to override the to toString of the employee object to return desired output. however, the it may break the code.

Sample code below:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package comboboxsample;


import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import javafx.util.Callback;


/**
 *
 *
 */
public class Combobox2 extends Application {


    public static void main(String[] args) {
        launch(args);
    }
    final ComboBox<SampleObject> sampleComboBox = new ComboBox<>();


    @Override
    public void start(Stage stage) throws Exception {
        initActionCodeComboBox();
        GridPane grid = new GridPane();
        grid.setVgap(4);
        grid.setHgap(10);
        grid.setPadding(new Insets(5, 5, 5, 5));


        grid.add(sampleComboBox, 1, 0);


        Scene scene = new Scene(new Group(), 450, 250);


        Group root = (Group) scene.getRoot();
        root.getChildren().add(grid);
        stage.setScene(scene);
        stage.show();
    }


    class SampleObject {


        String firstName;
        String lastName;


        public SampleObject(String firstName, String lastName) {
            this.firstName = firstName;
            this.lastName = lastName;
        }


        public String getFirstName() {
            return firstName;
        }


        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }


        public String getLastName() {
            return lastName;
        }


        public void setLastName(String lastName) {
            this.lastName = lastName;
        }
//        public String toString() {
//            return this.lastName;
//        }
    }


    private void initActionCodeComboBox() {
        sampleComboBox.getItems().addAll(
                new SampleObject("firstName1", "lastname1"),
                new SampleObject("firstName2", "lastname2"),
                new SampleObject("firstName3", "lastname3"),
                new SampleObject("firstName4", "lastname4"),
                new SampleObject("firstName5", "lastname5"));
        sampleComboBox.setPromptText("Select name");






        sampleComboBox.setCellFactory(new Callback<ListView<SampleObject>, ListCell<SampleObject>>() {
            @Override
            public ListCell<SampleObject> call(ListView<SampleObject> arg0) {
                return new comboboxsample.Combobox2.ComboBoxCell();
            }
        });
        sampleComboBox.valueProperty().addListener(new ChangeListener<SampleObject>() {
            @Override
            public void changed(ObservableValue<? extends SampleObject> ov, SampleObject t, SampleObject t1) {
                System.out.println("FirstName =" + ((SampleObject) sampleComboBox.getValue()).getFirstName());
                sampleComboBox.setValue(t1);
            }
        });
        sampleComboBox.setPrefWidth(300.0);
    }


    class ComboBoxCell extends ListCell<SampleObject> {


        @Override
        public void updateItem(SampleObject item, boolean empty) {
            super.updateItem(item, empty);
            if (item != null) {
                setContentDisplay(ContentDisplay.TEXT_ONLY);
                setText(item.getFirstName());
            }
        }
    }
}
Tagged:
lngrvr

Best Answer

Answers

This discussion has been closed.