2017-01-13 9 views
0

私はArrayListを設定したいコンボボックスでGUIを作成します。私は自分のコーディングで試しましたが、うまくいきません。Swingのコンボボックスに配列リストを入力してください

private void jcbSourceActionPerformed(java.awt.event.ActionEvent evt) {           
     ArrayList al=new ArrayList(); 
     al.add("A"); 
     al.add("B"); 
     al.add("C"); 
     al.add("D"); 
     al.add("E"); 

     jcbSource.setModel(new DefaultComboBoxModel(al.toArray())); 
     jcbSource.addItem(al.toString()); 
    } 
+0

下記の例のようにJComboBox<String>ArrayList<String>DefaultComboBoxModel<String>を使用し、ジェネリック医薬品のためStringタイプを設定するには、投稿[MCVE]または[ショート、自炊、正しい例](http://www.sscce.org/)。 –

+0

よろしいですか。ありがとうございます:) –

+0

あなたは明確にすることができますか? – Sergey

答えて

1

てみてください、すなわちが早いほど良いのヘルプについて

public class Test extends JFrame { 
    public Test() { 
     getContentPane().setLayout(new FlowLayout()); 
     final JComboBox<String> jcbSource = new JComboBox<String>(); 
     jcbSource.setSize(new Dimension(30, 20)); 
     add(jcbSource); 

     JButton setupButton = new JButton("Setup model"); 
     setupButton.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       ArrayList<String> al = new ArrayList<String>(); 
       al.add("A"); 
       al.add("B"); 
       al.add("C"); 
       al.add("D"); 
       al.add("E"); 

       String[] items = new String[al.size()]; 
       al.toArray(items); 

       jcbSource.setModel(new DefaultComboBoxModel<String>(items)); 
      } 
     }); 
     add(setupButton); 

     pack(); 
    } 

    public static void main(String[] args){ 
     new Test().setVisible(true); 
    } 
} 
+0

夕食を済ませたら結果を教えてください。 –

+0

こんにちは、サーゲイ、ご指摘ありがとうございます。その仕事! –

+0

どういう意味ですか?私のGUIの_comboボックスはまだarraylist要素を知っていませんか? – Sergey

関連する問題