2013-03-12 27 views
0

我想填充我的JComboBox與序號25〜50,這是我做了什麼,的JComboBox的字符串數組填充(使用循環)沒有出現

--at the variable declarations-- 

String[] val = new String[25]; 
JComboBox box1 = new JComboBox(); 

--and in the "main"-- 

for(int i=0; i==val.length; i++){ 
    val[i] = Integer.toString(i+25); 
} 

box1.setModel(new DefaultComboBoxModel(val)); 

但最終,JComboBox時只顯示空白的25個空格,但不顯示應該保存在數字25 - 50的字符串數組中的數字。請幫忙。

回答

4

嘗試改變

for(int i=0; i==val.length; i++){ 

for(int i=0; i<val.length; i++){ 
+0

仍無法正常工作,感謝您的幫助,雖然 – 2013-03-12 09:00:03

2
val[i] = Integer.toString(i+25); 
  • 使用適當的數據類型的JComboBox,使用整數爲例如在(矢量的使用的ComboBoxModel

  • 代碼實現mented在API模擬相同的代碼的ComboBoxModel)

enter image description here

import java.awt.GridLayout; 
import java.util.Vector; 
import javax.swing.Icon; 
import javax.swing.JComboBox; 
import javax.swing.JFrame; 
import javax.swing.SwingUtilities; 
import javax.swing.UIManager; 

public class ComboBoxIntegerModel { 

    private JComboBox comboBoxDouble; 
    private JComboBox comboBoxInteger; 
    private JComboBox comboBoxBoolean; 
    private JComboBox comboBoxIcon; 
    private Vector<Double> doubleVector = new Vector<Double>(); 
    private Vector<Integer> integerVector = new Vector<Integer>(); 
    private Vector<Boolean> booleanVector = new Vector<Boolean>(); 
    private Vector<Icon> iconVector = new Vector<Icon>(); 
    private Icon icon1 = ((UIManager.getIcon("OptionPane.errorIcon"))); 
    private Icon icon2 = (UIManager.getIcon("OptionPane.informationIcon")); 
    private Icon icon3 = (UIManager.getIcon("OptionPane.warningIcon")); 
    private Icon icon4 = (UIManager.getIcon("OptionPane.questionIcon")); 

    public ComboBoxIntegerModel() { 
     doubleVector.addElement(1.001); 
     doubleVector.addElement(10.00); 
     doubleVector.addElement(0.95); 
     doubleVector.addElement(4.2); 
     comboBoxDouble = new JComboBox(doubleVector); 
     integerVector.addElement(1); 
     integerVector.addElement(2); 
     integerVector.addElement(3); 
     integerVector.addElement(4); 
     comboBoxInteger = new JComboBox(integerVector); 
     booleanVector.add(Boolean.TRUE); 
     booleanVector.add(Boolean.FALSE); 
     comboBoxBoolean = new JComboBox(booleanVector); 
     iconVector.addElement(icon1); 
     iconVector.addElement(icon2); 
     iconVector.addElement(icon3); 
     iconVector.addElement(icon4); 
     comboBoxIcon = new JComboBox(iconVector); 
     JFrame frame = new JFrame(""); 
     frame.setLayout(new GridLayout(2,2,5,5)); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.add(comboBoxDouble); 
     frame.add(comboBoxInteger); 
     frame.add(comboBoxBoolean); 
     frame.add(comboBoxIcon); 
     frame.setLocationRelativeTo(null); 
     frame.pack(); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       ComboBoxIntegerModel comboBoxModel = new ComboBoxIntegerModel(); 
      } 
     }); 
    } 
} 
+0

非常感謝它的工作原理。但我也想做一些像JComboBox那樣的顯示至少一百年(元素)(比如1950 - 2050年)的JComboBox,但是我怎麼能夠一個接一個地做「vector.addElement()」呢?當然,對於長長的元素列表,不建議使用.addElement。 – 2013-03-12 09:20:15

+0

也許沒有任何理由將數據存儲在數組中,直接將它們放到ComboBoxModel中,這個模型與Java應用程序的其餘部分是相同的數組(準備用於GUI)[參見@MadProgrammer評論中可能的short_cuts](http ://stackoverflow.com/a/15355529/714968) – mKorbel 2013-03-12 09:24:46