Hi guys, i'm in the process of learning Java and so far i'm really enjoying it. However i've hit a small problem. I have two seperate classes, one class creates the swing user interface and the other handles the connection to an irc server.

Here is the User interface class:

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;


public class SwingTest extends javax.swing.JFrame implements ActionListener {
	
	JButton connect = new JButton("Connect");
	JTextArea chat = new JTextArea(15, 20);
	
    public SwingTest() {
    	//set the title of the frame
    	super("Chat Window");
    	
    	//set the size of the frame
    	setSize(800, 600);
    	
    	//set what happens when the close button is clicked
    	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	
    	// create the scroll pane to add the text area to
    	chat.setLineWrap(true);
    	JScrollPane scroll = new JScrollPane(chat, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
    	
    	//create the container panel
    	JPanel jp = new JPanel();
    	
    	//create the layout manager and assign it to the container panel
    	BoxLayout horizontal = new BoxLayout(jp, BoxLayout.Y_AXIS);
    	jp.setLayout(horizontal);
    	
    	//add the text area and button to the container panel
    	jp.add(scroll);
    	jp.add(connect);
    	
    	//add the container panel to the frame
    	add(jp);
    	
    	//make the frame and its contents visible
    	setVisible(true);
    	
    	//add an event listener to the connect button
    	connect.addActionListener(this);
    	
    }
    
    public void actionPerformed(ActionEvent event) {
   				try {
   			SockTest mySock = new SockTest();
    		chat.append(mySock.chatText);
   		} catch (IOException ioe) {
   			chat.setText("Error " + ioe.getMessage());
   		}
    
    	}
    	
    	
    
    public static void main(String[] arguments)  {
    	//create an instance of the SwingTest class and let it do its thing
    	SwingTest st = new SwingTest();
    	
    	}
    
    
}

And here is the class the handles the connection to the irc server:

import java.io.*;
import java.net.*;
import java.util.*;

public class SockTest {
	
	public String chatter;
	public String chatText;
	

	public SockTest() throws java.io.IOException {
		String hostname = "efnet.xs4all.nl";
		int portnumber = 6669;
		Socket socktest = new Socket("efnet.xs4all.nl", portnumber);
		PrintStream out = new PrintStream(socktest.getOutputStream());
	    BufferedReader in = new BufferedReader(new InputStreamReader(socktest.getInputStream()));
		
		out.print("user "+ "strangename" + " stranger irc : " + "Stranger");
		out.print("\n");
		out.print("nick " + "Stranger");
		out.print("\n");
		
		boolean eof = false;
			
			try {
				while (!eof) {
			String charText = in.readLine();
			if (charText != null) {
			chatText = charText;
			} else {
				eof = true;
				//socktest.close();
			}
			
		}
		
		} catch (IOException ioe) {
			
		}
		
		}	
}

When i try to pass what the socket is sending to the JTextArea in the first class I see nothing, but if i change the socket class so it can run on its own and print out the variable recieve the socket data everything works fine.

Can anyone tell if i'm doing something wrong ? Been at this a couple of days now and just cant seem to figure out where I'm going wrong.

Recommended Answers

All 6 Replies

You are replacing "chatText" with every line that gets read from the stream. I believe you probably want to append the new text to "chatText" instead of replacing it.

String response = "";
while ( (response = in.readLine()) != null) {
    chatText += response+"\n";
}

Thanks for the reply Ezzaral, i tried what you suggest but still no luck.

I would add System.out.println(chatText); after the code that reads from the socket to make sure you're getting the response because your there's nothing wrong with the chat.append() statement.

Yes i tried that within the socket class itself and got the full MOTD from the server as well as respone that it had recieve my username.

Its a strange one, but thanks for offering your help I appreciate it :D

I'm still having no luck getting the socket data to the JTextArea :( , does anyone have any other suggestions I can try ? :

Ok I've decided to come back to this project after leaving it for a while, and I'm still having the same problem but not just with passing the text to the Jtext area.

I decided to try and make a console only version and it seems the problem in general is trying to pass the text out of the socket class.

If i let the program run long enough I will eventuall get the last response from the server which is a Ping timeout message, but i get nothing at all before that.

If i do a system.out.println from within the socket class its self i get everything. Does any one have any idea at all where i may be going wrong ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.