Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

ComboBox selected item display

User_CDX3FAug 28 2013 — edited Sep 20 2013

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());

            }

        }

    }

}

This post has been answered by James_D on Aug 28 2013
Jump to Answer

Comments

Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Oct 18 2013
Added on Aug 28 2013
2 comments
856 views