java的JCombobox实现中国省市区三级联动
源代码下载:点击下载源代码
用xml存储中国各大城市的数据。
xml数据太多了就不贴上了,贴个图片:
要解释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的JCombobox实现中国省市区三级联动的更多相关文章
- [moka同学转载]Yii2 中国省市区三级联动
1.获取源码:https://github.com/chenkby/yii2-region 2.安装 添加到你的composer.json文件 "chenkby/yii2-region&qu ...
- MVC和WebForm 中国省市区三级联动
MVC和WebForm是微软B/S端的两条腿,两种不同的设计理念,相对来说MVC更优于WebForm对于大数据的交互,因为WebForm是同一时间传输所有数据,而MVC它只是传输所用到的数据,更精确, ...
- Yii2 中国省市区三级联动
1.获取源码:https://github.com/chenkby/yii2-region 2.安装 添加到你的composer.json文件 "chenkby/yii2-region&qu ...
- laraveladmin省市区三级联动
Distpicker是一个中国省市区三级联动选择组件,这个包是基于Distpicker的laravel-admin扩展,用来将Distpicker集成进laravel-admin的表单中 安装 com ...
- ajax省市区三级联动
jdbc+servlet+ajax开发省市区三级联动 技术点:jdbc操作数据库,ajax提交,字符拦截器,三级联动 特点:局部刷新达到省市区三级联动,举一反三可以做商品分类等 宗旨:从实战中学习 博 ...
- JS实现年月日三级联动+省市区三级联动+国家省市三级联动
开篇随笔:最近项目需要用到关于年月日三级联动以及省市区三级联动下拉选择的功能,于是乎网上搜了一些做法,觉得有一些只是给出了小的案例或者只有单纯的js还不完整,却很难找到详细的具体数据(baidu搜索都 ...
- 【JavaScript&jQuery】省市区三级联动
HTML: <%@page import="com.mysql.jdbc.Connection"%> <%@ page language="java&q ...
- javaweb--json--ajax--mysql实现省市区三级联动(附三级联动数据库)
在web中,实现三级联动很常见,尤其是利用jquery+json.但是从根本上来说jquery并不是最能让人容易理解的,接下来从最基本的javascript开始,实现由javascript+json+ ...
- 用jsp实现省市区三级联动下拉
jsp+jquery实现省市区三级联动下拉 不少系统都需要实现省市区三级联动下拉,像人口信息管理.电子商务网站.会员管理等,都需要填写地址相关信息.而用ajax实现的无刷新省市区三级联动下拉则可以改善 ...
随机推荐
- [LeetCode]题解(python):087-Scramble String
题目来源: https://leetcode.com/problems/scramble-string/ 题意分析: 给定一个字符串,字符串展成一个二叉树,如果二叉树某个或多个左右子树颠倒得到的新字符 ...
- python第一步
安装2.7的python 环境:到cmd下python,就可以跑代码了,要是想运行py文件,在命令行python test.py,记得在windows下把python加入环境变量 学习基础的语法: 注 ...
- c语言各类问题 代码
定义一个结构体,有两个成员变量,一个整型的n,一个字符型的c,利用结构体类型声明一个具有5个元素的数组,并随机初始化,根据成员变量n进行从小到大排序,然后输出 冒泡排序然后 在输出结构体#includ ...
- (Problem 46)Goldbach's other conjecture
It was proposed by Christian Goldbach that every odd composite number can be written as the sum of a ...
- S70卡
产品名称:Mifare 4K(S70)卡 芯片类型:Philips Mifare 1 S70(MOA2) 存储容量:32Kbit,32个分区,每分区两组密码 工作频率:13.56 MHz 通讯 ...
- swjtu 2213 A Game About Cards(模拟题)
题目链接:http://acm.swjtu.edu.cn/JudgeOnline/showproblem?problem_id=2213 思路分析:该问题与约瑟夫问题相似:每次将前n张牌放到队列的最后 ...
- 双网卡绑定(suse)
网卡绑定技术有助于保证高可用性特性并提供其它优势以提高网络性能,Linux双网卡绑定实现就是使用两块网卡虚拟成为一块网卡,这个聚合起来的设备看起来是一个单独的以太网接口设备,就是两块网卡具有相同的IP ...
- java 解析国密SM2算法证书
首先说明用Java自带的解析x509证书类,是不能解析sm2算法的证书,执行会抛出异常. 用开源库bouncycastle能够解析.详细代码 private byte[] getCSPK(byte[] ...
- python下使用protobuf
python解决ImportError: No module named google.protobuf 关于protocol buffer的优点,就过多涉及:如果涉及到数据传输和解析,使用pb会比自 ...
- windows版的node.js简单示例
1.下载node.exe放到任意目录,假设E:\nodejs\ 2.在E:\nodejs\下新建helloworld.js,输入以下内容,保存关闭 var http = require('http') ...