Results 1 to 2 of 2
  1. #1
    JavaJimme is offline Member
    Join Date
    Nov 2012
    Posts
    30
    Rep Power
    0

    Default JFrame stuck on connect

    Hello, I've worked for a while on this chat client and it worked fine when I ran the connect(); method in the contructor, but the moment I have the connect(); method in an actionlistener and I press it the JFrame seems to get stuck..
    Here's the code:

    Java Code:
    public void connect(){
    		try{
    		
    			
    			
    		socket = new Socket(host,port);
    		in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    		out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
    			
    		writeLine("NICK :" + nick);
    		writeLine("USER " + user + " * * :" + realName);
    		
    		String line =  null;
    
    	    //keep reading the lines from the inputstream
    		while((line = in.readLine()) != null) {
    			try{
    			chatwindowDoc.insertString(chatwindowDoc.getLength(), line+"\n", null);
    				
    			AutoScroll(); //autoscroll down
    			 
    			 //If the line contains 001 it means you have connected to the server and you can type your join channel command
    			 if(line.contains("001")){
    				 joinChannel();
    			 }
    			 //The server reads the usernames in the channel with a line that starts with the number 353
    			 if(line.contains("JOIN")){
    				 if(!line.contains("PRIVMSG")){
    				 saveJoinedUsers(line);
    				 getJoinedUser(line);
    				 }
    			 }
    			 if(line.contains("QUIT")){
    				 if(!line.contains("PRIVMSG")){
    					 deleteUser(line);
    				 }
    			}
    			 
    			 if(line.contains("353")){
    				 if(!line.contains("PRIVMSG")){
    					 getChannelUsers(line);
    					 saveChannelUsers(line);
    				 }
    			 }
    			 
    			//All the messages to the server are being send with #PRIVMSG 
    			 if(line.contains("PRIVMSG "+channelName)){
    				 getMessage(line);
    			 }
    			 if(line.contains("PRIVMSG "+user)){
    					 getPrivateMessage(line);
    			 }
    			//when the line startswith PING the server needs to get a responde: "PONG" so we don't get disconnected, see below.
    			if (line.startsWith("PING")) {
    				if(!line.contains("PRIVMSG")){
    					sendPong(line);
    					}
    				}
    			}catch(BadLocationException ex){
    				ex.printStackTrace();
    			}
    		}
    		
    		}catch(IOException ex){
    			ex.printStackTrace();
    		}
    	}
    I really put alot of work in it so it wouls be awsome if somebody could help.. Now I have to say even when the frame gets stuck the sysout messages are still being send after I ran the connect(); method with an actionlistener.. It's just that the Frame gets stuck for some reason..

    J van der Meer

  2. #2
    JavaJimme is offline Member
    Join Date
    Nov 2012
    Posts
    30
    Rep Power
    0

    Default Re: JFrame stuck on connect

    Oh I got the solution this morning.. I didn't know that swing is not thread safe. So the easy solution is when you while loop in swing to just make another thread. Thanks anyway

Similar Threads

  1. Stuck already!
    By zb82 in forum New To Java
    Replies: 8
    Last Post: 05-09-2013, 12:31 AM
  2. Click in JFrame Titlebar sets the JFrame Opaque
    By tim8w in forum AWT / Swing
    Replies: 1
    Last Post: 02-02-2013, 07:29 AM
  3. Replies: 3
    Last Post: 11-27-2011, 05:18 AM
  4. Replies: 5
    Last Post: 06-14-2010, 06:10 PM
  5. Replies: 4
    Last Post: 04-22-2009, 11:02 PM

Tags for this Thread

Posting Permissions

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