Results 1 to 4 of 4
  1. #1
    GuyFawkes is offline Member
    Join Date
    Mar 2008
    Posts
    1
    Rep Power
    0

    Unhappy 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:
    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) {
    			
    		}
    		
    		}
    And here is my very basic user interface class:

    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();
        	
        	}
        
        
    }
    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:

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    10

    Default

    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

  3. #3
    doraz72 is offline Member
    Join Date
    May 2008
    Posts
    1
    Rep Power
    0

    Default 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...

  4. #4
    Eku
    Eku is offline Senior Member
    Join Date
    May 2008
    Location
    Makati, Philippines
    Posts
    234
    Rep Power
    8

    Default

    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

  1. JTextArea - text align
    By bradder in forum AWT / Swing
    Replies: 1
    Last Post: 11-29-2007, 08:08 PM
  2. Replies: 1
    Last Post: 07-18-2007, 11:20 PM
  3. Replies: 0
    Last Post: 07-17-2007, 03:30 PM
  4. viewing the contents of a text file in JTextArea
    By warship in forum New To Java
    Replies: 0
    Last Post: 07-17-2007, 02:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •