Java fx editable combobox with objects

寵の児 提交于 2021-02-07 20:50:31

问题


I am just starting to learn Java Fx. I have a combo box filled with objects. I dealt with toString() method, and I can see that name I wanted to display on the screen. But now I would like to make it editable, that user will enter its own text, and ComboBox will create a new object and put that text into the correct field. I know that problem is in converter FromString or ToString, but I cannot deal with it.

package mnet;

import javafx.application.Application;
import javafx.scene.control.ComboBox;
import javafx.scene.Scene;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import javafx.util.StringConverter;

public class sample extends Application {
    Stage window;

    public static void main(String[] args) {
        launch(args);
    }

    public void start(Stage primaryStage) {

        window = primaryStage;
        window.setTitle("Sample");
        GridPane grid = new GridPane();
        User usr1 = new User("Witold", "ciastko");
        User usr2 = new User("Michał", "styk");
        User usr3 = new User("Maciej", "masloo");
        ComboBox<User> combo1 = new ComboBox<User>();
        combo1.getItems().addAll(usr1, usr2, usr3);
        combo1.setConverter(new StringConverter<User>() {
            @Override
            public String toString(User usr) {
                return usr.getName();
            }

            @Override
            public User fromString(String s) {
                User usr = new User(s, "haslo");
                combo1.getItems().add(usr);
                return usr;
            }
        });
        combo1.setEditable(true);
        combo1.valueProperty().addListener((v, oldValue, newValue) -> {
            System.out.println(newValue);
        });
        GridPane.setConstraints(combo1, 2, 1);
        grid.getChildren().addAll(combo1);
        Scene scene = new Scene(grid, 400, 200);
        window.setScene(scene);
        window.show();

    }
}

package mnet;

public class User {
    String user;
    String password;

    public User() {
        this.user="";
        this.password="";
    }
    public  User(String user, String password){
    this.user=user;
    this.password=password;
    }

    public String getName(){
        return this.user;
    }
}

If I get rid of StringConverter it works correctly, but instead of name of user I only see address of Object like this "mnet.User@1f3b971"

EDIT: Added appropriately working code


回答1:


You have a null pointer exception in you stringconverter since you can get a null User.

Your string converter should only convert User to/from String without modifying items since you don't know how many time it will be called.

To add a user I add an on event handler (when you type enter) on the combo that add a new user.

Note that thanks to the string converter you can call getValue on the combobox and get a user with the entered name

You should add a plus button to commit the user instead of my on event handler

here my working example :

public class Main extends Application {
Stage window;

public static void main(String[] args) {
    launch(args);
}

@Override
public void start(Stage primaryStage) {

    window = primaryStage;
    window.setTitle("Sample");
    GridPane grid = new GridPane();
    User usr1 = new User("Witold", "ciastko");
    User usr2 = new User("Michał", "styk");
    User usr3 = new User("Maciej", "masloo");
    ComboBox<User> combo1 = new ComboBox<User>();
    combo1.getItems().addAll(usr1, usr2, usr3);
    combo1.setConverter(new StringConverter<User>() {
        @Override
        public String toString(User usr) {
            return usr == null ? "" : usr.getName();
        }

        @Override
        public User fromString(String s) {
            User usr = new User(s, "haslo");
            return usr;
        }
    });
    combo1.setEditable(true);
    combo1.addEventHandler(KeyEvent.KEY_RELEASED, e -> {
        if (e.getCode() == KeyCode.ENTER) {
            combo1.getItems().add(combo1.getValue());
        }

    });
    GridPane.setConstraints(combo1, 2, 1);
    grid.getChildren().addAll(combo1);
    Scene scene = new Scene(grid, 400, 200);
    window.setScene(scene);
    window.show();

}


来源:https://stackoverflow.com/questions/46988860/java-fx-editable-combobox-with-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!