从一个Combobox中选择的项目如何更改第二Combobox上的特定项目?

 2023-02-15    320  

问题描述

Combobox1有3个项目(西班牙,法国,英国)和Combobox2 6项目(巴塞罗那,马德里,巴黎,巴黎,马赛,伦敦,布里斯托尔).
当从Combobox1(西班牙)中选择一个项目时,应该可以将特定项目(巴塞罗那,马德里)组合起来.

有人可以帮我吗?

从一个Combobox中选择的项目如何更改第二Combobox上的特定项目?

预先感谢

推荐答案

三个combox侦听器示例代码.我认为此解决方案用于您的

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package threecombobox;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

/**
 *
 * @author reegan
 */
public class ThreeComboBox extends Application {

    @Override
    public void start(Stage primaryStage) {

        List combox1List = new ArrayList();
        for (int i = 1; i < 10; i++) {
            combox1List.add(i);
        }

        final Map combox2Map = new HashMap();

        for (int i = 0; i < combox1List.size(); i++) {
            List l = new ArrayList();
            for (int j = 1; j < 10; j++) {
                int k = (int) combox1List.get(i) * 10 + j;
                l.add(k);
            }
            combox2Map.put(combox1List.get(i), l);
        }
        final Map combox3Map = new HashMap();
        for (Object o : combox1List) {
            for (Object o1 : (List) combox2Map.get(o)) {
                List l = new ArrayList();
                for (int i = 1; i < 10; i++) {
                    int value = (int) o1 * 10 + i;
                    l.add(value);
                }
                combox3Map.put(o1, l);
            }
        }
        ObservableList combox1 = FXCollections.observableList(combox1List);
        HBox box = new HBox(20);
        box.setPadding(new Insets(20, 20, 20, 20));
        ComboBox cb1 = new ComboBox();
        final ComboBox cb2 = new ComboBox();
        final ComboBox cb3 = new ComboBox();
        cb1.setItems(combox1);
        cb1.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() {
            @Override
            public void changed(ObservableValue ov, Object t, Object t1) {
                ObservableList combox2 = FXCollections.observableArrayList((List) combox2Map.get(t1));
                cb2.setItems(combox2);
            }
        });

        cb2.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() {
            @Override
            public void changed(ObservableValue ov, Object t, Object t1) {
                if (t1 != null) {
                    ObservableList combox3 = FXCollections.observableArrayList((List) combox3Map.get(t1));
                    cb3.setItems(combox3);
                }
            }
        });
        box.getChildren().addAll(cb1, cb2, cb3);
        Scene scene = new Scene(box, 300, 250);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * The main() method is ignored in correctly deployed JavaFX application.
     * main() serves only as fallback in case the application can not be
     * launched through deployment artifacts, e.g., in IDEs with limited FX
     * support. NetBeans ignores main().
     *
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
}

其他推荐答案

好吧,我们不知道您的数据结构,但是如果一个城市附在一个国家/地区,请添加听众,将听众添加到您的国家组合,每次选择更改时,您都会在City Combobox上重新配置项目.

我会做这样的事情:

ComboBox<City> cityBox;
    ComboBox<Country> countryBox;

    countryBox.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() {
        @Override
        public void changed(ObservableValue observableValue, Country oldCountry, Country newCountry) {
            //Here configure your cityBox corresponding with newCountry
        }
    });

或结合

cityBox.itemsProperty().bind(new ListBinding<City>() {
        {
            bind(cityBox.getSelectionModel().selectedItemProperty());
        }
        @Override
        protected ObservableList<City> computeValue() {
            Country country = countryBox.getValue();
            // add the city corresponding to the country on a new list
            return //The list with the city filtered with the country
        }
    });

以上所述是小编给大家介绍的从一个Combobox中选择的项目如何更改第二Combobox上的特定项目?,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对77isp云服务器技术网的支持!

原文链接:https://77isp.com/post/33785.html

=========================================

https://77isp.com/ 为 “云服务器技术网” 唯一官方服务平台,请勿相信其他任何渠道。