javafx Combox完成页面选择名称,后台得到名称对应id的操作。

ChoiceBox 可实现列表选择的事件, 如有一个动物列表
在这里插入图片描述

在这里插入代码片

choiceBox可满足要求。可如果需要点击dog后台获取到dog的id值,choiceBox就有些力不从小。combox可以通过重写方法,来完成这个操作。

	@FXML
	private ComboBox<Map<String, String>> queryClass;
	// 存放queryClass列表的值
	private ObservableList<Map<String, String>> ComboBoxItems = FXCollections.observableArrayList();
/**
	 * 初始化queryClass
	 */
	public void initQueryClass() {
		if (ComboBoxItems != null)
			ComboBoxItems.clear();
		List<ClassInfo> classInfo = query.queryClassInfo();

		Map<String, String> map = new HashMap<>();
		map.put("ID", "-1");
		map.put("Name", "请选择");
		ComboBoxItems.add(map);
		if (classInfo != null && classInfo.size() > 0) {
		Map<String, String> maps 
			for (ClassInfo info : classInfo) {
				maps  = new HashMap<>();
				maps.put("ID", info.getClassId());
				maps.put("Name", info.getClassName());
				ComboBoxItems.add(maps);
			}
		}
		queryClass.setItems(ComboBoxItems);
		queryClass.converterProperty().set(new StringConverter<Map<String, String>>() {

			@Override
			public String toString(Map<String, String> object) {
				return object.get("Name");
			}

			@Override
			public Map<String, String> fromString(String string) {
				return null;
			}
		});
		queryClass.getSelectionModel().select(0);

	}

取值

String classId = queryClass.getValue().get("ID");

这样就可以获得一个前端展示班级姓名,后台取Id的操作了。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值