Jcombobox中下拉菜单的值比较多,用户不方便寻找,因此将这些值分类属于不同parent,在Jcombobox中初始只显示几个parent然后点击parent后 属于该parent的child会展开,再点一下就缩回。就像tree一样不知我的描述是否准确分不够再加~~
解决方案 »
- 求助 工程下每个文件包里的东西都是做什么的?
- 与tomcat服务器建立socket连接
- 将oracle数据库中的数据导入到access数据库中,有谁会导入,给我写个完成的例子,定重谢!
- 向大家请教:关于多个类共用Log4j的问题。
- 已知asc码,怎么转为汉字
- 想要从事JAVA方向的工作。要从哪里学起呢?
- 急救:struts项目,页面无法跳转。
- 高分请教:在myeclipse下基于框架的开发,工程从CVS上拉下来后,TOMCAT启动报错!
- 刚开始学习JAVA,下载了spring可是不知道怎么用
- 那位高人有<java思想>电子版侯捷译的?
- 开个java webservice的板块吧,寻支持
- 关于session丢失的问题,大家进来看看
public static void main(String[] args) throws Exception{
Item parent1=new Item("类别1");
parent1.add(new Item(" 1-001"));
parent1.add(new Item(" 1-002"));
parent1.add(new Item(" 1-003"));
Item parent2=new Item("类别2");
parent2.add(new Item(" 2-001"));
parent2.add(new Item(" 2-002"));
parent2.add(new Item(" 2-003"));
JComboBox cb=new MyComboBox(new Object[]{parent1,parent2}); JFrame r = new JFrame("测试");
r.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
r.setSize(300,200);
r.add(cb,BorderLayout.NORTH);
r.setVisible(true);
}
static class Item{
Item parent;
String title;
ArrayList<Item> subs;
public Item(String title){
this.title=title;
}
public String toString(){
return title;
}
public void add(Item it){
if(subs==null){
subs=new ArrayList<Item>();
}
it.parent=this;
subs.add(it);
}
}
static class MyComboBox extends JComboBox{
DefaultComboBoxModel model;
public MyComboBox(Object[] os){
super(os);
model=(DefaultComboBoxModel)getModel();
}
public void setPopupVisible(boolean b){
Item it=(Item)getSelectedItem();
int index=getSelectedIndex();
if(!b && it!=null && it.subs!=null){
boolean find=false;
for(int i=model.getSize();--i>=0;){
Item t=(Item)model.getElementAt(i);
if(t.parent==it){
//收拢
find=true;
model.removeElementAt(i);
}
}
if(!find){
//展开
ArrayList<Item> subs=it.subs;
for(Item t:subs){
index++;
model.insertElementAt(t,index);
}
}
return;
}
super.setPopupVisible(b);
}
}