Results 1 to 5 of 5
  1. #1
    Debugger is offline Member
    Join Date
    Jan 2011
    Posts
    27
    Rep Power
    0

    Default 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

    Java 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");
    						}
    					}
    this is the actionlistener for the start button

    Java 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
    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 classes

    Thanks in advance!

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,889
    Rep Power
    8

    Default

    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!

  3. #3
    Debugger is offline Member
    Join Date
    Jan 2011
    Posts
    27
    Rep Power
    0

    Default

    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?

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

  5. #5
    Debugger is offline Member
    Join Date
    Jan 2011
    Posts
    27
    Rep Power
    0

Similar Threads

  1. Replies: 1
    Last Post: 01-18-2011, 06:27 PM
  2. Why are the threads start and terminate randomly?
    By ggyyree in forum Threads and Synchronization
    Replies: 6
    Last Post: 12-14-2010, 04:11 PM
  3. Threads don't start after few iterations
    By gaurav2211 in forum Threads and Synchronization
    Replies: 2
    Last Post: 12-18-2009, 09:34 AM
  4. crawler doesn't start threads.
    By Pierced1 in forum Threads and Synchronization
    Replies: 2
    Last Post: 09-28-2009, 08:02 PM
  5. File Templates: line breaks not being used
    By TheShanMan in forum IntelliJ IDEA
    Replies: 0
    Last Post: 08-31-2009, 08:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •