java 实现省市区三级联动时jcombobox的监听问题

如题,想要通过读取Excel文件(一列id,一列地区名称)来实现,但是目前可以实现省市两级,
在区上遇到了combobox框的监听问题。希望通过选中下拉框的选项,调取该选项对应的id,来传给下一级来筛序,在调试的过程中发现程序会自动执行市级组合框的监听,结果是找不到匹配的选项。
贴上代码:

package area;


import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.FileInputStream;
import java.io.InputStream;
/**
 * Created by zhigeek on 2016/11/8.
 */
public class SecondComboboxDemo {
    Frame frame = new JFrame("JComboBox Demo");
    JComboBox jcb1, jcb2, jcb3;
    JTextArea ta = new JTextArea(0, 30); // 用于显示结果的文本区
    String[] No1 = null, No2 = null;
    String[] itemList;
    String[] itemList2;
    String[] itemList3;
    JPanel p1 = new JPanel();
    JPanel p2 = new JPanel();
    JPanel p3 = new JPanel();
    JPanel p4 = new JPanel();
    JPanel p5 = new JPanel();
    Border etched = BorderFactory.createEtchedBorder(); // 创建边框
    Border border = BorderFactory.createTitledBorder(etched, "Uneditable JComboBox"); // 表框加上标题


    public static void main(String args[]) {
        SecondComboboxDemo cbd = new SecondComboboxDemo();
        //String[] No1 = getNo1Lever();
        //String[] No2 = getNo2Lever(1);
        cbd.go();
    }

