我已尝试使用Id和Value Pair创建一个组合框代码。现在我想设置组合框的值与指定的Id传递。示例:我想设置组合框的值,其员工姓名的工资为1400.0
package demo;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
/**
*
* @author vikassingh
*/
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);
// testing
//combobox.getSelectionModel().selectFirst();
//combobox.setValue(1400.0); // How to set value with specific Id Passed
// End testing
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;
}
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);
}
}
答案 0 :(得分:1)
使用您用于任何其他overlay.setMap(map)
/ Employee
的相同技术,可以在data
列表中找到正确的Collection
:迭代完成收集并找到符合标准的第一个元素。 Streams API提供了一种简单的方法:
List