切换帧时组合框似乎是空的?

时间:2013-03-12 07:04:11

标签: java swing netbeans arraylist jcombobox

Addstudent具有5个组合框的框架具有填充不同阵列的框。 目前,当框架独立运行时,组合框很好;显示我想要显示的数组的所有项目。

然而,当我从框架切换到它时,e.g. (via button actions) AddStudent frame-> NewFrame -> AddStudent frame组合框显示为空,没有加载阵列。

按钮,可以从“NewFrame”切换到“AddStudent”框架

 private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {                                       

        ComboboxFrame CF = new ComboboxFrame();
        CF.setVisible(true);
        setVisible(false);

    }

“AddStudent”框架的编码,用于将阵列加载到组合框中

public class AddStudent extends javax.swing.JFrame {

    /**
     * Creates new form AddStudent
     */
public AddStudent() 
{
    initComponents();
}

    List<String> sundayList;
    List<String> mondayList;
    List<String> tuesdayList;
    List<String> wednesdayList;
    List<String> thursdayList;

    private void loadLists() throws IOException
    {
        //Creating the array of Activities to put into the ComboBoxes
        File f = new File("Activities.dat");

        sundayList = new ArrayList<>();
        mondayList= new ArrayList<>();
        tuesdayList= new ArrayList<>();
        wednesdayList= new ArrayList<>();
        thursdayList= new ArrayList<>();

        try{
            BufferedReader reader = new BufferedReader(new FileReader(f));

            while(reader.ready())
            {
                String CDay = reader.readLine();                               
                String CActivityName = reader.readLine();
                String CSupervisor = reader.readLine();
                String CLocation = reader.readLine();
                String CPaid = reader.readLine();
                String nothing = reader.readLine();

                if(CDay.equals("Sunday"))
                {
                    sundayList.add(CActivityName);
                }
                else if(CDay.equals("Monday"))
                {
                    mondayList.add(CActivityName);
                }
                else if(CDay.equals("Tuesday"))
                {
                    tuesdayList.add(CActivityName);
                }
                else if(CDay.equals("Wednesday"))
                {
                    wednesdayList.add(CActivityName);
                }
                else if(CDay.equals("Thursday"))
                {
                    thursdayList.add(CActivityName);
                }                
            }
            sundayList.add("-");
            mondayList.add("-");
            tuesdayList.add("-");
            wednesdayList.add("-");
            thursdayList.add("-");
            reader.close();

        }
        catch (IOException ex) 
        {
            Logger.getLogger(StartUpFrame.class.getName()).log(Level.SEVERE, null, ex);
        } 
               comboboxSunday.setModel(new DefaultComboBoxModel<>(sundayList.toArray(new String[sundayList.size()])));
               comboboxMonday.setModel(new DefaultComboBoxModel<>(mondayList.toArray(new String[mondayList.size()])));
               comboboxTuesday.setModel(new DefaultComboBoxModel<>(tuesdayList.toArray(new String[tuesdayList.size()])));
               comboboxWednesday.setModel(new DefaultComboBoxModel<>(wednesdayList.toArray(new String[wednesdayList.size()])));
               comboboxThursday.setModel(new DefaultComboBoxModel<>(thursdayList.toArray(new String[thursdayList.size()])));
    }
...
...
...
    public static void main(String args[]) {

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    AddStudent addStudent = new AddStudent();
                    addStudent.loadLists();
                    addStudent.setVisible(true);                                   
                } catch (IOException ex) {
                    Logger.getLogger(AddStudent.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }

}

1 个答案:

答案 0 :(得分:0)

当您点击按钮时,您似乎正在创建一个新的框架。新框架是否与原始框架共享元素(如组合框)? Java Swing对你可以重用的东西以及你不能重复使用的内容非常挑剔。作为基本准则,模型可以安全地重复使用,但JComponents应该独立存在(即,您可以跨框架共享模型,但不能共享JComponents本身。)