2016-12-27 1 views
0

Je travaille sur un projet en Java. Je suis confronté à un problème dans l'actualisation de la zone de liste déroulante après l'ajout de nouvelles données dans le fichier. La zone de liste modifiable est dans un fichier (classe/cadre) et le formulaire d'ajout est dans un autre fichier/classe. méthodeJComboBox ne rafraîchit pas après l'ajout de nouvelles données dans le fichier

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

J'appelle createComboBox() dans:

SetPackages.java (cette classe ajoute de nouvelles données):

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 (voici la zone de liste déroulante) SetPackages.java mais il ne rafraîchit pas la zone de liste déroulante. Merci de m'aider.

Répondre

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

Ici vous créez un nouvel objet avec une nouvelleJComboBox CBPkgCode mais vous ne l'ajouter à l'interface actuelle.

+0

son ne fonctionne toujours pas –

+0

s'il vous plaît ajouter un [MVCE] (http://stackoverflow.com/help/mcve) afin que nous puissions trouver votre problème. –

+0

Comme vous l'avez dit "Ici, vous créez un nouvel objet avec un nouveau JComboBox CBPkgCode mais vous ne l'ajoutez jamais à l'interface graphique actuelle." Maintenant, comment l'ajouter à l'interface graphique actuelle? –