Results 1 to 4 of 4
  1. #1
    javastuden's Avatar
    javastuden is offline Senior Member
    Join Date
    Nov 2009
    Posts
    162
    Rep Power
    6

    Default switch data between jfram and jdialog

    hi all ,
    i have a class InventoryGUI in that i'm implements Observer and from the update method i'm updating the jtable row with new values.
    in the InventoryGUI i'm calling a LIstener class which extends observable and it addobserver with the values that are getting from a socket program.
    if a client send a data to a pport that is listening by the LIstener class.
    i had added a button to start the listening see the code in the class InventoryGUI

    Java Code:
    if (jButton1.getText().trim().equals("Start Listening")) {
    			jButton1.setForeground(Color.red);
    
    			jButton1.setText("Stop Listening");
    			listner = new Listner();
    			Observer observer = new InventoryGUI();
    			listner.addObserver(observer);
    			thread = new Thread(listner);
    			thread.start();
    
    		}

    and LIstener implements Runnable
    so it start lisning the port and when the data came it call setChanged() and notifyObservers(data);
    ok that will show the value in the jtable's 1st column

    ok
    now i add a new tab and it will call a class InventorySales
    that class when calling it will also do the same

    Java Code:
    			listner = new Listner();
    			Observer observer = new InventorySales();
    			listner.addObserver(observer);
    			thread = new Thread(listner);
    			thread.start();
    
    		}

    but if this code is added in my that pop up a jdialog window and show the data that are listening ,but it shows "Address already in use: JVM_Bind"

    if i changed listner = new SalesListner(); maeans a new class for that listening then also the same eror

    and if i send data to the second (jdialog) the data will go to the first frame InventoryGUI ,but the button on InventoryGUI is not clicked then there is no problem

    so how can i recognise that the if jdialog is popup then the observer has to send data to the jdialog


    please expalin me correctly ,i hope all of you go my problem



    thanks in advance
    Last edited by Fubarable; 05-17-2011 at 12:59 AM. Reason: code tags added

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

    Default

    I'm not sure I fully understand what you're doing and what your problem is, but I suspect that you want both Obsevers to listen to the same Observable and this may not be happening because you are creating another Observable. It seems that your Listner variable, listner is a class field, and that you initialize it when jButton1 is pressed (please rename this variable to something that makes sense since "jButton1" tells us and you nothing about the button's function). The problem may be coming when you create a new Listner object in your second bit of code, and then start a new thread with this object. What if you don't create a new object and use the current one and just add your new Observer to it, perhaps something like so?:

    Java Code:
          if (listner != null) {
             // listner = new Listner();
             Observer observer = new InventorySales();
             listner.addObserver(observer);
             // thread = new Thread(listner);
             // thread.start();
          } else {
             // your listner is null and perhaps you do need to create a new one and start a thread.
          }
    Oh, I added code tags to your post above. Please understand that the tags for this forum are [code] and [/code], not <code> and <code>

    Best of luck!

  3. #3
    javastuden's Avatar
    javastuden is offline Senior Member
    Join Date
    Nov 2009
    Posts
    162
    Rep Power
    6

    Default i will clear the problem

    thanks for reply

    See class A implemts runnable and in run method accepts the values from a port in that setchanged and notifyObservers(data); methods are there.

    class B implements Observer which uses a button and onclick will call the thread to start like this
    Java Code:
    listner = new Listner();
    
    			Observer observer = b;
    			listner.addObserver(observer);
    			thread = new Thread(listner);
    			thread.start();
    ok when the data came in the port it will call the observer's update and that will populate the table column.

    in the same class B a jmenuitem on select
    just call new C().main();(where C is another class and for the eas of use remove the String args[] from the main)

    and in the main()
    Java Code:
    c= new C();
    		Listner listner = new Listner();
    		Observer observer = c;
    		listner.addObserver(observer);
    		// used the same listner
    		Thread thread = new Thread(listner);
    		thread.start();
    		c.setVisible(true);
    if i clicked the button and select the jmenuitem then JVM already exist error will be showing ,if i clicked the button only no problem,and stped the program and then again run the program and selected the jmenuitem then no problem it will work correctly and data will be forwarded respectivly to the frame popup

    what is the problem and how can i correct it

    thanks in advance

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

Similar Threads

  1. Replies: 1
    Last Post: 03-28-2011, 07:19 AM
  2. JDialog
    By frenk_castle in forum AWT / Swing
    Replies: 3
    Last Post: 05-11-2010, 01:28 PM
  3. how to make Broswe button in jfram form to attache file
    By ibrahimyoussof in forum AWT / Swing
    Replies: 3
    Last Post: 04-12-2010, 09:40 PM
  4. Returning data from a JFrame/JDialog?!
    By Joe2003 in forum AWT / Swing
    Replies: 6
    Last Post: 01-08-2009, 01:14 AM
  5. help with jdialog
    By leonard in forum AWT / Swing
    Replies: 1
    Last Post: 08-05-2007, 06:37 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
  •