    public static String[] getNo1Lever() {
        InputStream is;
        String[] no1List = null;
        int j = 0;
        try {
            is = new FileInputStream("中国各地区代码表.xls");

            jxl.Workbook workbook = Workbook.getWorkbook(is);

            Sheet rs = workbook.getSheet(0);

            int row = rs.getRows();
            no1List = new String[35];

            for (int i = 0; i < row; i++) {
                Cell cell = rs.getCell(0, i);
                int cellsize = Integer.parseInt(cell.getContents());
                if (cellsize / 10 <= 9) {
                    Cell cell2 = rs.getCell(1, i);
                    //System.out.println(cell2.getContents());
                    no1List[j] = cell2.getContents();
                    j++;
                }


            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return no1List;
    }

    public static String[] getNo2Lever(int no1) {
        InputStream is;
        String[] no2List = null;

        try {
            is = new FileInputStream("中国各地区代码表.xls");

            jxl.Workbook workbook = Workbook.getWorkbook(is);

            Sheet rs = workbook.getSheet(0);
            int row = rs.getRows();
            int j = 0;
            no2List = new String[50];
            for (int i = 0; i < row; i++) {
                Cell cell = rs.getCell(0, i);
                int Cell2 = Integer.parseInt(cell.getContents());


                if (Cell2 / 100 == no1 && Cell2 / 100 < 99) {
                    Cell cell2 = rs.getCell(1, i);
                    //System.out.println(cell2.getContents());
                    no2List[j] = cell2.getContents();
                    j++;
                }
            }


        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return no2List;
    }

    public static String[] getNo3Lever(int no2) {
        InputStream is;
        String[] no3List = null;

        try {
            is = new FileInputStream("中国各地区代码表.xls");

            jxl.Workbook workbook = Workbook.getWorkbook(is);

            Sheet rs = workbook.getSheet(0);
            int row = rs.getRows();
            int cell3 = 0;
            int j = 0;
            no3List = new String[50];
            for (int i = 0; i < row; i++) {
                Cell cell = rs.getCell(0, i);
                //int cellsize=cell.toString().length();
                int Cell2 = Integer.parseInt(cell.getContents());
                if (Cell2 / 100 == no2) {
                    Cell cell2 = rs.getCell(1, i);
                    //System.out.println(cell2.getContents());
                    no3List[j] = cell2.getContents();
                    j++;
                }
            }


        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return no3List;
    }

    public void go() {
        // 创建内部JPanel容器
        itemList = getNo1Lever();
        jcb1 = new JComboBox(itemList);
        jcb1.setSelectedIndex(0); // 设置第4个可选项为当前的显示项
//
//        p1.add(jcb1);
//        p1.setBorder(border);


        // 添加4个可选项
        itemList2 = getNo2Lever(11);
        jcb2 = new JComboBox(itemList2);

        // 将jcb2设置为可编辑的
        jcb2.setEditable(true);

        //itemList3 = getNo3Lever(0);
        jcb3 = new JComboBox();

        // 将jcb2设置为可编辑的
        jcb3.setEditable(true);

        p1.add(jcb1);
        border = BorderFactory.createTitledBorder(etched, " 省直辖市");
        p1.setBorder(border);

        p2.add(jcb2);
        border = BorderFactory.createTitledBorder(etched, " 区市");
        p2.setBorder(border);

        p5.add(jcb3);
        border = BorderFactory.createTitledBorder(etched, "县");
        p5.setBorder(border);

        JScrollPane jp = new JScrollPane(ta);
        p3.setLayout(new BorderLayout());
        p3.add(jp);
        border = BorderFactory.createTitledBorder(etched, "Results");
        p3.setBorder(border);
        ActionListener a1 = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JComboBox jcb = (JComboBox) e.getSource();
                if (jcb == jcb1) {
                    int no1 = 0;
                    InputStream is;
                    try {
                        is = new FileInputStream("中国各地区代码表.xls");
                        jxl.Workbook workbook = Workbook.getWorkbook(is);
                        Sheet rs = workbook.getSheet(0);
                        int row = rs.getRows();
                        for (int i = 0; i < row; i++) {
                            Cell cell = rs.getCell(0, i);
                            Cell cell2 = rs.getCell(1, i);
                            if (cell2.getContents().equals(jcb1.getSelectedItem())) {
                                int cell3 = Integer.parseInt(cell.getContents());
                                no1 = cell3;
                                break;
                            }
                            else{

                            }


                        }
                    } catch (Exception e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }

                    jcb2.removeAllItems(); // 去除所有选项
                    p2.remove(jcb2);
                    itemList2 = getNo2Lever(no1);
                    jcb2 = new JComboBox(itemList2);
                    jcb2.setEditable(true);
                    p2.add(jcb2);
                    border = BorderFactory.createTitledBorder(etched, "Editable JComboBox");

                }
            }
        };
       jcb2.addItemListener(new ItemListener() {
           @Override
           public void itemStateChanged(ItemEvent e) {
               if (e.getStateChange() == ItemEvent.SELECTED) {
                   int no2 = 0;
                   InputStream is2;
                   //String[] no1List = null;
                   try {
                       is2 = new FileInputStream("中国各地区代码表.xls");
                       jxl.Workbook workbook = Workbook.getWorkbook(is2);
                       Sheet rs = workbook.getSheet(0);
                       int row = rs.getRows();
                       for (int i = 0; i < row; i++) {
                           Cell cell = rs.getCell(0, i);
                           Cell cell2 = rs.getCell(1, i);
                           //int cellsize=Integer.parseInt(cell.getContents());
                           //String a=jcb.getSelectedItem();
                           if (cell2.getContents().equals(jcb2.getSelectedItem())) {
                               int cell3 = Integer.parseInt(cell.getContents());
                               no2 = cell3;
                           }


                       }
                   } catch (Exception e1) {
                       // TODO Auto-generated catch block
                       e1.printStackTrace();

                   }

                   jcb3.removeAllItems(); // 去除所有选项
                   p5.remove(jcb3);
                   itemList3 = getNo3Lever(no2);
                   jcb3 = new JComboBox(itemList3);
                   jcb3.setEditable(true);
                   p5.add(jcb3);
                   border = BorderFactory.createTitledBorder(etched, "Editable JComboBox");
               }


           }
       });

        jcb1.addActionListener(a1);
        //jcb2.addActionListener(a2);

        p4.setLayout(new GridLayout(0, 1));
        p4.add(p1);
        p4.add(p2);
        p4.add(p5);

        Container cp = ((JFrame) frame).getContentPane();
        cp.setLayout(new GridLayout(0, 1));
        cp.add(p4);
        cp.add(p3);

        ((JFrame) frame).setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}



这是excel文件的存储格式
图片说明
求好心人解答

1个回答

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问