Sign in to follow this  
mrhodes

[java] Swing Textfield question

Recommended Posts

Hey everyone... I'm trying to write to a textfield in my client program with some difficulty :( I have 2 Classes so far, CMainApplet, and CNetwork. The main applet class has a method CreateGUI() and there it set's up a couple textfields and a command button. In the init function I create my network object, and I pass a reference to the MainApplet class. What I want to do is beable to append onto my textfield using that reference in the network class. It's not working. I get a NullPointer error. So, I wrote a method in the CMainApplet class called simply append. I thought that maybe if I try writing from the parent class of the textfield it would work... but same problem :( I think I know what the problem is. I beleive that what is happening is that the this pointer going to the network class is not pointing to the same data that I originally initialized. I think it is creating a new CMainApplet object, and of course when I try to append text to an un-initialized textfield its going to give me the nullPointer reference. So, if in fact that is correct... can anyone help me to get around this? It's very important that I be able to post status messages from various classes to the textfield. Thanks :) Mike

Share this post


Link to post
Share on other sites
Woohoo.... I figured it out :)

I wasn't using the event-dispatching thread. I modified my append method to loook like this..


public void appendMsg(final String msg)
{
Runnable updateChatText = new Runnable(){
public void run(){
chatText.append(msg + "\n");
}
};
SwingUtilities.invokeLater(updateChatText);
}


Hope this can help someone else out sometime

Also, if anyone knows a better way to do this can you please share :) I'd love to have the fastest code for this..

thanks

Mike

Share this post


Link to post
Share on other sites
Well, not sure if I understood you, but from what I can guess, your object is not instantiated by the time it is used, hence producing a simple NullPointerException.

Without seeing some code, I can't really tell you what is wrong, but you should consider passing the text field as a reference to the class who's going to "post" at it, that is, the CNetwork.

Hope I got it right ;)

Son of Cain

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this