Page 1 of 2 12 LastLast
Results 1 to 20 of 25
  1. #1
    kevinn205 is offline Member
    Join Date
    Nov 2011
    Posts
    65
    Rep Power
    0

    Default Add action listener to server

    Hey, I was wondering how i can add an action listener to my server and client

    is it possible to add an action listener to the section where it recieves requests? or do i need to change the type of requests it sends

    Thank you in advance for your help!!

    Java Code:
    //The server code Server.java:      
     import java.io.*;  
     import java.net.*;  
     import java.util.*;   
     /**   
      * This is to help people to write Client server application   
      *  I tried to make it as simple as possible... the client connect to the server  
      *  the client send a String to the server the server returns it in UPPERCASE thats all  
      */ 
    
     public class Server {    
         // the socket used by the server  
    
         private ServerSocket serverSocket;  
         // server constructor  
         Server(int port) {  
             /* create socket server and wait for connection requests */ 
    
             try  
             {  
                 serverSocket = new ServerSocket(port);  
                 System.out.println("Server waiting for client on port " + serverSocket.getLocalPort());  
                 while(true)   
                 {  
                     Socket socket = serverSocket.accept();  // accept connection  
                     System.out.println("New client asked for a connection");  
                     TcpThread t = new TcpThread(socket);    // make a thread of it  
                     System.out.println("Starting a thread for a new Client");  
                     t.start();  
                 }  
             }  
             catch (IOException e) {  
                 System.out.println("Exception on new ServerSocket: " + e);  
             }  
         }             
     //  you must "run" server to have the server run as a console application  
         public static void main(String[] arg) {  
             // start server on port 1500  
             new Server(1500);  
         }         
         /** One instance of this thread will run for each client */ 
        class TcpThread extends Thread {  
             // the socket where to listen/talk  
             Socket socket;  
             ObjectInputStream Sinput;  
             ObjectOutputStream Soutput;             
             TcpThread(Socket socket) {  
                 this.socket = socket;  
             }  
             public void run() {  
                 /* Creating both Data Stream */ 
                 System.out.println("Thread trying to create Object Input/Output Streams");  
                 try 
                 {  
                     // create output  
                     Soutput = new ObjectOutputStream(socket.getOutputStream());  
                     Soutput.flush();  
                     Sinput  = new ObjectInputStream(socket.getInputStream());  
                 }  
    
                 catch (IOException e) {  
                     System.out.println("Exception creating new Input/output Streams: " + e);  
                     return; 
                 }  
                 System.out.println("Thread waiting for a String from the Client");  
                 // read a String (which is an object)  
    
                 try {  
                     String str = (String) Sinput.readObject();  
                     Soutput.writeObject("hellow");
                     
                     Soutput.flush();  
                 }  
                 catch (IOException e) {  
                     System.out.println("Exception reading/writing  Streams: " + e);  
                     return;               
                 }  
                 // will surely not happen with a String  
                 catch (ClassNotFoundException o) {                
                 }  
                 finally {  
                     try {  
                         Soutput.close();  
                         Sinput.close();  
                     }  
                    catch (Exception e) {                     
                     }  
                 }  
            }  
         }  
     }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default Re: Add action listener to server

    is it possible to add an action listener
    What is the source of the ActionEvents that you want to listen for?

  3. #3
    kevinn205 is offline Member
    Join Date
    Nov 2011
    Posts
    65
    Rep Power
    0

    Default Re: Add action listener to server

    Aww, The actual source is a class that has action listener on a jbutton,

    If you mean what initiates it in the server client part that, I'm not sure, I was thinking maybe it could know by whenever the i/o recieved data?

    I want the server to know when the button is pressed and to inform the clients then have the clients refresh a jtable, any insight?

    Heres two snippets
    Java Code:
    StoreData2.acceptButton.addActionListener(new jtable());
            StoreData2.acceptButton.addActionListener(this);
    
    
    Public Jtable{
    class AA implements ActionListener {     
    		    
    			public void actionPerformed(ActionEvent e) {         
    			System.out.println("hi");
    			GetModel();
    				} 
    			}
    Last edited by kevinn205; 12-13-2011 at 10:13 PM.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default Re: Add action listener to server

    Can you explain what your problem is?

  5. #5
    kevinn205 is offline Member
    Join Date
    Nov 2011
    Posts
    65
    Rep Power
    0

    Default Re: Add action listener to server

    Ill try at the macro level and micro level;

    On a macro scale I am trying to get one class to add a Peron, Another class to add that person and refresh. Yet I also want another computer add that person and to refresh when the person is added. I used a action listener to add the person and notify the class on the same computer, however, I needed to run networking to notify the other computer so i began a server and client. So far the program notifys the server says hi whenever someone is added but I need to add a action listener that reaches the server and comes back to jtable.

    On a micro scale I'm trying to get my Server to add an action listener to all incoming requests

    does this help clarify?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default Re: Add action listener to server

    get my Server to add an action listener to all incoming requests
    I guess you are using the term action listener in a generic sense, not related to the java class ActionListener.
    If you want to allow the server to have listeners for events (incoming requests) you can either write your own listener support code: addListener, listenerInterface, yourEvent and a method to create the event and call the listeners the were saved by the addListener method.
    Or there is a java class that will help you with this: Observable

  7. #7
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Add action listener to server

    If I understand your question: think about how objects travel through a network - you create an object on one computer, send it to another computer (which is often through serialization) - when done now there are 2 instances of the same object residing in 2 different locations, calling the methods on one of those instances does nothing to the second, so if I understand your question correctly: no you cannot send an ActionListener to another computer and hope it will notify something running on the previous computer. As I mentioned in another thread of yours, you will need to notify via something like a Socket - this can be wrapped in something like an ActionListener, but the networking portion you will have to deal with.
    Last edited by doWhile; 12-13-2011 at 10:41 PM.

  8. #8
    kevinn205 is offline Member
    Join Date
    Nov 2011
    Posts
    65
    Rep Power
    0

    Default Re: Add action listener to server

    I suppose this has slightly confused me now :/ so what exactly would i send across the server?

    Ultimatly i want it to perform refresh(); in the jtable class

    would the other method you mentioned work better?(RMI)?

  9. #9
    kevinn205 is offline Member
    Join Date
    Nov 2011
    Posts
    65
    Rep Power
    0

    Default Re: Add action listener to server

    Oh and i wasnt hoping that it would notify I thought that I would do an action listener to notify it that something has happend which would then invoke another different action listener notify the second that second would happen, basicly a long chain of listeners

  10. #10
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Add action listener to server

    Quote Originally Posted by kevinn205 View Post
    I suppose this has slightly confused me now :/ so what exactly would i send across the server?

    Ultimatly i want it to perform refresh(); in the jtable class

    would the other method you mentioned work better?(RMI)?
    You can use whatever method you like....socket, rmi, etc...but you have to use them knowing that you cannot pass objects back and forth and expect calls to methods on one computer to make the call on the 'mirror' object on another computer unless you explicitly code it to do so.

    A simple approach - start a ServerSocket on the server. Each client connects to that server when it started (and disconnects when it is closed). The server keeps the list of connected sockets when one connects. When something changes, the client sends a message to this ServerSocket telling it to update clients (or the change on the server notifies the ServerSocket directly) - the ServerSocket then goes through the list of client sockets and notifies them with another message that the data has changed. The client, upon receiving this message performs the appropriate action.

  11. #11
    kevinn205 is offline Member
    Join Date
    Nov 2011
    Posts
    65
    Rep Power
    0

    Default Re: Add action listener to server

    Thank you, yes thats exactly what I was looking for now what I was wondering was how do I get the Server to notify all others that the data has changed? perhaps a action listener, or is there something better? If its a action listener I cant figure out what to add it to :/

    Basicly the way I have it right now the server recieves a message, then the server sends a message, but I know that a message shouldnt be able to invoke a change in another class?
    Last edited by kevinn205; 12-14-2011 at 03:36 AM.

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default Re: Add action listener to server

    a message shouldnt be able to invoke a change in another class?
    Why not? When the message is received, the receiving method can react to the message and make changes.

  13. #13
    kevinn205 is offline Member
    Join Date
    Nov 2011
    Posts
    65
    Rep Power
    0

    Default Re: Add action listener to server

    Woah now this could be helpfull! I'll explain where my logic came from

    I tried to make my action listener on one class create a string ERP which when the button was clicked made it = "happend", then on the other I created a if statment in the public static main void if adddata.ERP = "happend then .... refresh(); and it seemed like the message could not invoke the change because it didnt do what I asked . One of the problems I ran into is that it said that refresh is not static if it was static it still seemed not to work

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default Re: Add action listener to server

    Have you studied what doWhile said in post#10?

    I have no idea what you just said in the last post. Could you post the code you are talking about to show what problem you had?

  15. #15
    kevinn205 is offline Member
    Join Date
    Nov 2011
    Posts
    65
    Rep Power
    0

    Default Re: Add action listener to server

    Yeah, in this case that I just said message 12 (yes its confusing)

    is that I tried to use a message to invoke a change by using a method on the *same computer* and that It failed , if it were indeed possible then my problem is solved

    **the work around that i found was that an action listener can in fact work across classes** could you write a very simple way to communiate across classes without a action listener if you are aware? I would really appreciate it!!

    so say I clicked the ActionButton and it sent out a message "hi" to the server then the server sent a message to everyone "You have changed"

    could my Client take the message "You have changed" invoke each client to refresh , one problem i guess that needs to be adressed is that the client is on a different class then the jtable

    sorry i dont mean to be confusing im just a little lost this is very difficult stuff escpecially for me

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default Re: Add action listener to server

    a very simple way to communiate across classes without a action listener
    That could be done by a simple call to a method. A method in classA can call a method in classB.
    What does action listener have to do with communication between classes?

  17. #17
    kevinn205 is offline Member
    Join Date
    Nov 2011
    Posts
    65
    Rep Power
    0

    Default Re: Add action listener to server

    so heres my attempt

    class A loads up its own frame and a bunch of jcomboboxes that i can select values i fill them in and they contact my mysql database adding the data

    then it calls
    ClassB.Refresh();

    I have class B open with its own frame , would class B really refresh? Is it just a little more complex then calling the method do i need to specify which calls the method?


    What i ended up doing was using a listener to on class A to say Hi class B i noticed your accept button was pushed so i have decided to refresh the table for you



    Perhpas another solution if i complied it all as one program rather than two seperate programs with their own main that suggestion would work>?
    Last edited by kevinn205; 12-14-2011 at 04:35 AM.

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default Re: Add action listener to server

    if i complied it all as one program rather than two seperate programs with their own main
    Having all the classes in the same compilation unit(source file) instead of separate files for each class shouldn't make any difference except for the case of inner classes.
    If you are talking about separate JVMs executing separate programs on the same machine, then the two programs can NOT communicate by trying to call methods in the other program's classes.
    Multiple programs with main methods can execute in one JVM and communicate with each other within that single JVM.

  19. #19
    kevinn205 is offline Member
    Join Date
    Nov 2011
    Posts
    65
    Rep Power
    0

    Default Re: Add action listener to server

    Excellent description :)

    yes I am trying to make them communicate using differnt jvms,

    so now the fun part for me is how to get that to work, I assume what i can do is have it communicate with a server then contact my client that there has been changes to inform both jvms? bascily each jvm class will require a client I assume which can then invoke a change to the jvm? my biggest problem has been setting up the listener which recieves the message or object "changed" and then invokes a method to execute.
    I have been having problems getting my Jtable to listen for the request from the client portion

    One solution I have came up with is a delayed loop -- Is this infinite loop bad for performance or is it praticaly the same as a listener?
    Is there a simple way to replace this with some kind of notifyer or listener?
    Java Code:
    //getMessage();
    //(selects current message in server)
     Mesage= "No";
    (sets up a default value to the mesage listens for changes  
      while(a = true ){waiting(100);
      getMessage();
      if(!InfoID.equalsIgnoreCase(Message))
      {Message= "No";
       GetModel();
    (Returns model updated)
    }
    }public long();{
         long t0, t1;
         t0 =  System.currentTimeMillis();
        do{
        t1 = System.currentTimeMillis();}
        while ((t1 - t0) < (n));
    	  }}}
    Last edited by kevinn205; 12-14-2011 at 07:56 PM.

  20. #20
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default Re: Add action listener to server

    while(a = true )
    = is the assignment operator
    If you want to use a boolean's value in a condition, you can use it directly:
    while(a)

    Your coding style is very poor. You should NOT put any code following the opening {
    while(a = true ){waiting(100);

    The beginning of the statement with the opening { should be vertically in line with the ending } so you can easily see the block of code that is enclosed in the {}s

    Code nested within {}s should be indented 3-4 spaces
    Last edited by Norm; 12-14-2011 at 07:59 PM.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 2
    Last Post: 12-11-2011, 11:44 PM
  2. Problem in action listener
    By cool in forum AWT / Swing
    Replies: 4
    Last Post: 11-16-2010, 08:44 AM
  3. Action Listener
    By greatmajestics in forum AWT / Swing
    Replies: 8
    Last Post: 03-25-2010, 06:39 PM
  4. Action Listener? how to use this?
    By jeffrey in forum New To Java
    Replies: 2
    Last Post: 10-12-2009, 09:51 AM
  5. action listener on jcombobox
    By chkm8 in forum New To Java
    Replies: 2
    Last Post: 02-05-2009, 11:14 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
  •