2017-10-19 4 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を解決する助けに動作していませんadvance

答えて

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を使用しています エラーは です。2番目のコンボボックスが表示されません – sukesh

関連する問題