【问题标题】:JComboBox from ArrayList<String> - Not working - Java Swing来自 ArrayList<String> 的 JComboBox - 不工作 - Java Swing
【发布时间】:2014-07-27 05:44:15
【问题描述】:

我还在玩 Java 和 Swing(对这一切还是很陌生)。我正在尝试使用 .txt 文件中的数据填充 JComboBox。我将数据拉入 ArrayList 并尝试使用 ArrayList 变量填充 JComboBox。但是,当我运行应用程序时,组合框是空白的。

这是数组代码:

private ArrayList<String> list = new ArrayList<String>();

文件阅读器代码:

private void fileRead(){
         try{
             Scanner scan = new Scanner(new File("Examiner.txt"));
            // ArrayList<String> list = new ArrayList<String>();
             while(scan.hasNext()){
                 list.add(scan.next());
             }
             scan.close();
         }
         catch (FileNotFoundException e){
             e.printStackTrace();
         }
     }

还有我用于组合框的混乱:

 private void comboBoxes(){                
        panel.setBorder(new EmptyBorder(0, 5, 5, 10));
        String[] comboBox1Array = list.toArray(new String[list.size()]);
        JComboBox comboBox1 = new JComboBox(comboBox1Array);          
        panel.add(examinerLabel);
        panel.add(comboBox1);          
        panel.add(viewTeachedCourses);
         JComboBox comboBox2 = new JComboBox();
         panel.add(courseLabel);         
         panel.add(comboBox2); 
         panel.add(viewPrograms);
         add(panel, BorderLayout.LINE_START); 

    }

这个类的全部代码。

package messing with swing;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.border.EmptyBorder;


public class ReportGUI extends JFrame{
    //Fields
    private JButton viewAllReports = new JButton("View All Program Details");
    private JButton viewPrograms = new JButton("View Programs and Majors Associated with this course"); 
    private JButton viewTeachedCourses = new JButton("View Courses this Examiner Teaches"); 
    private JLabel courseLabel = new JLabel("Select a Course: ");
    private JLabel examinerLabel = new JLabel("Select an Examiner: "); 
    private JPanel panel = new JPanel(new GridLayout(6,2,4,4));  
    private ArrayList<String> list = new ArrayList<String>();
    //private String storeAllString="";



     public ReportGUI(){   
       reportInterface();
       allReportsBtn();     
       comboBoxes();
       fileRead();
     }   



     private void fileRead(){
         try{
             Scanner scan = new Scanner(new File("Examiner.txt"));
            // ArrayList<String> list = new ArrayList<String>();
             while(scan.hasNext()){
                 list.add(scan.next());
             }
             scan.close();
         }
         catch (FileNotFoundException e){
             e.printStackTrace();
         }
     }

    private void reportInterface(){         
          setTitle("Choose Report Specifications");                   
          setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
          JPanel panel = new JPanel(new FlowLayout());        
          add(panel, BorderLayout.CENTER);
          setSize(650,200);
          setVisible(true);    
          setResizable(false);
          setLocationRelativeTo(null);
}    
    private void allReportsBtn(){
        JPanel panel = new JPanel(new GridLayout(1,1)); 
        panel.setBorder(new EmptyBorder(70, 50, 70, 25));
        panel.add(viewAllReports);        
        viewAllReports.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e){
                JFrame AllDataGUI = new JFrame();
                new AllDataGUI();
            }
        });         
        add(panel, BorderLayout.LINE_END);
    }       
    private void comboBoxes(){                
        panel.setBorder(new EmptyBorder(0, 5, 5, 10));
        String[] comboBox1Array = list.toArray(new String[list.size()]);
        JComboBox comboBox1 = new JComboBox(comboBox1Array);          
        panel.add(examinerLabel);
        panel.add(comboBox1);          
        panel.add(viewTeachedCourses);
         JComboBox comboBox2 = new JComboBox();
         panel.add(courseLabel);         
         panel.add(comboBox2); 
         panel.add(viewPrograms);
         add(panel, BorderLayout.LINE_START); 

    }      







}

任何想法我哪里出错了?

【问题讨论】:

  • 您确定list 包含任何内容吗?另外,panel 使用的是什么布局管理器?
  • 我们又见面了 Mad:P 我会在上面发布整个课程。这很尴尬,我现在很尴尬,所以不要太用力了。
  • @Splunk 你在哪里将列表添加到组合框??!!
  • 好吧,您还可以填充列表它被添加到组合框之后。
  • @Splunk 您将元素复制到数组中,这是在读取文件之前完成的。在ReportGUI 构造函数中调用comboBoxes() 之前调用fileRead()

标签: java swing arraylist


【解决方案1】:
 public ReportGUI()
 {   
   reportInterface();
   allReportsBtn();     
   comboBoxes();
   fileRead();
 }   

正如 Obicere 指出的那样:

  1. 您首先调用comboBoxes(),它会创建组合框并使用空列表填充组合框。
  2. 然后调用fileRead() 方法将数据添加到列表中,但这不会更新组合框的模型。

代码顺序需要颠倒:

fileRead();
comboBoxes();

【讨论】:

  • 漫长的一天。忍不住写了这么多文字哈哈。好家伙
【解决方案2】:

Taka 仔细看看你做的顺序……

public ReportGUI(){   
   reportInterface();
   allReportsBtn();     
   comboBoxes();
   fileRead();
 }   

首先,您调用reportInterface,它会初始化您的框架...

其次,您调用allReportsBtn,它会创建您的按钮...

第三,您调用comboBoxes,它将List 的内容应用到您的组合框,它是空的...

第四,你调用fileRead,它从文件中读取值...

您提供给JComboBox 或您读取的文件值的List 的数组之间没有关系,因此即使您告诉组合框值已更改,它也不会看到该更改

尝试做一些类似的事情

public ReportGUI(){   
   reportInterface();
   allReportsBtn();     
   fileRead();
   comboBoxes();
 }   

相反...

【讨论】:

  • 我不会撒谎,我真的不知道您调用方法的顺序很重要。谢谢大家。
  • 有也有没有,这里重要的是组件和List之间的关系不存在,所以即使你更新List,组件根本无法知道它已更改或查看这些更改。
  • @Splunk 你能帮我一个忙吗,从技术上讲,camickr 首先回答了我们的答案,如果你在我的答案中找不到任何比他更能帮助你的东西,你能将他的问题标记为正确
  • 由于某种原因,现在我的组合框只显示一个单词。例如,不是在一行显示 Andrew Taylor,而是在一行显示 Andrew,然后在另一行显示 Taylor。
  • 美丽。我也将其标记为 camickr 已回答,因为他比您早一分钟到达:P
猜你喜欢
  • 2014-01-13
  • 2017-11-10
  • 1970-01-01
  • 2013-01-27
  • 2012-01-29
  • 1970-01-01
  • 2015-05-13
  • 1970-01-01
  • 2020-09-11
相关资源
最近更新 更多