I'm working on a project in java. I am facing a problem in refreshing the combo box after adding new data in file. Combo box is in one file(class / frame) and add form is in another file / class.

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

SetPackages.java (this class is adding new data):

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 (here is the combo box):

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

I'm calling createComboBox() method in SetPackages.java but it is not refreshing the combo box. Kindly help me out.

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

1 个解决方案

#1


0

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

Here you create a new object with a new JComboBox CBPkgCode but you never add that to the current GUI.

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

更多相关文章

  1. java写入文件的几种方法小结
  2. 使用SAX Filter将新元素插入XML文件
  3. 小聊天程序,访问文件之间的变量
  4. 请问用Java如何逐行的读取一个文本文件呀?我现在只能完整读取.
  5. FilenameFilter文件名过滤器使用实例
  6. 怎么用java 实现两个web service之间调用各自的接口 实现数据的
  7. java操作ftp实现文件的上传下载(适用于图片文档服务器)
  8. (翻译)Java使用POI中的SXSSF处理大数据量Excel文档
  9. 黑马程序员——Java学习笔记 String类和基本数据类型对象包装类

随机推荐

  1. jquery ajax 跨域请求
  2. Node.js如何将Ajax成功消息传递给app.js
  3. jQuery在点击按钮上迭代/循环遍历数据表
  4. 对于带有占位符的select2.js,我无法选择第
  5. 代替jquery $.post 跨域提交数据的N种形
  6. jQuery使用之(四)处理页面的表单元素
  7. 中国省市区地址三级联动jQuery插件
  8. 如果原始页面加载是https,那么没有完整网
  9. 无法理解如何使用javascript和jquery代码
  10. 在不触发hashchange事件的情况下更改哈希