在文件中添加新数据后,JComboBox没有刷新

时间:2016-12-27 18:38:33

标签: java jcombobox

我正在研究java中的一个项目。我在文件中添加新数据后刷新组合框时遇到问题。组合框位于一个文件(类/框架)中,添加表单位于另一个文件/类中。

SetPackages.java(此类正在添加新数据):

Packages pkg = new Packages(Integer.parseInt(TPkgId.getText()), TPkgName.getText(), Integer.parseInt(String.valueOf(CBminGuest.getSelectedItem())), Integer.parseInt(String.valueOf(CBmaxGuest.getSelectedItem())), Integer.parseInt(TMeal.getText()), Integer.parseInt(TMusic.getText()), Integer.parseInt(TDecoration.getText()));
ObjectOutputStream outputStream = null;
try {
    ArrayList<Packages> PackageList = readAllData();
    PackageList.add(pkg);
    outputStream = new ObjectOutputStream(new FileOutputStream("packages.ser"));
    for(int i = 0 ; i < PackageList.size() ; i++) {
        outputStream.writeObject(PackageList.get(i));                                                                                                                                                                                                                                
    }
    outputStream.close();                   
    JOptionPane.showMessageDialog(new JFrame(), "New Package Added Successfully");     
    ClearForm();
    Events ev = new Events(); //here I'm creating object to call method of 2nd class to populate combo box again
    ev.createComboBox();
}catch(IOException e) {
    System.out.println("IO Exception while opening file");
} finally { 
    try {
        if(outputStream != null) {
           outputStream.close();                                
        }
    } catch (IOException e) {
        System.out.println("IO Exception while closing file");
    }
}

Events.java(这是组合框):

public class Events extends JInternalFrame {
JTabbedPane tabs;
JPanel panel1;
JPanel panel2;
SpringLayout layout;
JLabel p1Head, PkgCode;
JComboBox CBPkgCode;

public Events(){
    super("Manage Events", false, true, false, false);
    tabs = new JTabbedPane();
    layout = new SpringLayout();
    ButtonHolder b = new ButtonHolder();

    panel1 = new JPanel(); 
    panel1.setLayout(layout);
    p1Head = new JLabel("Book An Event"); 
    p1Head.setFont(new Font("Arial Rounded MT Bold", 4, 18));
    PkgCode = new JLabel("Package Code");
    PkgCode.setFont(new Font("Arial Rounded MT Bold", 4, 14));
    CBPkgCode = new JComboBox();
    createComboBox();
    panel1.add(p1Head);
    panel1.add(PkgCode);
    panel1.add(CBPkgCode);
    layout.putConstraint(SpringLayout.WEST, p1Head, 300, SpringLayout.WEST, this);
    layout.putConstraint(SpringLayout.NORTH, p1Head, 20, SpringLayout.NORTH, this);
    layout.putConstraint(SpringLayout.WEST, PkgCode, 50, SpringLayout.WEST, this);
    layout.putConstraint(SpringLayout.NORTH, PkgCode, 40, SpringLayout.NORTH, p1Head);
    layout.putConstraint(SpringLayout.WEST, CBPkgCode, 150, SpringLayout.WEST, PkgCode);
    layout.putConstraint(SpringLayout.NORTH, CBPkgCode, 40, SpringLayout.NORTH, p1Head);
    tabs.addTab("Book an Event", panel1);

    add(tabs);
}

public void createComboBox(){
    List<String> codes = new ArrayList<String>();
    ObjectInputStream inputStream = null;
    try{
        inputStream = new ObjectInputStream(new FileInputStream("packages.ser"));
        boolean EOF = false;
        while(!EOF) {
            try {
                Packages myObj = (Packages) inputStream.readObject();
                codes.add(String.valueOf(myObj.getPkg_id()));
            }     
            catch (ClassNotFoundException e) {
            } catch (EOFException end) {
                EOF = true;
            }
        }
    } catch(FileNotFoundException e) {
        System.out.println("Cannot find file");
    } catch (IOException e) {
        System.out.println("IO Exception while opening stream");
    } finally { // cleanup code to close stream if it was opened
         try {
            if(inputStream != null)
                inputStream.close( );
         } catch (IOException e) {
            System.out.println("IO Exception while closing file");
         }
    }
    String[] codesArray = codes.toArray(new String[]{});
    CBPkgCode.setModel(new javax.swing.DefaultComboBoxModel(codesArray));
  }       
}

我在SetPackages.java中调用createComboBox()方法,但它没有刷新组合框。请帮助我。

1 个答案:

答案 0 :(得分:0)

  Events ev = new Events(); //here I'm creating object to call method of 2nd class to populate combo box again

在这里,您使用 new JComboBox CBPkgCode创建 new 对象,但您永远不会将其添加到当前GUI。