aspose file tools*
The moose likes Beginning Java and the fly likes Realtime update when changing value in JTextField Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Realtime update when changing value in JTextField" Watch "Realtime update when changing value in JTextField" New topic
Author

Realtime update when changing value in JTextField

Christoffer Nilsen
Greenhorn

Joined: Apr 08, 2013
Posts: 9

Hello, I'm new to the forum and programming in general.

I have decided to study Computer Science next year, 2014/2015 and know my school start out with learning Java the first year.
Because of this I have started to look into programming and Java.

I have a few questions, I try to google and play around with my code before asking questions, but feel I will learn more getting some pointers by more experienced people.

So over to the question:
I'm trying to create a program where people can put a value into a JTextField1 & JTextField2, and the answer will automatically appear into JTextField3 without having to press Enter or any other function.
When changing the value of JTextField1 or JTextField2 it will update realtime into JTextField3.

What kind of tool do I have to use to do this?

Offtopic:
When I have more questions, should I make a new thread or just put them here?

- Nilsen
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2405
    
  50
Welcome to the ranch.

I'm trying to create a program where people can put a value into a JTextField1 & JTextField2, and the answer will automatically appear into JTextField3 without having to press Enter or any other function.
When changing the value of JTextField1 or JTextField2 it will update realtime into JTextField3.

What kind of tool do I have to use to do this?

I'm not sure what you mean by 'tools'. I assume you already have a jdk and some form of text editor so do you mean how do you write the program?

When I have more questions, should I make a new thread or just put them here?

If they are a continuation of this topic then please put them here else start a new thread.
Christoffer Nilsen
Greenhorn

Joined: Apr 08, 2013
Posts: 9

Tony Docherty wrote:Welcome to the ranch.

I'm trying to create a program where people can put a value into a JTextField1 & JTextField2, and the answer will automatically appear into JTextField3 without having to press Enter or any other function.
When changing the value of JTextField1 or JTextField2 it will update realtime into JTextField3.

What kind of tool do I have to use to do this?

I'm not sure what you mean by 'tools'. I assume you already have a jdk and some form of text editor so do you mean how do you write the program?

When I have more questions, should I make a new thread or just put them here?

If they are a continuation of this topic then please put them here else start a new thread.


Thanks for a quick reply.
I have jdk and a text editor, I was thinking more of how to program this, my guess is ActionListener/Event however can't figure out what "tools/code" to use.
Already know how to make an JTextField, getSting from JTextField and make Sting into Integer and make it display in JTextField3, however I currently have do an action for it to update, I want it to update real time.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2405
    
  50
If you want to respond to every key press then get the underlying Document from the JTextField using getDocument() and then add a DocumentListener to it. Your document listener will be notified when the document changes.
Christoffer Nilsen
Greenhorn

Joined: Apr 08, 2013
Posts: 9

Tony Docherty wrote:If you want to respond to every key press then get the underlying Document from the JTextField using getDocument() and then add a DocumentListener to it. Your document listener will be notified when the document changes.

Thanks again for a very quick answer, I will look into it, and it sounds like the right thing to use.
Again, thanks!
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2405
    
  50
My pleasure and good luck.
Christoffer Nilsen
Greenhorn

Joined: Apr 08, 2013
Posts: 9



I tried to play around with the DocumentListener code but I get error msgs.

Simply tried to make a JFrame /w a JTextField, and when someone removed/inserted stuff into the JTextField
I would get a message in Eclipse so I could see that it was working as expected, however it does not.

I added the error msgs to the code in the comment lines.
Anyone that can see my mistakes?

Also, could someone explain to me what the "e" in public void xxxxxxUpdate(DocumentEvent e) does? is it something I name it?

- Nilsen
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2405
    
  50
Why are you setting a layout manager on a JTextField, did you mean to do it on mainframe?

That's because you have no variable called f. You need to use 'this' as your class implements DocumentListener.
Note: I wouldn't recommend implementing listeners like this, it's generally a much better idea to have an anonymous listener class or a separate listener class.

Also, could someone explain to me what the "e" in public void xxxxxxUpdate(DocumentEvent e) does? is it something I name it?

It is the name of the method parameter that will contain the reference to the DocumentEvent. You can name it whatever you want.
Christoffer Nilsen
Greenhorn

Joined: Apr 08, 2013
Posts: 9

Tony Docherty wrote: Why are you setting a layout manager on a JTextField, did you mean to do it on mainframe?

That's because you have no variable called f. You need to use 'this' as your class implements DocumentListener.
Note: I wouldn't recommend implementing listeners like this, it's generally a much better idea to have an anonymous listener class or a separate listener class.

Also, could someone explain to me what the "e" in public void xxxxxxUpdate(DocumentEvent e) does? is it something I name it?

It is the name of the method parameter that will contain the reference to the DocumentEvent. You can name it whatever you want.

Thanks for answering. I ment to set the FlowLayout on the whole method, setLayout(new FlowLayout()); dunno why I did it that way, but I have changed it.

I will try out what you suggest. This is also not my main program/file. I always make an separate file to try out different stuff I don't know how to do.
Currently the class are separated on my main file.


Again thanks for your comment!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Realtime update when changing value in JTextField