2017-10-19 34 views
0
public class dropit extends JFrame implements ActionListener{ 
    JComboBox lis,pop; 
    Map<String,String[]>map=new TreeMap<String,String[]>(); 
    JTextField tf; 
    String days[]={"sun","mon","tue","wed","thur","fri","sat"}; 
    String mon[]={"man","van"}; 
    String tue[]={"car","bus"}; 
dropit() 
{ 
    setLayout(null); 
    tf=new JTextField(); 
    tf.setBounds(50, 50, 100, 100); 
    add(tf); 
    map.put("mon", mon); 
    map.put("tue",tue); 
    lis=new JComboBox(days); 
    lis.setSelectedItem(4); 
    lis.addActionListener(this); 
    lis.setBounds(100,100,100,100); 
    add(lis); 


    /*pop=new JComboBox(); 
    pop.setBounds(200,100 , 100, 100); 
    add(pop);*/ 
    setVisible(true); 
    setSize(500,500); 
    } 
public void actionPerformed(ActionEvent e) 
    { 
    lis=(JComboBox) e.getSource(); 
    String name=(String) lis.getSelectedItem(); 
    tf.setText(name); 
    Iterator<String>iter=map.keySet().iterator(); 
    while(iter.hasNext()) 
    { 
     String arryname=iter.next(); 
     String []array=map.get(arryname); 
     if(arryname.contains(name)) 
     { 
      pop=new JComboBox(array); 
      pop.setBounds(200,100 , 100, 100); 
      add(pop) 
      }} 
     pop.repaint(); 
     pop.revalidate(); 
     } 
    public static void main(String[] args) { 
    new dropit(); 
      }} 

組合框是可以正常使用,但在接下來的組合框的填充值是單純的工作首次如何刷新每個選擇的Jcombobox?

我試圖重新繪製重新驗證,但沒有工作

幫我解決這個.Thanks在提前

回答

0

你永遠不會刪除創建的第一個pop,所以它隱藏了其他的。

public void actionPerformed(ActionEvent e) 
{ 
    lis = (JComboBox) e.getSource(); 
    String name = (String) lis.getSelectedItem(); 
    tf.setText(name); 

    if (map.containsKey(name)) 
    { 
     if (pop != null) 
      this.remove(pop); 
     pop = new JComboBox(map.get(name)); 
     pop.setBounds(200,100 , 100, 100); 
     add(pop); 
    } 
} 

,而不是添加和刪除的JComboBox或者,你可以只是空的,填充它,隱藏它,當map不包含的關鍵。

Dropit() 
{ 
    setLayout(null); 
    tf = new JTextField(); 
    tf.setBounds(50, 50, 100, 100); 
    add(tf); 
    map.put("mon", mon); 
    map.put("tue", tue); 
    lis = new JComboBox<String>(days); 
    lis.setSelectedItem("sat"); 
    lis.addActionListener(this); 
    lis.setBounds(100,100,100,100); 
    add(lis); 


    pop = new JComboBox<String>(); 
    pop.setBounds(200,100 , 100, 100); 
    pop.setVisible(false); 
    add(pop); 
    setVisible(true); 
    setSize(500,500); 
} 

public void actionPerformed(ActionEvent e) 
{ 
    lis = (JComboBox<String>) e.getSource(); 
    String name = (String) lis.getSelectedItem(); 
    tf.setText(name); 

    if (map.containsKey(name)) 
    { 
     pop.removeAllItems(); 
     for (String s: map.get(name)) 
      pop.addItem(s); 
     pop.setVisible(true); 
    } else 
     pop.setVisible(false); 
} 
+0

它不工作... – sukesh

+0

@sukesh我剛剛編輯帖子。你的IDE是什麼?什麼是錯誤? –

+0

我正在使用eclipse 錯誤是 第二個組合框不顯示 – sukesh