Jump to content
  • Advertisement
Sign in to follow this  
mrhodes

[java] Swing Textfield question

This topic is 4888 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!