2

I have a basic server working but I want to be able to press a button in the GUI to run said server.

Currently, when I press the button to run the server, the button stays pressed and the rest of the GUI is inaccessible until I force stop it in Netbeans. I want to include buttons to stop the server etc so I want to know how I can have the server code run in the background whilst interacting with the GUI for anything else.

How can I achieve this in the GUI?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Rookie
  • 1,879
  • 3
  • 17
  • 18
  • Please edit your question to provide some code details at the very least. Otherwise we'll be guessing what a "basic server" is. – Gray Mar 23 '12 at 22:01
  • @Andrew EDT == Event Dispatching Thread? – Gray Mar 23 '12 at 22:03
  • 1
    @Hovercraft I decide to upgrade that comment to an answer, since it is almost certainly the problem. – Andrew Thompson Mar 23 '12 at 22:03
  • @Gray Right first time. Every Swing programmer should be familiar with it, and what it means to program accounting for it. – Andrew Thompson Mar 23 '12 at 22:04
  • Sure @Andrew. I always answer and comment for posterity and try to be verbose. Cheers. – Gray Mar 23 '12 at 22:05
  • Thanks for the comments so far, I'll look into that link Hovercraft :-) Also, when I say 'basic server' it basically receives a file sent from a client before closing. – Rookie Mar 23 '12 at 22:07
  • @Gray *"I .. try to be verbose."* When I ***try*** to be verbose in a comment, I generally hit the character limit. :( – Andrew Thompson Mar 23 '12 at 22:07

2 Answers2

11

Don't block the EDT (Event Dispatch Thread). Run the server on a Thread. See Concurrency in Swing for more details.


Mentioned in a comment, but see also this (very much) related example of providing a Swing GUI for a server & client.

enter image description here

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
6

You should use Thread class for this purpose.
Implement runnable interface in your main class, then override run() method. Put the codes you want them to be done when the button is pressed in run() method. Then in the button ActionListener write this code:

new Thread(this).start();

This will execute the code in a different 'thread'.

mehrmoudi
  • 1,096
  • 1
  • 11
  • 22
  • 3
    I think your advice has some weaknesses as it reduces class cohesion and increases coupling. The GUI class should not also be the Runnable class as the socket code should be in a separate class and should be totally ignorant about all GUI code. Also better to use a SwingWorker rather than a plain Thread since the SwingWorker has tools that allow interaction and communication with the Swing thread that Thread doesn't have. – Hovercraft Full Of Eels Mar 23 '12 at 22:06
  • I just wanted to provide him with a fast solution for overcoming his problem; in a greater scale and bigger project yes you are right. This solution is not a good one! – mehrmoudi Mar 23 '12 at 22:09
  • 1
    my view, I think that this is OP's issue, I'd suggest to edit with real answer about EDT, better coud be to delete that – mKorbel Mar 23 '12 at 22:10