Results 1 to 4 of 4
- 03-14-2008, 01:20 PM #1
Member
- Join Date
- Mar 2008
- Posts
- 1
- Rep Power
- 0
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:
And here is my very basic user interface class:Java 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) { } }
I really would appreciate any help at all with this problem. I've been at it for days now and nothing i try works :confused:Java 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(); } }
- 03-14-2008, 07:58 PM #2
pkwooster did some pioneering work with Sockets at the Sun forums.
He left some examples that are now in the archives.
Here are some links that may be helpful:
Taming the NIO circus
NIO Client Example
Multithreaded Server Example // w/fixes
Simple Socket Client
// compatible with:
Simple Socket Server
Socket Example using Java 5
NIO Server Example // w/update to NIOConnection class
- 05-01-2008, 03:08 PM #3
Member
- Join Date
- May 2008
- Posts
- 1
- Rep Power
- 0
Better Late than never
First, declare the JPanel outside the public SwingTest() you call, then update it at the end of ActionListener by calling jp.updateUI();
This will refresh the UI with the new text...
- 05-05-2008, 09:19 AM #4
Senior Member
- Join Date
- May 2008
- Location
- Makati, Philippines
- Posts
- 234
- Rep Power
- 6
Try to create a refresh button.
And try this code. I hope it helps.
public void actionPerformed(ActionEvent event) {
String command = event.getActionCommand();
if (command.equals("Connect")) {
try {
SockTest mySock = new SockTest();
chat.append(mySock.chatText);
} catch (IOException ioe) {
chat.setText("Error " + ioe.getMessage());
}
}
else if (command.equals("Refresh")) {
//Create your refresh here
}
}
Similar Threads
-
JTextArea - text align
By bradder in forum AWT / SwingReplies: 1Last Post: 11-29-2007, 07:08 PM -
problems trying to view the contents of a text file in JTextArea
By warship in forum New To JavaReplies: 1Last Post: 07-18-2007, 11:20 PM -
problem trying to view the contents of a text file in JTextArea
By warship in forum AWT / SwingReplies: 0Last Post: 07-17-2007, 03:30 PM -
viewing the contents of a text file in JTextArea
By warship in forum New To JavaReplies: 0Last Post: 07-17-2007, 02:29 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks