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实现的无刷新省市区三级联动下拉则可以改善 ...
随机推荐
- Easyui登陆页面制作
一.登陆页面HTML <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Web ...
- C语言字符转换ASCII码
//函 数 名:CharToHex()//功能描述:把ASCII字符转换为16进制//函数说明://调用函数://全局变量://输 入:ASCII字符//返 回:16进制///////// ...
- ubuntu16.04安装kde桌面出错: /var/cache/apt/archives/kde-config-telepathy-accounts_4%3a15.12.3-0ubuntu1_amd64.deb
出错提示: 正在读取软件包列表... 完成 正在分析软件包的依赖关系树 正在读取状态信息... 完成 kubuntu-desktop 已经是最新版 (1.338). 您可能需要运行“apt-get - ...
- IWebBrowser隐藏滚动条
刚才在项目里看到一个IWebBrowser2,竟然需要通过MoveWindow的方式把滚动条遮挡,如果要缩小IWebBrowser2控件的显示大小呢?这种方法至少我用不习惯,起码也得从源头解决这样的问 ...
- 5.4 RegExp类型
ECMAScript通过RegExp类型来支持正则表达式.使用下面类似Perl的语法,就可以创建一个正则表达式. var expression=/pattern/flags; 复制代码 其中的模式(p ...
- .net 更改日期格式
示例:更改日期格式 下面的代码示例使用 Regex.Replace 方法将 mm/dd/yy 格式的日期替换为 dd-mm-yy 格式的日期. static string MDYToDMY(strin ...
- php mysql实现栏目分类递归
header("content-type:text/html;charset=utf-8"); $dbhost = "localhost"; // 数据库主 ...
- [LeetCode]题解(python):096-Unique Binary Search Trees
题目来源: https://leetcode.com/problems/unique-binary-search-trees/ 题意分析: 给定一个整数n,返回所有中序遍历是1到n的树的可能. 题目思 ...
- 24_Core Data Demo
今天开始学习Core Data,类似于数据库,可以永久保存数据.不过当把App从iPhone删掉之后就没有了.可以用来保存App的运行数据. 参考链接:iOS Swift教程 Core Data 概述 ...
- 转: vim简明教程
vim的学习曲线相当的大(参看各种文本编辑器的学习曲线),所以,如果你一开始看到的是一大堆VIM的命令分类,你一定会对这个编辑器失去兴趣的.下面的文章翻译自<Learn Vim Progress ...