java 分组框,带有对象的Java FX可编辑组合框

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 combo1 = new ComboBox();

combo1.getItems().addAll(usr1, usr2, usr3);

combo1.setConverter(new StringConverter() {

@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

解决方案

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 combo1 = new ComboBox();

combo1.getItems().addAll(usr1, usr2, usr3);

combo1.setConverter(new StringConverter() {

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

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值