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

用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实现中国省市区三级联动的更多相关文章

  1. [moka同学转载]Yii2 中国省市区三级联动

    1.获取源码:https://github.com/chenkby/yii2-region 2.安装 添加到你的composer.json文件 "chenkby/yii2-region&qu ...

  2. MVC和WebForm 中国省市区三级联动

    MVC和WebForm是微软B/S端的两条腿,两种不同的设计理念,相对来说MVC更优于WebForm对于大数据的交互,因为WebForm是同一时间传输所有数据,而MVC它只是传输所用到的数据,更精确, ...

  3. Yii2 中国省市区三级联动

    1.获取源码:https://github.com/chenkby/yii2-region 2.安装 添加到你的composer.json文件 "chenkby/yii2-region&qu ...

  4. laraveladmin省市区三级联动

    Distpicker是一个中国省市区三级联动选择组件,这个包是基于Distpicker的laravel-admin扩展,用来将Distpicker集成进laravel-admin的表单中 安装 com ...

  5. ajax省市区三级联动

    jdbc+servlet+ajax开发省市区三级联动 技术点:jdbc操作数据库,ajax提交,字符拦截器,三级联动 特点:局部刷新达到省市区三级联动,举一反三可以做商品分类等 宗旨:从实战中学习 博 ...

  6. JS实现年月日三级联动+省市区三级联动+国家省市三级联动

    开篇随笔:最近项目需要用到关于年月日三级联动以及省市区三级联动下拉选择的功能,于是乎网上搜了一些做法,觉得有一些只是给出了小的案例或者只有单纯的js还不完整,却很难找到详细的具体数据(baidu搜索都 ...

  7. 【JavaScript&jQuery】省市区三级联动

    HTML: <%@page import="com.mysql.jdbc.Connection"%> <%@ page language="java&q ...

  8. javaweb--json--ajax--mysql实现省市区三级联动(附三级联动数据库)

    在web中,实现三级联动很常见,尤其是利用jquery+json.但是从根本上来说jquery并不是最能让人容易理解的,接下来从最基本的javascript开始,实现由javascript+json+ ...

  9. 用jsp实现省市区三级联动下拉

    jsp+jquery实现省市区三级联动下拉 不少系统都需要实现省市区三级联动下拉,像人口信息管理.电子商务网站.会员管理等,都需要填写地址相关信息.而用ajax实现的无刷新省市区三级联动下拉则可以改善 ...

随机推荐

  1. jquery 插件 validate 学习

    jquery是十分方便的对于现在来说. 首先应该明白一个问题: <p> <label  for="password">Password</label& ...

  2. Cookie获取、设置值

    设置: HttpCookie cookie = new HttpCookie("cookieName"); cookie.Value = "name1" Htt ...

  3. 前缀 树 背单词(Remember the Word,LA 3942)

    已哭瞎. 搞了2个多小时的错误居然是在  没有初始化............教训:每个例子一定要考虑到初始化问题.! 每个节点存了一个数组  该数组记录的有26个大小  0-25分别表示记录表示'a' ...

  4. C++ STL中map存储方式——SAP电面(4)

    map存储方式  一般是平衡二叉树 红黑树

  5. CDH 无法查看history log

    1.配置(core-site.xml) <property>  <name>hadoop.http.staticuser.user</name>  <valu ...

  6. 转移服务器,DEDE网站遇到no input file specified!

    公司新配置了服务器,需要从旧服务器上把原来的站点迁移到新服务器,迁移.NET网站没有遇到任何问题,但是在迁移过来一个用DEDE做的网站后,访问首页出现No Input File Specified,后 ...

  7. IntelliJ IDEA中创建并运行scala应用程序

    1.安装scala插件 2.创建scala项目 下载scala SDK,如果你已经下载了,选择你所下载的版本,点击OK

  8. (C#)Windows Shell 编程系列2 - 解释,从“桌面”开始展开

    原文 (C#)Windows Shell 编程系列2 - 解释,从“桌面”开始展开 (本系列文章由柠檬的(lc_mtt)原创,转载请注明出处,谢谢-) 接上一篇:(C#)Windows Shell 编 ...

  9. ActionBarSherlock学习笔记 第一篇——部署

    ActionBarSherlock学习笔记 第一篇--部署          ActionBarSherlock是JakeWharton编写的一个开源框架,使用这个框架,可以实现在所有的Android ...

  10. HTML的标题样式

    标题样式1 <p> <span style=" text-align: center; padding-bottom: 6px; padding-left: 20px; p ...