【问题标题】:JComboBox populated by string array(using for loop) not appearing由字符串数组填充的JComboBox(使用for循环)未出现
【发布时间】:2013-03-12 08:08:32
【问题描述】:

我正在尝试用数字 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 的字符串数组中的数字。请帮助。

【问题讨论】:

    标签: java swing jcombobox arrays comboboxmodel


    【解决方案1】:

    尝试改变

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

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

    【讨论】:

      【解决方案2】:
      val[i] = Integer.toString(i+25);
      
      • 为 JComboBox 使用正确的数据类型,为 ComboBoxModel 使用 Integer

      • 例如(在模拟 ComboBoxModel 相同代码的 API 中实现的代码中使用 Vector)

      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();
                  }
              });
          }
      }
      

      【讨论】:

      • 非常感谢它有效。但我也想做类似 JComboBox 的东西,它显示至少一百年(元素),如 1950 - 2050 年,如果不逐一执行“vector.addElement()”,我怎么能做到这一点?当然,对于长长的元素列表,不建议逐个执行 .addElement。
      • 也许没有任何理由将数据存储在数组中,将它们直接放入 ComboBoxModel,这个模型与 Java 应用程序的其余部分相同(为 GUI 准备),see possible short_cuts in comments by @MadProgrammer跨度>
      猜你喜欢
      • 2022-12-10
      • 2019-05-27
      • 1970-01-01
      • 1970-01-01
      • 2014-04-09
      • 1970-01-01
      • 1970-01-01
      • 2015-09-22
      • 1970-01-01
      相关资源
      最近更新 更多