[java] Swing Textfield question

Started by
2 comments, last by Son of Cain 18 years, 10 months ago
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
Michael RhodesTiger Studios Web Designhttp://tigerstudios.net
Advertisement
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
Michael RhodesTiger Studios Web Designhttp://tigerstudios.net
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

a.k.a javabeats at yahoo.ca
Yo, fast typer! ;)

Glad you fixed it.

Son Of Cain
a.k.a javabeats at yahoo.ca

This topic is closed to new replies.

Advertisement