带有对象的Java FX可编辑组合框 [英] Java fx editable combobox with objects
问题描述
我才刚刚开始学习Java Fx. 我有一个装满对象的组合框.我处理了toString()方法,可以看到要在屏幕上显示的名称.但是现在我想使其可编辑,该用户将输入自己的文本,而ComboBox将创建一个新对象并将该文本放入正确的字段中.我知道问题出在转换器FromString或ToString中,但是我无法处理它.
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;
}
}
如果我摆脱了StringConverter,它可以正常工作,但是除了用户名,我只能看到对象的地址,例如"mnet.User@1f3b971"
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"
添加了适当的工作代码
推荐答案
stringconverter中有一个空指针异常,因为您可以获得一个空用户.
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.
要添加用户,我在添加新用户的组合上添加了on事件处理程序(当您键入enter时).
To add a user I add an on event handler (when you type enter) on the combo that add a new user.
请注意,由于使用了字符串转换器,您可以在组合框上调用getValue并获得具有输入名称的用户
Note that thanks to the string converter you can call getValue on the combobox and get a user with the entered name
您应该添加一个加号按钮来提交用户,而不是我的on事件处理程序
这是我的工作示例:
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();
}
这篇关于带有对象的Java FX可编辑组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!