简体   繁体   中英

how to use a list/map with a combobox

I'm trying to make a form for a java application. It contains a Combobox with a list of users. Let's say I got a class Professor, with 3 attributes. Id being the 'unique', which is required for the database.

public class Professor
  private String id;
  private String lastName;
  private String firstName;

I figured the most user friendly option would be to fill the combobox with the last name and first name of the 'Professor' class, which is what I did. But now I can't figure out how to get the Professor object back.

Like I said, I filled the combobox with the lastName, a space and the firstName, but once the user has choosen a professor, I can't figure out how to select the professor that the user choose.

Is there some way to put an Index and a value in a Combobox? as in first option would be John Smith, second one would be Jane Adams... I've tried the use a Map collection, a List of 'id' attributes, but I can't seem to figure out how to link the 2.

Any help would be much appreciated.

edit: found the answer. The easiest solution is to fill the Combobox with custom classes, here 'Professor' objects. I thought that wasn't possible, but apparently it is. Add a toString() method to your class, with what you want it to be. In my case lastName + firstName. Add a @Override annotation to it and it'll show it in the Combobox, while still giving you easy access to any attributes.

Two different problems in your question:

  1. Have user-friendly representation of your Professor in the ComboBox ( John Doe instead of Professor@71c3cfdd )
  2. Get the selected object back from the ComboBox.

For the first problem, I would create a StringConverter which will transform your Professor object in a human-readable string.

Example of implementation:

public class ProfessorConverter extends StringConverter<Professor> {

     @Override
     public Professor fromString(String professorAsString) {
         // Create a professor from a string, e.g. (assuming that
         // the professorAsString as the form "1 John Doe"
         String[] pieces = professorAsString.split(" ");
         return new Professor(pieces[0], pieces[1], pieces[2]);
     }

     @Override
     public String toString(Professor professor) {
         // Converts your professor to a String of type "John Doe"
         return professor.getFirstName() + " " + professor.getLastName();
     }

}

Then assign an instance of your ProfessorConverter to the ComboBox:

box.setConverter(new ProfessorConverter());

For your second problem, you can use the selectionModel of the ComboBox object to get the selected object (the type of object that you will get back is specified by the generic you use in the declaration of the ComboBox, ie ComboBox<Professor> in our case).

Professor selectedProfessor = box.getSelectionModel().getSelectedItem();

will return the selected professor or null if none is selected.

To populate the ComboBox, I use the utility methods of FXCollections to create a new ObservableList from a 'normal' java list.

List<Professor> myListOfProfessors = ... ;
box.setItems(FXCollections.observableList(myListOfProfessors));

Hope it helps :)

Which kinds of Combobox are you using? I guess it is JCombobox. Anyway I would suggest you read the API. For the JCombobox it is http://docs.oracle.com/javase/8/docs/api/javax/swing/JComboBox.html . There you will find method to get selected Item. http://docs.oracle.com/javase/8/docs/api/javax/swing/JComboBox.html#getSelectedIndex-- Get selected objects http://docs.oracle.com/javase/8/docs/api/javax/swing/JComboBox.html#getSelectedObjects-- etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM