View Single Post
  #1 (permalink)  
Old 03-14-2008, 02:20 PM
GuyFawkes GuyFawkes is offline
Member
 
Join Date: Mar 2008
Posts: 1
GuyFawkes is on a distinguished road
Need Help showing text in JTextArea
Hi guys, I hope i'm posting this in the correct section of the forum since my problem involves swing and networking.

Basically my problem is this: I'm new to java and trying to create a very basic irc client, nothing fancy at all. I have two classes. One class creates the socket connection to the irc server, and the second class draws a basic swing interface.

I want the information that the server sends through the socket to be displayed in a JTextArea, however this is where my problem starts. No matter what i try i can not get the JTextArea to show anything at all.

I've tested the socket class to make sure it is actually conencting to the server, and it works fine. I just cant get the text the server sends back to be displayed in the text area.

Here is my socket class:
Code:
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; } } } catch (IOException ioe) { } }
And here is my very basic user interface class:

Code:
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(); } }
I really would appreciate any help at all with this problem. I've been at it for days now and nothing i try works
Reply With Quote
Sponsored Links