hai-geek 2016-11-10 14:02 采纳率: 60%
浏览 1597

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条回答 默认 最新

  • threenewbee 2016-11-10 15:49
    关注
    评论

报告相同问题?

悬赏问题

  • ¥30 STM32 INMP441无法读取数据
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境