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

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

0818b9ca8b590ca3270a3433284dd417.png

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

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png

要解释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 getDistricts(String districts) {

List list = new ArrayList();

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

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 getCities(String provinces) {

List list = new ArrayList();

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

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 getProvinces() {

List list = new ArrayList();

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

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 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 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);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值