ArrayList的JComboBox<;字符串>;-不工作-Java Swing

ArrayList的JComboBox<;字符串>;-不工作-Java Swing,java,swing,arraylist,Java,Swing,Arraylist,我仍然在玩Java和swing(对这一切还是很陌生)。我试图用.txt文件中的数据填充JComboBox。我将数据拉入ArrayList,并尝试用ArrayList变量填充JComboBox。但是,当我运行应用程序时,组合框为空 这是数组代码: private ArrayList<String> list = new ArrayList<String>(); 这个类的整个混乱的代码 package messing with swing; import javax.sw

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

这是数组代码:

private ArrayList<String> list = new ArrayList<String>();
这个类的整个混乱的代码

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

    }      







}
包装与swing混为一谈;
导入javax.swing.*;
导入java.awt.*;
导入java.awt.event.*;
导入java.io.*;
导入java.util.*;
导入javax.swing.border.EmptyBorder;
公共类ReportGUI扩展了JFrame{
//田地
私有JButton viewAllReports=新JButton(“查看所有程序详细信息”);
私有JButton viewPrograms=新JButton(“查看与本课程相关的程序和专业”);
私人JButton viewTeachedCourses=新JButton(“查看该考官教授的课程”);
private JLabel courseLabel=新建JLabel(“选择课程:”);
私有JLabel-examicerlabel=新JLabel(“选择一个考官:”);
专用JPanel面板=新JPanel(新网格布局(6,2,4,4));
private ArrayList list=new ArrayList();
//私有字符串storeAllString=“”;
公共报表GUI(){
reportInterface();
allReportsBtn();
组合框();
fileRead();
}   
私有void fileRead(){
试一试{
扫描仪扫描=新扫描仪(新文件(“Examiner.txt”);
//ArrayList=新建ArrayList();
while(scan.hasNext()){
list.add(scan.next());
}
scan.close();
}
catch(filenotfounde异常){
e、 printStackTrace();
}
}
私有void reportInterface(){
setTitle(“选择报告规范”);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel panel=newjpanel(newflowlayout());
添加(面板、边框布局、中心);
设置大小(650200);
setVisible(真);
可设置大小(假);
setLocationRelativeTo(空);
}    
私有void allReportsBtn(){
JPanel面板=新JPanel(新网格布局(1,1));
专家组命令(新的清空命令(70,50,70,25));
panel.add(查看所有报告);
viewAllReports.addActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
JFrame AllDataGUI=newjframe();
新的AllDataGUI();
}
});         
添加(面板、边框布局、线_端);
}       
专用无效组合框(){
面板顺序(新的清空顺序(0,5,5,10));
String[]comboBox1Array=list.toArray(新字符串[list.size()]);
JComboxCombox1=新的JCombox1阵列;
面板。添加(检查标签);
panel.add(组合框1);
panel.add(viewTeachedCourses);
JComboBox comboBox2=新的JComboBox();
面板。添加(courseLabel);
panel.add(组合框2);
panel.add(查看程序);
添加(面板、边框布局、线条开始);
}      
}
你知道我哪里出错了吗

 public ReportGUI()
 {   
   reportInterface();
   allReportsBtn();     
   comboBoxes();
   fileRead();
 }   
正如奥比切雷所指出的那样:

  • 首先调用
    组合框()
    ,它创建组合框并用空列表填充组合框
  • 然后调用
    fileRead()
    方法,将数据添加到列表中,但这不会更新组合框的模型
  • 代码顺序需要颠倒:

    fileRead();
    comboBoxes();
    
    正如奥比切雷所指出的那样:

  • 首先调用
    组合框()
    ,它创建组合框并用空列表填充组合框
  • 然后调用
    fileRead()
    方法,将数据添加到列表中,但这不会更新组合框的模型
  • 代码顺序需要颠倒:

    fileRead();
    comboBoxes();
    

    Taka仔细看看你做事情的顺序

    public ReportGUI(){   
       reportInterface();
       allReportsBtn();     
       comboBoxes();
       fileRead();
     }   
    
    首先,调用
    reportInterface
    ,它初始化您的帧

    第二,调用
    allReportsBtn
    ,创建按钮

    第三,调用
    组合框
    ,将
    列表的内容应用于组合框,组合框为空

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

    您提供给
    JComboBox
    的数组与您读取的文件值的
    列表之间没有关系,因此即使您告诉combobox值已更改,它也不会看到更改

    试着做一些更像

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

    相反…

    塔卡仔细看看你做事情的顺序

    public ReportGUI(){   
       reportInterface();
       allReportsBtn();     
       comboBoxes();
       fileRead();
     }   
    
    首先,调用
    reportInterface
    ,它初始化您的帧

    第二,调用
    allReportsBtn
    ,创建按钮

    第三,调用
    组合框
    ,将
    列表的内容应用于组合框,组合框为空

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

    您提供给
    JComboBox
    的数组与您读取的文件值的
    列表之间没有关系,因此即使您告诉combobox值已更改,它也不会看到更改

    试着做一些更像

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

    相反…

    您确定
    列表中包含任何内容吗?另外,
    panel
    使用的布局管理器是什么?我们再次见面,Mad:P我将在上面发布整个类。这很尴尬,我现在很尴尬,所以不要太激动。@Splunk你在哪里将列表添加到组合框??!!当然,在列表被添加到组合框后,您也可以填充该列表。@Splunk将元素复制到数组中,这是在读取文件之前完成的。调用前调用
    fileRead()