Results 1 to 5 of 5
Thread: GUI breaks after threads start
- 02-23-2011, 05:30 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 27
- Rep Power
- 0
GUI breaks after threads start
Ok so this is for a game that i wrote for my final project in a programming class, it all worked great and we got great grade :) now im re visiting it and trying to make a GUI for the server. It is a 2d java shooter, client server, client connects, and once it has the min number of players the game begins. Before the minimum number of players was hard coded in, but for V2 i want to make it user defined with a GUI.
Here is what happens:
GUI and everything opens find, once the user enters in the number of players and hits start, a ServerSocket and a Socket is created and a new thread is kicked off every time a client connects to the socket.When start is hit the GUI "breaks" the start button remains depressed and the only way to close it is to pull up the task manager and kill it.
HOWEVER, the server does start and run correctly, i know this because i can still launch the client and connect to the server and my print statements come up.
Here is some code, not looking to be given the answer just help, i have tried various things such as having the Socket work be done in its own method, and even having the entire GUI run in its own thread, hope it something dumb
this is the actionlistener for the start buttonJava Code:public void actionPerformed(ActionEvent e){ try{ String temp = ""; temp = jtfNumberPlayer.getText(); if(temp.equalsIgnoreCase("")){ jtaOutput.append("Set Number of Player Required to Play!"); } else{ try{ numOfPlayersRequired = Integer.parseInt(temp); jtaOutput.append("\n Server is now starting, "+ numOfPlayersRequired + " players are required to play"); try{ System.out.println("The connection has been made via the Make ConnectionMethod"); ServerSocket ss = new ServerSocket(SOCKET); Socket cs = null; //accept connections while(true){ cs = ss.accept(); ServerThread st = new ServerThread(cs); st.start(); }//end while }//end try catch(BindException be){ jtaOutput.append("The socket the server is trying to use is already in use"); } catch(IOException ioe){ ioe.printStackTrace(); }//end catch } catch(NumberFormatException nfe){ jtaOutput.append("You entered a letter rather than a number"); } }
so although the GUI breaks it does reach the "made it to the server thread" print statement once a client connects. the run() method is lengthy so i wont put any of that up unless you think its needed, also will put client code if needed but that is also quite long and consists of many classesJava Code:class ServerThread extends Thread{ // Attributes Socket cs; private int enableJoin = 0, playerCount = 0; // Default constructor public ServerThread(Socket _cs){ this.cs = _cs; System.out.println("made it to the server thread"); }//end the threads constructor
Thanks in advance!
- 02-23-2011, 05:36 PM #2
It looks like you have a while(true) loop in your actionPerformed method, which is going to tie up the EDT, which will freeze the GUI. You probably want that in another thread.
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 02-23-2011, 05:50 PM #3
Member
- Join Date
- Jan 2011
- Posts
- 27
- Rep Power
- 0
THANK YOU! that worked. Put that while loop and gave it its own thread and everything worked fine. Much appreciated. Could you elaborate more with what you meant when you said it will tie up the EDT?
-
- 02-23-2011, 06:10 PM #5
Member
- Join Date
- Jan 2011
- Posts
- 27
- Rep Power
- 0
Similar Threads
-
Start Swing GUI program by Java Web Start with IE in Eclipse debug mode
By albertkao in forum EclipseReplies: 1Last Post: 01-18-2011, 06:27 PM -
Why are the threads start and terminate randomly?
By ggyyree in forum Threads and SynchronizationReplies: 6Last Post: 12-14-2010, 04:11 PM -
Threads don't start after few iterations
By gaurav2211 in forum Threads and SynchronizationReplies: 2Last Post: 12-18-2009, 09:34 AM -
crawler doesn't start threads.
By Pierced1 in forum Threads and SynchronizationReplies: 2Last Post: 09-28-2009, 08:02 PM -
File Templates: line breaks not being used
By TheShanMan in forum IntelliJ IDEAReplies: 0Last Post: 08-31-2009, 08:07 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks