简体   繁体   English

级联组合框java

[英]Cascaded combo boxes java

In a java application I want a dialog with several combo boxes, as A, B, and C. A is populated in init components, B is populated when an item in A is selected, and C is populated when B is populated. 在Java应用程序中,我需要一个带有多个组合框的对话框,例如A,B和C。在初始组件中填充A,在选择A中的一个项目时填充B,在填充B时填充C。 I know the basic approach, ie, set up a listeners for A & B that populate the next box on Item.SELECTED. 我知道基本的方法,即为A和B设置一个侦听器,该侦听器填充Item.SELECTED上的下一个框。

What stumps me is what to do when the B item list is of length 1. With a single item no selection change is possible, so C doesn't get repopulated. 让我感到困扰的是,当B项列表的长度为1时,该怎么办。对于单个项,不可能进行选择更改,因此C不会重新填充。 Can someone give me a hint? 有人可以给我提示吗?

Thanks? 谢谢?

Ed S 埃德·S

At each level of the tree, check if the current node has no siblings (children of the same parent). 在树的每个级别上,检查当前节点是否没有兄弟姐妹(同一父级的子级)。 If that's the case, fire a selection event programmatically and load the children of the current node. 如果是这种情况,请以编程方式触发选择事件并加载当前节点的子级。

How about add empty item to B? 如何将空项目添加到B? And C ignore the selection change event when B select empty item. 当B选择空项目时,C忽略选择更改事件。

Problems solved. 问题解决了。 My problem was assuming that initializing using DefaultComboBoxModel(rootboxitems) would fire the item state change when the box was initialized. 我的问题是假设初始化Default时,使用DefaultComboBoxModel(rootboxitems)进行初始化会触发项目状态更改。 It does not; 它不是; all succeeding boxes must be updated after the initial population and after each selection change. 在初始填充之后和每次选择更改之后,必须更新所有后续的框。 This is easily done if the updating code for each box is put in a separate function so it can be reused. 如果将每个框的更新代码放在单独的函数中以便可以重复使用,则很容易做到这一点。

The example I devised is selection of a car based on the three attributes Make, Model, and Color, eg, "Ford", "Mustang", "Blue". 我设计的示例是根据“制造”,“模型”和“颜色”这三个属性(例如“福特”,“野马”,“蓝色”)选择汽车。 Each Make can have several Models, each with it's own list of Colors. 每个品牌可以有多个型号,每个型号都有自己的颜色列表。

The dialog & initialization is: 对话框和初始化为:

    public CascadeGui(java.awt.Frame parent, boolean modal, Makes aMakes) {
    super(parent, modal);
    ArrayList<Make> makes;
    Make selectedMake;
    Model slectedModel;
    initComponents();
    makes = aMakes.GetMakesArrayList();
    cboMakes.setModel(new javax.swing.DefaultComboBoxModel(makes.toArray(new Make[makes.size()])));
    selectedMake = (Make) cboMakes.getSelectedItem();
    cboModels.setModel(new javax.swing.DefaultComboBoxModel(selectedMake.GetModelsArray()));
    slectedModel = (Model) cboModels.getSelectedItem();
    cboColors.setModel(new javax.swing.DefaultComboBoxModel(slectedModel.GetColorsArray()));

}

This establishes an initial selection of all attributes. 这将建立所有属性的初始选择。 The Makes Item State Change event handler is: Makes Item State Change事件处理程序为:

    private void cboMakesItemStateChanged(java.awt.event.ItemEvent evt) {                                          
    if (evt.getStateChange() == SELECTED) {
        UpdateModelsCbo();
        UpdateColorsCbo();
    }
}                                         

As you can see, a new section of Make will result in updating of the Models box and then the Colors box. 如您所见,“制作”的新部分将导致“模型”框和“颜色”框的更新。 The corresponding code for a Model selection event is: 模型选择事件的相应代码为:

    private void cboModelsItemStateChanged(java.awt.event.ItemEvent evt) {                                           
    if (evt.getStateChange() == SELECTED) {
        UpdateColorsCbo();
    }
}                                          

I will be glad to share the Netbeans zip file for the example and/or answer questions. 我将很高兴分享该示例的Netbeans zip文件和/或回答问题。 At the same time, being somewhat of an novice Java programmer, I would appreciate comments and suggestions for improvement. 同时,作为Java的新手,我希望提出一些改进的意见和建议。

Ed 埃德

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM