Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-14-2008, 02:20 PM
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-14-2008, 08:58 PM
Senior Member
 
Join Date: Jul 2007
Posts: 910
hardwired is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 05-01-2008, 04:08 PM
Member
 
Join Date: May 2008
Posts: 1
doraz72 is on a distinguished road
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...
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 05-05-2008, 10:19 AM
Eku Eku is online now
Member
 
Join Date: May 2008
Posts: 12
Eku is on a distinguished road
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
}
}
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
JTextArea - text align bradder AWT / Swing 1 11-29-2007 08:08 PM
problems trying to view the contents of a text file in JTextArea warship New To Java 1 07-19-2007 12:20 AM
problem trying to view the contents of a text file in JTextArea warship AWT / Swing 0 07-17-2007 04:30 PM
viewing the contents of a text file in JTextArea warship New To Java 0 07-17-2007 03:29 PM


All times are GMT +3. The time now is 10:50 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org