java的JCombobox实现中国省市区三级联动

源代码下载:点击下载源代码

用xml存储中国各大城市的数据。


xml数据太多了就不贴上了,贴个图片:


java的JCombobox实现中国省市区三级联动_第1张图片

要解释xml,添加了一个jdom.jar,上面的源代码下载里面有。

解释xml的类:

package com.qiantu.component;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class XMLDao {
	/**
	 * 根据某个城市获取此省市的所有地区
	 * @param districts
	 * @return
	 */
	public static List<String> getDistricts(String districts) {
		List<String> list = new ArrayList<String>();
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		try {
			factory.setIgnoringElementContentWhitespace(true);
			
			DocumentBuilder db = factory.newDocumentBuilder();
			Document xmldoc = db.parse(new File("xml/Districts.xml"));
			Element root = xmldoc.getDocumentElement();
			
			NodeList nodes = selectNodes("//District[@city='"+districts+"']", root);
			for(int i=0; i<nodes.getLength(); i++) {
				Node node = nodes.item(i);
				String name = node.getTextContent();
				list.add(name);
			}
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return list;
	}
	
	/**
	 * 根据某个省份的名字获取此省份的所有城市
	 * @param provinces
	 * @return
	 */
	public static List<String> getCities(String provinces) {
		List<String> list = new ArrayList<String>();
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		try {
			factory.setIgnoringElementContentWhitespace(true);
			
			DocumentBuilder db = factory.newDocumentBuilder();
			Document xmldoc = db.parse(new File("xml/Cities.xml"));
			Element root = xmldoc.getDocumentElement();
			
			NodeList nodes = selectNodes("//City[@Province='"+provinces+"']", root);
			for(int i=0; i<nodes.getLength(); i++) {
				Node node = nodes.item(i);
				String name = node.getTextContent();
				list.add(name);
			}
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return list;
	}
	
	/**
	 * 获取所有省份
	 * @return 
	 */
	public static List<String> getProvinces() {
		List<String> list = new ArrayList<String>();
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		try {
			factory.setIgnoringElementContentWhitespace(true);
			
			DocumentBuilder db = factory.newDocumentBuilder();
			Document xmldoc = db.parse(new File("xml/Provinces.xml"));
			Element root = xmldoc.getDocumentElement();
			
			NodeList nodes = selectNodes("/Provinces/Province", root);
			for(int i=0; i<nodes.getLength(); i++) {
				Node node = nodes.item(i);
				String name = node.getTextContent();
				list.add(name);
			}
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return list;
	}

	/**
	 * 根据xpath获取某一个节点
	 * @param express
	 * @param source
	 * @return
	 */
	public static Node selectSingleNode(String express, Object source) {// 查找节点,并返回第一个符合条件节点
		Node result = null;
		XPathFactory xpathFactory = XPathFactory.newInstance();
		XPath xpath = xpathFactory.newXPath();
		try {
			result = (Node) xpath
					.evaluate(express, source, XPathConstants.NODE);
		} catch (XPathExpressionException e) {
			e.printStackTrace();
		}

		return result;
	}

	/**
	 * 根据xpath获取符合条件的所有节点
	 * @param express
	 * @param source
	 * @return
	 */
	public static NodeList selectNodes(String express, Object source) {// 查找节点,返回符合条件的节点集。
		NodeList result = null;
		XPathFactory xpathFactory = XPathFactory.newInstance();
		XPath xpath = xpathFactory.newXPath();
		try {
			result = (NodeList) xpath.evaluate(express, source,
					XPathConstants.NODESET);
		} catch (XPathExpressionException e) {
			e.printStackTrace();
		}

		return result;
	}
}
实现三级联动的类:

package com.qiantu.component;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;

public class JComboboxOfChina {
	private JComboBox combobox_privince;
	private JComboBox combobox_city;
	private JComboBox combobox_area;
    private DefaultComboBoxModel model1 = new DefaultComboBoxModel();
	private DefaultComboBoxModel model2 = new DefaultComboBoxModel();
	private DefaultComboBoxModel model3 = new DefaultComboBoxModel();
	
	public JComboboxOfChina() {
		//设置省市区三级联动数据
        //设置第一级数据,从xml里面获取数据
		for(String str : XMLDao.getProvinces()) {
			model1.addElement(str);
		}
		combobox_privince = new JComboBox(model1);
		combobox_privince.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				JComboBox source = (JComboBox) evt.getSource();
				//根据获取的省份找到它下面的级别的市
				String provinces = (String) source.getSelectedItem();
				List<String> cities = XMLDao.getCities(provinces);
				model2.removeAllElements();
				for (String str : cities) {
					model2.addElement(str);
				}
				model3.removeAllElements();
				for (String str : XMLDao.getDistricts(cities.get(0))) {
					model3.addElement(str);
				}
			}
		});
		//设置二级数据
		for (String str : XMLDao.getCities("北京市")) {
			model2.addElement(str);
		}
		combobox_city = new JComboBox(model2);
		combobox_city.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				JComboBox source = (JComboBox) evt.getSource();
				String city = (String) source.getSelectedItem();
				List<String> districts = XMLDao.getDistricts(city);
				model3.removeAllElements();
				for (String str : districts) {
					model3.addElement(str);
				}
			}
		});
		//设置三级数据
		for (String str : XMLDao.getDistricts("北京市")) {
			model3.addElement(str);
		}
		combobox_area = new JComboBox(model3);
	}

	public JComboBox getCombobox_privince() {
		return combobox_privince;
	}

	public JComboBox getCombobox_city() {
		return combobox_city;
	}

	public JComboBox getCombobox_area() {
		return combobox_area;
	}
}

一个调用的例子:

package com.qiantu.component;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.WindowConstants;


public class TestJComboboxOfChina {
	public static void main(String[] args) {
		JFrame j = new JFrame();
		j.setSize(300,300);
		j.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		j.setLayout(null);
		
		//构建中国各大城市的三级联动下拉框
		JComboboxOfChina box = new JComboboxOfChina();
		
		//构造省级下拉框
		JLabel label_privince = new JLabel("省份:");
		label_privince.setBounds(50, 50, 50, 30);
		JComboBox combobox_privince = box.getCombobox_privince();
		combobox_privince.setBounds(100, 50, 150, 30);
		j.add(label_privince);
		j.add(combobox_privince);
		
		//构造市级下拉框
		JLabel label_city = new JLabel("城市:");
		label_city.setBounds(50, 100, 50, 30);
		JComboBox combobox_city = box.getCombobox_city();
		combobox_city.setBounds(100, 100, 150, 30);
		j.add(label_city);
		j.add(combobox_city);
		
		//构建区级下拉框
		JLabel label_area = new JLabel("地区:");
		label_area.setBounds(50, 150, 50, 30);
		JComboBox combobox_area = box.getCombobox_area();
		combobox_area.setBounds(100, 150, 150, 30);
		j.add(label_area);
		j.add(combobox_area);
		
		j.setVisible(true);
	}
}



你可能感兴趣的:(java,xml,三级联动,JComboBox)