当前位置: 技术问答>java相关
如何建立三个下拉列表之间的连动关系?在线等侯!!!
来源: 互联网 发布时间:2015-08-11
本文导语: 能否解决三个下拉列表的连动? 我的程序中有三个下拉列表。 例如: 第一个下拉列表中的值是各省名, 第二个下拉列表中显示该省下属的各市。 第三个下拉列表中显示该地区下属的各县。 谢谢! 在线等候。。...
能否解决三个下拉列表的连动?
我的程序中有三个下拉列表。
例如:
第一个下拉列表中的值是各省名,
第二个下拉列表中显示该省下属的各市。
第三个下拉列表中显示该地区下属的各县。
谢谢!
在线等候。。。
我的程序中有三个下拉列表。
例如:
第一个下拉列表中的值是各省名,
第二个下拉列表中显示该省下属的各市。
第三个下拉列表中显示该地区下属的各县。
谢谢!
在线等候。。。
|
我写了一个简单的例子,就用于连动显示省、市和县的关系,如下,你试运行一下,但愿对你有所帮助:
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
import java.awt.*;
public class Control3Combox extends JFrame {
private JComboBox comb1 ;
private JComboBox comb2 ;
private JComboBox comb3 ;
private Vector provinces = new Vector();
public Control3Combox() {
// comb1 = new JComboBox();
// comb2 = new JComboBox();
// comb3 = new JcomboBox();
jb_init();
}
private void jb_init(){
// create the first province
Province pro = new Province("Bei Jing");
City city1 = new City("海淀", pro);
County coun1 = new County("hd1", city1);
city1.addCounty(coun1) ;
coun1 = new County("hd2", city1);
city1.addCounty(coun1) ;
coun1 = new County("hd3",city1);
city1.addCounty(coun1) ;
pro.addCity(city1) ;
city1 = new City("chaoyang", pro);
coun1 = new County("cy1", city1);
city1.addCounty(coun1) ;
coun1 = new County("cy2",city1);
city1.addCounty(coun1) ;
pro.addCity(city1) ;
city1 = new City("丰台", pro);
coun1 = new County("ft1", city1);
city1.addCounty(coun1) ;
coun1 = new County("ft2",city1);
city1.addCounty(coun1) ;
pro.addCity(city1) ;
provinces.addElement(pro);
// create the second province
pro = new Province("He Bei");
city1 = new City("shijazhuang", pro);
coun1 = new County("sjz1", city1);
city1.addCounty(coun1) ;
coun1 = new County("sjz2", city1);
city1.addCounty(coun1) ;
coun1 = new County("sjz3",city1);
city1.addCounty(coun1) ;
pro.addCity(city1) ;
city1 = new City("handan", pro);
coun1 = new County("hd1", city1);
city1.addCounty(coun1) ;
coun1 = new County("hd2",city1);
city1.addCounty(coun1) ;
coun1 = new County("hd3",city1);
city1.addCounty(coun1) ;
pro.addCity(city1) ;
city1 = new City("bbb", pro);
coun1 = new County("bbb1", city1);
city1.addCounty(coun1) ;
coun1 = new County("bbb2",city1);
city1.addCounty(coun1) ;
pro.addCity(city1) ;
provinces.addElement(pro);
// set data of each comboBox
comb1 = new JComboBox(provinces);
comb2 = new JComboBox(((Province)provinces.elementAt(0) ).getCities() );
comb3 = new JComboBox(((City)(((ComboBoxModel)comb2.getModel()).getSelectedItem())).getCounties() );
comb1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
System.out.println(comb1.getSelectedItem());
System.out.println(((Province)comb1.getSelectedItem()).getCities() );
DefaultComboBoxModel model = new DefaultComboBoxModel(((Province)comb1.getSelectedItem()).getCities() );
comb2.setModel(model) ;
}
});
comb2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
if(comb2.getSelectedItem() != null){
System.out.println(comb2.getSelectedItem());
System.out.println(((City)comb2.getSelectedItem()).getCounties() );
DefaultComboBoxModel model = new DefaultComboBoxModel(((City)comb2.getSelectedItem()).getCounties() );
comb3.setModel(model) ;
}
}
});
this.getContentPane() .setLayout(new BorderLayout()) ;
getContentPane() .add(comb1,BorderLayout.WEST);
getContentPane() .add(comb2, BorderLayout.CENTER );
getContentPane() .add(comb3,BorderLayout.EAST );
setSize(400,80);
setVisible(true) ;
}
public static void main(String args[]){
Control3Combox c3c = new Control3Combox();
}
}
class Province {
private String name;
private Vector cities;
public Province(String name, Vector cities){
this.name = name;
this.cities= cities;
}
public Province (String name){
this(name, new Vector());
}
public String toString(){
return this.name;
}
public String getName(){return name;}
public void setName(String name){
this.name= name;
}
public Vector getCities(){return cities;}
public void addCity (City oneCity){
this.cities.addElement(oneCity);
}
public void setCities (Vector cities){
this.cities = cities;
}
}
class City {
private String name;
private Province province;
private Vector counties;
public City(String name, Province p, Vector counties){
this.name = name;
this.province =p;
this.counties = counties;
}
public City(String name, Province p){
this(name,p, new Vector());
}
public String toString(){
return this.name;
}
public String getName(){return name;}
public void setName(String name){
this.name = name;
}
public Vector getCounties(){return this.counties;}
public void addCounty(County oneCounty){
this.counties.addElement(oneCounty);
}
public void setCounties(Vector counties){
this.counties=counties;
}
}
class County{
private City city;
private String name;
public County(String name, City city){
this.name = name;
this.city = city;
}
public String toString(){
return name;
}
public String getName(){return this.name;}
public void setName(String name){
this.name= name;
}
}
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
import java.awt.*;
public class Control3Combox extends JFrame {
private JComboBox comb1 ;
private JComboBox comb2 ;
private JComboBox comb3 ;
private Vector provinces = new Vector();
public Control3Combox() {
// comb1 = new JComboBox();
// comb2 = new JComboBox();
// comb3 = new JcomboBox();
jb_init();
}
private void jb_init(){
// create the first province
Province pro = new Province("Bei Jing");
City city1 = new City("海淀", pro);
County coun1 = new County("hd1", city1);
city1.addCounty(coun1) ;
coun1 = new County("hd2", city1);
city1.addCounty(coun1) ;
coun1 = new County("hd3",city1);
city1.addCounty(coun1) ;
pro.addCity(city1) ;
city1 = new City("chaoyang", pro);
coun1 = new County("cy1", city1);
city1.addCounty(coun1) ;
coun1 = new County("cy2",city1);
city1.addCounty(coun1) ;
pro.addCity(city1) ;
city1 = new City("丰台", pro);
coun1 = new County("ft1", city1);
city1.addCounty(coun1) ;
coun1 = new County("ft2",city1);
city1.addCounty(coun1) ;
pro.addCity(city1) ;
provinces.addElement(pro);
// create the second province
pro = new Province("He Bei");
city1 = new City("shijazhuang", pro);
coun1 = new County("sjz1", city1);
city1.addCounty(coun1) ;
coun1 = new County("sjz2", city1);
city1.addCounty(coun1) ;
coun1 = new County("sjz3",city1);
city1.addCounty(coun1) ;
pro.addCity(city1) ;
city1 = new City("handan", pro);
coun1 = new County("hd1", city1);
city1.addCounty(coun1) ;
coun1 = new County("hd2",city1);
city1.addCounty(coun1) ;
coun1 = new County("hd3",city1);
city1.addCounty(coun1) ;
pro.addCity(city1) ;
city1 = new City("bbb", pro);
coun1 = new County("bbb1", city1);
city1.addCounty(coun1) ;
coun1 = new County("bbb2",city1);
city1.addCounty(coun1) ;
pro.addCity(city1) ;
provinces.addElement(pro);
// set data of each comboBox
comb1 = new JComboBox(provinces);
comb2 = new JComboBox(((Province)provinces.elementAt(0) ).getCities() );
comb3 = new JComboBox(((City)(((ComboBoxModel)comb2.getModel()).getSelectedItem())).getCounties() );
comb1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
System.out.println(comb1.getSelectedItem());
System.out.println(((Province)comb1.getSelectedItem()).getCities() );
DefaultComboBoxModel model = new DefaultComboBoxModel(((Province)comb1.getSelectedItem()).getCities() );
comb2.setModel(model) ;
}
});
comb2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
if(comb2.getSelectedItem() != null){
System.out.println(comb2.getSelectedItem());
System.out.println(((City)comb2.getSelectedItem()).getCounties() );
DefaultComboBoxModel model = new DefaultComboBoxModel(((City)comb2.getSelectedItem()).getCounties() );
comb3.setModel(model) ;
}
}
});
this.getContentPane() .setLayout(new BorderLayout()) ;
getContentPane() .add(comb1,BorderLayout.WEST);
getContentPane() .add(comb2, BorderLayout.CENTER );
getContentPane() .add(comb3,BorderLayout.EAST );
setSize(400,80);
setVisible(true) ;
}
public static void main(String args[]){
Control3Combox c3c = new Control3Combox();
}
}
class Province {
private String name;
private Vector cities;
public Province(String name, Vector cities){
this.name = name;
this.cities= cities;
}
public Province (String name){
this(name, new Vector());
}
public String toString(){
return this.name;
}
public String getName(){return name;}
public void setName(String name){
this.name= name;
}
public Vector getCities(){return cities;}
public void addCity (City oneCity){
this.cities.addElement(oneCity);
}
public void setCities (Vector cities){
this.cities = cities;
}
}
class City {
private String name;
private Province province;
private Vector counties;
public City(String name, Province p, Vector counties){
this.name = name;
this.province =p;
this.counties = counties;
}
public City(String name, Province p){
this(name,p, new Vector());
}
public String toString(){
return this.name;
}
public String getName(){return name;}
public void setName(String name){
this.name = name;
}
public Vector getCounties(){return this.counties;}
public void addCounty(County oneCounty){
this.counties.addElement(oneCounty);
}
public void setCounties(Vector counties){
this.counties=counties;
}
}
class County{
private City city;
private String name;
public County(String name, City city){
this.name = name;
this.city = city;
}
public String toString(){
return name;
}
public String getName(){return this.name;}
public void setName(String name){
this.name= name;
}
}
|
两个联动 你会吗?那三个还不是一样!
关键是处理联动的时候 数据存到哪怎么更新 往这边去思考 一定会找到答案的!
关键是处理联动的时候 数据存到哪怎么更新 往这边去思考 一定会找到答案的!
|
我在JBuilder中,用jComboBox做过这个,是两个的,三个应该也是一样的。下面是我的做法,不知道跟你情况一样不,给你参考一下。
添加jComboBox1的ActionPerformed事件,在该方法先判断选定的Item即jComboBox1.getSelectedItem().toString()是否变化,若变化了,则根据对jComboBox1的选择,查询出要在jComboBox2中显示的内容,然后先用jComboBox2.removeAllItems()方法删掉下一个相关下拉列表中的原有内容,再用jComboBox2.addItem()方法加入新的内容。
代码如下:
void jComboBox1_actionPerformed(ActionEvent e) {
if(selectedItem != jComboBox1.getSelectedItem().toString()){
selectedItem = jComboBox1.getSelectedItem().toString();
jComboBox2.removeAllItems();
parameterRow1.setString("queryColumn",selectedItem);
queryDataSet1.refresh();
queryDataSet1.open();
rowCount1 = queryDataSet1.getRowCount();
for(int i = 0;i
添加jComboBox1的ActionPerformed事件,在该方法先判断选定的Item即jComboBox1.getSelectedItem().toString()是否变化,若变化了,则根据对jComboBox1的选择,查询出要在jComboBox2中显示的内容,然后先用jComboBox2.removeAllItems()方法删掉下一个相关下拉列表中的原有内容,再用jComboBox2.addItem()方法加入新的内容。
代码如下:
void jComboBox1_actionPerformed(ActionEvent e) {
if(selectedItem != jComboBox1.getSelectedItem().toString()){
selectedItem = jComboBox1.getSelectedItem().toString();
jComboBox2.removeAllItems();
parameterRow1.setString("queryColumn",selectedItem);
queryDataSet1.refresh();
queryDataSet1.open();
rowCount1 = queryDataSet1.getRowCount();
for(int i = 0;i
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
站内导航:
特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!