Results 1 to 8 of 8
  1. #1
    mbouras.f is offline Member
    Join Date
    Mar 2014
    Posts
    13
    Rep Power
    0

    Default Server/Client from JButtonl

    Hi.

    I am looking to run Server and Client from two diffirent JButton.

    when I click on start Server button, the server starts to run but I cannot click on start Client button to start the client
    here the code for server:
    Java Code:
    public void actionPerformed(ActionEvent e) {
    				Client c = new Client();
    				c.run();
    			}
    thank you

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,087
    Rep Power
    4

    Default Re: Server/Client from JButtonl

    You ofcourse realize that with three lines of code, no one is going to be able to help you. I do however have a wild guess: if Client is a Thread, then the method to use is start() and not run(). If it's not, then you need to provide more information.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    mbouras.f is offline Member
    Join Date
    Mar 2014
    Posts
    13
    Rep Power
    0

    Default Re: Server/Client from JButtonl

    Server is a class. when i run the server separetly with its main it works fine. and now i wanna run it from a button (JFrame application having 2 buttons: Server Start && Client Start)

    for the Server Start button:

    Java Code:
    JButton btnserver = new JButton("Start Server");
    		btnserver.addActionListener(new ActionListener() {
    
    			public void actionPerformed(ActionEvent e) {
    				Server s = new Server();
    				s.run();	
    			}
    });
    for the Client Start button:

    Java Code:
    JButton btnclient = new JButton("Start Client");
    		btnclient.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				Client c = new Client();
    				c.run();
    			}
    		});

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,087
    Rep Power
    4

    Default Re: Server/Client from JButtonl

    Your application proably hangs, because you execute the server on the EDT (The "GUI thread"). Try wrapping that code in a different thread so your EDT keeps running. Read the SwingWorker API on how to do that.

    SwingWorker (Java Platform SE 7 )
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default Re: Server/Client from JButtonl

    Did you read Surfman's reply? i.e not run() but start() if your Server and Client extend Threads; otherwise you tie up the EDT.

    kind regards,

    Jos

    edit: too late again, but that is old news ... *sigh*
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    mbouras.f is offline Member
    Join Date
    Mar 2014
    Posts
    13
    Rep Power
    0

    Default Re: Server/Client from JButtonl

    my Server has a void run()

    Class Server implements Runnable

    the start() method doesn't work.

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: Server/Client from JButtonl

    Quote Originally Posted by mbouras.f View Post

    the start() method doesn't work.
    Doesn't work says absolutely nothing. You get an exception? If so which one. Or nothing happens? If so: how do you know that? Did you put a breakpoint or put a few System.out in the thread code?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,087
    Rep Power
    4

    Default Re: Server/Client from JButtonl

    Quote Originally Posted by mbouras.f View Post
    my Server has a void run()

    Class Server implements Runnable

    the start() method doesn't work.
    Runnable has run(), but Thread has start(). Those are two different things. If it implements Runnable, you could start it like this:
    Java Code:
    new Thread( new Server() ).start();
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

Similar Threads

  1. Replies: 3
    Last Post: 06-03-2012, 10:48 AM
  2. Replies: 1
    Last Post: 06-03-2012, 04:07 AM
  3. Replies: 2
    Last Post: 09-24-2010, 12:31 PM
  4. Replies: 1
    Last Post: 10-05-2008, 04:12 PM
  5. Identify Client in Socket Client Server Application
    By masadjie in forum Networking
    Replies: 1
    Last Post: 12-20-2007, 10:18 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
  •