4 Replies Latest reply: Jul 8, 2013 12:00 PM by 935265 RSS

    JComboBox not refreshing

    935265

      Hi i have two class Banque and Succursale, Bank has a list of Succursale and each time that list changes i want Succursale to get it and then using it to refresh the JCombobox of its view, problem is that the JComboBox is not refreshing while its item count is incrementing, here are the class

       

      import java.io.IOException;
      import java.net.ServerSocket;
      import java.util.ArrayList;
      import java.util.Iterator;
      import java.util.List;
      import java.util.Observable;
      
      
      public class Banque extends Observable {
          private List<Succursale> listSucc = new ArrayList<Succursale>();
          private int sommeTotale;
          private int nbSuccInit = 4;
          
          public void ajouteSucc(Succursale suc){
              
          }
          public Banque(){
              initialiserServeur();        
          }
          private void initialiserServeur() {
              ServerSocket serverSocket = null; 
              try { 
                  serverSocket = new ServerSocket(10118); 
              } 
              catch (IOException e) 
              { 
                  System.err.println("On ne peut pas ecouter au  port: 10118."); 
                  System.exit(1); 
              }
              System.out.println ("Le serveur est en marche, Attente de la connexion.....");
              int i = 0;
              while(i<5){
                  try {
                      UtilMultiTh mt = new UtilMultiTh(serverSocket.accept());
                      Thread t = new Thread(mt);
                      t.start();
                      t.join();
                      Succursale s = mt.getSuc();
                      listSucc.add(s);
                      this.addObserver(s);
                      this.setChanged();
                      this.notifyObservers(this.listSucc);
                      
                      //System.out.println(listSucc.size());
                       
                     /* for(int j =0; j<listSucc.size();j++){
                          System.out.println("La succursale "+(j+1)+" est:"+listSucc.get(j).getName());
                      }
                      i++;
                      System.out.println("FLAGPOSTban");*/
                  } 
                  catch (IOException | InterruptedException e) 
                  { 
                      System.err.println("Accept a echoue."); 
                      System.exit(1); 
                  } 
              }
              
      
              System.out.println ("connexion reussie");
              System.out.println ("Attente de l'entree.....");
              
          }
          
          public static void main (String [] args){
              Banque banK = new Banque();
          }
      }
      
      

       

       

      The class Succursale

       

      import java.io.IOException;
      import java.io.ObjectOutputStream;
      import java.io.Serializable;
      import java.net.Socket;
      import java.net.UnknownHostException;
      import java.util.ArrayList;
      import java.util.List;
      import java.util.Observable;
      import java.util.Observer;
      
      import javax.swing.ComboBoxModel;
      
      
      public class Succursale implements Serializable, Observer {
          private String coordonnees;
          private String name;
          private int sommeDepart;
          private int sommeRecue;
          private int sommeEnvoyee;
          private List<Succursale> listSuccAc = new ArrayList<Succursale>();
          private GuiSuccursale succView;
          
          public Succursale (){
              succView = new GuiSuccursale(Constantes.sommeDepart,1);
              this.sommeDepart=Constantes.sommeDepart;
              this.name="Succursale: "+(1);
              connexionBanque();
          }
          public void connexionBanque(){
              String host = Constantes.adrBanque[0];
              int port = Constantes.portBanque[0];
              Socket echoSocket = null;
              try {
                  echoSocket = new Socket(host, port);
                  ObjectOutputStream oos = new ObjectOutputStream(echoSocket.getOutputStream());
                  oos.writeObject(this);
                  //System.out.println("FLAGPOSTSUcc");
              } catch (UnknownHostException e) {
                  System.err.println("Hôte inconnu: " + host);
                  System.exit(1);
              } catch (IOException e) {
                  e.printStackTrace();
                  System.err.println("Ne pas se connecter au serveur: " + host);
                  System.exit(1);
              }
          }
          public void connexionSuccursales(){
              
          }
          public void envoiManuel(){
              
          }
          public String getName() {
              return name;
          }
          public void envoiPeriodique(){
              
          }
          public static void main (String[] args){
              Succursale suc = new Succursale();
          }
          @Override
          public void update(Observable arg0, Object arg1) {
              this.listSuccAc= (List)arg1;
              this.succView.setListeSucc(listSuccAc);
              this.succView.revalidate();
              //this.succView.validate();
              //System.out.println("Mes succ connectees sont:"+this.listSuccAc.size());
              //System.out.println("Mes succ connectees sont:"+this.getName());
      
          }
      }
      
      

       

      The view of succursale

       

      import java.awt.Font;
      import java.awt.GridLayout;
      import java.util.List;
      
      import javax.swing.ComboBoxModel;
      import javax.swing.DefaultComboBoxModel;
      import javax.swing.JButton;
      import javax.swing.JComboBox;
      import javax.swing.JFrame;
      import javax.swing.JLabel;
      import javax.swing.JPanel;
      
      
      public class GuiSuccursale extends JFrame{
          
          private JLabel labelSum = new JLabel("Montant:");
          private JIntField champMontant = new JIntField(0, 0, Integer.MAX_VALUE);
          private JLabel labelSumDispo = new JLabel ("Somme Disponible");
          private JIntField champSumDispo;
          private JLabel labelSucc = new JLabel("Vers la Succursale:");
          //private ComboBoxModel cbm<Succursale>;
          private JComboBox  listeSucc  = new JComboBox<>();
          private JPanel pan = new JPanel();
          private JButton btnTransferer = new JButton("Transferer");
          private JButton btnEffacer = new JButton("Effacer");
          public  GuiSuccursale(int sumDepart, int num){
              init(sumDepart, num);
          }
          public void init(int sumDepart, int num){
              
              GridLayout gl = new GridLayout();
              gl.setColumns(2);
              gl.setRows(4);
              gl.setHgap(5);
              gl.setVgap(5);
              pan.setLayout(gl);
              
              labelSumDispo.setHorizontalAlignment(JLabel.CENTER);
              labelSumDispo.setFont(new Font("Tahoma", Font.BOLD, 11));
              
              champSumDispo = new JIntField(sumDepart, 0, Integer.MAX_VALUE);
              champSumDispo.setEditable(false);
              
              labelSucc.setHorizontalAlignment(JLabel.CENTER);
              labelSucc.setFont(new Font("Tahoma", Font.BOLD, 11));
              
              labelSum.setHorizontalAlignment(JLabel.CENTER);
              labelSum.setFont(new Font("Tahoma", Font.BOLD, 11));
              
              listeSucc.addItem("ahhh");
              
              pan.add(labelSumDispo);
              pan.add(champSumDispo);
              pan.add(labelSucc);
              pan.add(listeSucc);
              pan.add(labelSum);
              pan.add(champMontant);
              pan.add(btnTransferer);
              pan.add(btnEffacer);
              
              this.setContentPane(pan);
              this.setSize(300, 150);
              this.setTitle("Succursale: "+num);
              this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
              this.setLocationRelativeTo(null);
              this.setResizable(true);
              this.setVisible(true);
          }
          public JComboBox getListeSucc() {
              return listeSucc;
          }
          public void setListeSucc(List<Succursale> listeSucc) {
              this.listeSucc.removeAllItems();
              //this.champMontant.setText("3333");
              DefaultComboBoxModel dcbm = new DefaultComboBoxModel<Succursale>();
              for(int i = 0; i<listeSucc.size();i++){
                  System.out.println("l<index est:"+i);
                  
                  dcbm.addElement(listeSucc.get(i));
                  //System.out.println(this.listeSucc.getItemCount());
              }
              this.listeSucc.setModel(dcbm);
              GuiSuccursale sui = this;
                      sui.setVisible(true);
          }
          
      }
      
      

       

      And finally the UtilMultiTH class

       

      
      
      import java.io.BufferedReader;
      import java.io.IOException;
      import java.io.InputStreamReader;
      import java.io.ObjectInputStream;
      import java.io.ObjectOutputStream;
      import java.io.PrintWriter;
      import java.net.*;
      
      public class UtilMultiTh implements Runnable {
      
          private Socket soc;
          private Succursale suc;
          public UtilMultiTh(Socket s){
              //System.out.println("FLAGconsmth");
              this.soc = s;
              }
          public void run() {
              //System.out.println("FLAGPOSrun");
              ObjectOutputStream oos;
              ObjectInputStream ois;
              try{            
                  oos = new ObjectOutputStream(soc.getOutputStream());
                  ois = new ObjectInputStream(soc.getInputStream());
      
                  //System.out.println("La succ est");
                  try {
                      Object o = ois.readObject();
                      if(o!=null){
                          suc = (Succursale)o;
                          //System.out.println("La succ est"+suc.getName());
                      }
                      /*while(o!=null){
                          suc = (Succursale)o;
                          System.out.println("La succ est"+suc.getName());
                      }*/
                  } catch (ClassNotFoundException e) {
                      e.printStackTrace();
                  }
                  oos.close();
                  ois.close();
                  soc.close();
              } catch (IOException e1) {
                  e1.printStackTrace();
              } 
          }
          public synchronized Succursale getSuc() {
              return suc;
          }
          public void setSuc(Succursale suc) {
              this.suc = suc;
          }
          
      }
      
      

      And i tried to pass by a DefaultComboBoxModel but that too wasn't effective.

        • 1. Re: JComboBox not refreshing
          mKorbel

          - you have an issue with Concurency in Swing

           

          - all event to the already visible AWT/Swing GUI must be done on E(vent)D(ispatch)T(thread)

           

          - Swing GUI isn't notified, doesn't care that out of EDT are executed any changes, all those event must be done on EDT

           

          - use SwingWorker (required very good knowledge about Java Essential Classes), Runnable#Thread (but your code missed, output to Swing GUI must be wrapped into invokeLater) or (shortcuts, dirty way) use only invokeLater

           

          -  in Java6 can be in some cases EDT notified from  Runnable#Thread without using invokeLater as notifiers (thread safe methods in APIs), in Java7 never ...

           

          - job for MutableComboBoxModel, not for  DefaultComboBoxModel

          • 2. Re: JComboBox not refreshing
            935265

            Is it normal that the itemscount of the JComboBox indicates that the list has effectively been filled while the view it self doesn't show this ?

            • 3. Re: JComboBox not refreshing
              mKorbel

              Is it normal that the itemscount of the JComboBox indicates that the list has effectively been filled while the view it self doesn't show this ?

               

              again to re-read my points (especially - Swing GUI isn't notified, doesn't care that out of EDT are executed any changes, all those event must be done on EDT), read Oracle tutorial about Concurency in Swing, there are two important points

               

              - all changes to the already visible Swing GUI must be done on EDT

               

              - Swing, EDT is single threaded, (sure you can to use SecondaryLoop (Java7), but nothing will be changed about == 1. done on EDT, 2. single threaded)

              • 4. Re: JComboBox not refreshing
                935265

                Thanks mKorbel, i will try to change my code accordingly and see how it works.