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.