Results 1 to 13 of 13
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: JComboBox not refreshing

  1. #1
    shonguiz is offline Member
    Join Date
    Jul 2013
    Posts
    6
    Rep Power
    0

    Default JComboBox not refreshing

    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

    Java Code:
     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

    Java Code:
    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.validate();
    		//System.out.println("Mes succ connectees sont:"+this.listSuccAc.size());
    		//System.out.println("Mes succ connectees sont:"+this.getName());
    
    	}
        }
    and the view of Succursale
    Java Code:
        import java.awt.Font;
        import java.awt.GridLayout;
        import java.util.List;
        
        import javax.swing.ComboBoxModel;
        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;
    	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));
    		
    		
    		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();
    		for(int i = 0; i<listeSucc.size();i++){
    			System.out.println("l<index est:"+i);
    			
    			this.listeSucc.addItem(listeSucc.get(i).getName());
    			System.out.println(this.listeSucc.getItemCount());
    		}
    	}
    	
        }
    I also tried the DefaultComboBoxModel but no success either there.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: JComboBox not refreshing

    • You should update the JComboBox's model, not the JComboBox itself.
    • Your code does not appear to abide by Swing's threading rules in that you appear to have long-running code tying up the Swing event dispatch thread or EDT. Consider doing all long-running processes in a background thread such as that provided by a SwingWorker, and only updating the GUI on the event thread.
    • When debugging complex code, you are far better off debugging in isolation in a small test program so as to get the essence of the problem and thereby more easily find its solution.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default Re: JComboBox not refreshing

    Moved from Advanced Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    shonguiz is offline Member
    Join Date
    Jul 2013
    Posts
    6
    Rep Power
    0

    Default Re: JComboBox not refreshing

    Quote Originally Posted by Fubarable View Post
    • You should update the JComboBox's model, not the JComboBox itself.
    • Your code does not appear to abide by Swing's threading rules in that you appear to have long-running code tying up the Swing event dispatch thread or EDT. Consider doing all long-running processes in a background thread such as that provided by a SwingWorker, and only updating the GUI on the event thread.
    • When debugging complex code, you are far better off debugging in isolation in a small test program so as to get the essence of the problem and thereby more easily find its solution.
    1/ I tried the JComboBoxModel, it also didn't work
    2/ Could rephrase your second point in a simpler way please or give me a concrete example as i am far from being a swing expert.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default Re: JComboBox not refreshing

    Quote Originally Posted by shonguiz View Post
    1/ I tried the JComboBoxModel, it also didn't work
    2/ Could rephrase your second point in a simpler way please or give me a concrete example as i am far from being a swing expert.
    1. Define 'didn't work' (didn't compile / threw exception / unexpected result / computer blew up)
    2. Lesson: Concurrency in Swing (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    3. SSCCE (Short, Self Contained, Correct (Compilable), Example)

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    shonguiz is offline Member
    Join Date
    Jul 2013
    Posts
    6
    Rep Power
    0

    Default Re: JComboBox not refreshing

    Quote Originally Posted by DarrylBurke View Post
    1. Define 'didn't work' (didn't compile / threw exception / unexpected result / computer blew up)
    2. Lesson: Concurrency in Swing (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    3. SSCCE (Short, Self Contained, Correct (Compilable), Example)

    db
    didn't work means that JcomboBox values weren't updated, when i printout its item count i see that the items have been added, but you don't notice that in the view. Also i tried something which is to instanciate another GuiSuccursale which is equal to my GuiSuccursal instance after the update and there i see ny list filled up correctly.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default Re: JComboBox not refreshing

    Time to post that SSCCE, than.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    shonguiz is offline Member
    Join Date
    Jul 2013
    Posts
    6
    Rep Power
    0

    Default Re: JComboBox not refreshing

    Well apart from the three classes that i already posted, only this threaded class was missing:
    Java Code:
        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;  
            }  
              
        }

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default Re: JComboBox not refreshing

    Quote Originally Posted by shonguiz View Post
    Well apart from the three classes that i already posted, only this threaded class was missing:
    You didn't read the SSCCE page, did you?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  10. #10
    shonguiz is offline Member
    Join Date
    Jul 2013
    Posts
    6
    Rep Power
    0

    Default Re: JComboBox not refreshing

    Quote Originally Posted by DarrylBurke View Post
    You didn't read the SSCCE page, did you?

    db
    It says that i have to provide a code that's compilable and since you can copy paste the code i posted, i assumed it was okay but if not i will provide a zip.

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default Re: JComboBox not refreshing

    What does the first S stand for?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  12. #12
    shonguiz is offline Member
    Join Date
    Jul 2013
    Posts
    6
    Rep Power
    0

    Default Re: JComboBox not refreshing

    Quote Originally Posted by DarrylBurke View Post
    What does the first S stand for?

    db
    I assumed it was simple enough as i already took out a lot of uncessary lines, but i will cut even more.

  13. #13
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default Re: JComboBox not refreshing

    Don't 'cut even more'. Write a new program that aims to demonstrate your problem, and nothing else.

    db
    Fubarable likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Refreshing a value
    By OPTYMISTA in forum New To Java
    Replies: 7
    Last Post: 04-03-2013, 11:35 PM
  2. refreshing
    By willy in forum New To Java
    Replies: 0
    Last Post: 12-04-2011, 10:56 AM
  3. Method value not refreshing
    By Thalvik in forum New To Java
    Replies: 3
    Last Post: 04-27-2011, 10:21 PM
  4. Replies: 2
    Last Post: 12-10-2009, 01:58 PM
  5. Bug in refreshing jsp
    By anki1234 in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 12-31-2007, 07:09 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •