我刚刚开始学习JavaFX 2.
现在我正在尝试构建一个示例应用程序.然后我被困在组合框中.
我没有在JavaFX中找到对combobox的键值对的任何引用.http://docs.oracle.com/javafx/2/api/index.html上
的组合框javadoc 没有说明关键值对.
如何创建具有不同显示值和实际值的项目的组合框?
Ulu*_*Biy 19
您有两种方法:
1.只需覆盖toString()datamodel类中的方法.例:
public class Demo extends Application {
private final ObservableList<Employee> data =
FXCollections.observableArrayList(
new Employee("Azamat", 2200.15),
new Employee("Veli", 1400.0),
new Employee("Nurbek", 900.5));
@Override
public void start(Stage primaryStage) {
ComboBox<Employee> combobox = new ComboBox<>(data);
combobox.getSelectionModel().selectFirst(); // Select first as default
combobox.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Employee>() {
@Override
public void changed(ObservableValue<? extends Employee> arg0, Employee arg1, Employee arg2) {
if (arg2 != null) {
System.out.println("Selected employee: " + arg2.getName());
}
}
});
StackPane root = new StackPane();
root.getChildren().add(combobox);
primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.show();
}
public static class Employee {
private String name;
private Double salary;
@Override
public String toString() {
return name + " (sal:" + salary + ")";
}
public Employee(String name, Double salary) {
this.name = name;
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getSalary() {
return salary;
}
public void setSalary(Double salary) {
this.salary = salary;
}
}
public static void main(String[] args) {
launch(args);
}
}
Run Code Online (Sandbox Code Playgroud)
请注意Employee类的重写toString().组合框的键(实际值)将是Employee实例,employee.toString()在这种情况下显示值.
2.第二种方法是设置组合框的cellFactory属性.
combobox.setCellFactory(new Callback<ListView<Employee>, ListCell<Employee>>() {
@Override
public ListCell<Employee> call(ListView<Employee> arg0) {
return new ListCell<Employee>() {
private final Button btn;
{
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
btn = new Button();
}
@Override
protected void updateItem(Employee item, boolean empty) {
super.updateItem(item, empty);
if (item == null || empty) {
setGraphic(null);
} else {
btn.setStyle(item.getSalary() > 1000 ? "-fx-base:red" : "-fx-base: green");
btn.setText(item.getName() + "=" + item.getSalary());
setGraphic(btn);
}
}
};
}
});
Run Code Online (Sandbox Code Playgroud)
这种方法可以更有效地控制细胞渲染.你不能只是格式显示值,但还包括(在这种情况下按钮)的任一节点(控制)单元格,并添加一些观看逻辑(item.getSalary()"?":"")了.实际值保持不变,即Employee实例.
aja*_*sti 13
还有另一个解决方案,实现StringConverter.它对于对象非常有用:
public class Product {
private String code;
private String name;
public Product(String code, String name) {
this.code = code;
this.name = name;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Run Code Online (Sandbox Code Playgroud)
转换器实现:
public class ProductConverter extends StringConverter<Product> {
/** Cache of Products */
private Map<String, Product> productMap = new HashMap<String, Product>();
@Override
public String toString(Product product) {
productMap.put(product.getName(), product);
return product.getName();
}
@Override
public Product fromString(String name) {
return productMap.get(name);
}
}
Run Code Online (Sandbox Code Playgroud)
代码在视图中:
ComboBox<Product> cboProducts = new ComboBox<Product>;
cboProducts.setConverter(new ProductConverter());
cboProducts.getItems().addAll(serviceManager.getProductList());
Run Code Online (Sandbox Code Playgroud)
要获取产品的价值,您可以调用getValue()方法:
cboProducts.getValue()
Run Code Online (Sandbox Code Playgroud)