求一个下拉菜单中带有类似Tree分层功能的JComboBox 例子 Jcombobox中下拉菜单的值比较多,用户不方便寻找,因此将这些值分类属于不同parent,在Jcombobox中初始只显示几个parent然后点击parent后 属于该parent的child会展开,再点一下就缩回。就像tree一样不知我的描述是否准确分不够再加~~ 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我觉得你说的这个像select二级联动的效果呢 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); } } 求这种效果${obj.time}.substring(0,10) 根据wsdl怎么建立服务 初学struts的问题 请教项目中的用户怎样与工作流shark参与者做映射!谢谢! 有没有介绍turbine和velocity的书? 入门问题 如何通过Oracle JDBC驱动向数据库插入非Oracle的Clob JMS是不是只能构建在j2ee平台上?? EJB问题,高分相送。 boolean 的运用问题,求指导 开个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);
}
}