• Post Reply Bookmark Topic Watch Topic
  • New Topic

Edit JTextfield, get text with getText

 
Per Olof Petersson
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to create a dialog there I can write in to JTextFields, get the text with getText and send as initial varibales in an instance of class tGlose. The problem is that nothing happens then I hit the enter-key.

I have tried to use "setEidtable(true)" with the JTextField norskfelt. It souldn't be necesarry, and i doesn't make at difference. I am stuck on this så I am really greatful for help.

 
Rob Spoor
Sheriff
Posts: 20817
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You never change the values of those variables any instance of tGlose. You set Leggtilord's own variables instead.

Leggtilord needs a reference to an instance of tGlose, then set that instance's variables.
 
Per Olof Petersson
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:You never change the values of those variables any instance of tGlose. You set Leggtilord's own variables instead.

Thats true, thats the problem.
Leggtilord needs a reference to an instance of tGlose, then set that instance's variables.

I do not understand that.
1. Then I type something in the textfields and then 'enter' I should have changed the varibales norskord and engelskord (class Lytternorsk, Lytterengelsk).
2. And then I click the button "legg til" a new instanse of tGlose would be constructed with norskord and engelskord as variables and with the new values..

Am I missing something obvious here?
 
Rob Spoor
Sheriff
Posts: 20817
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You only ever set the values of these fields on line 30, when you create the tGlose instance with the current values of these fields - null, because they haven't been initialized yet.
 
Per Olof Petersson
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:You only ever set the values of these fields on line 30, when you create the tGlose instance with the current values of these fields - null, because they haven't been initialized yet.

That's right, but that's just because I don't manage to change the values with the JTextfields. If I just could make that work I think the rest would work to.
 
Rob Spoor
Sheriff
Posts: 20817
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will need to make sure the tGlose instance can access the text fields. A quick&dirty attempt:
 
Per Olof Petersson
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have tried it, but it is just the same.
I think the tGlose instance do access the text fields. If I give norskord an initial value by inserting the line norskord = norskfelt.getText() the test sentence "sett inn norsk ord her!" is printed to the screen as expected. The problem is that I should be able to change the value of norskord with the method defines in Lytternorsk, but this doesn't work because the text in the JTextfield doesn't seems to be editable.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> I have tried it, but it is just the same.

worked OK for me.
 
Per Olof Petersson
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... and you where able to change the text in the textfields and thereby change the value of norskord?

This is that really frustrates me, nothing happens then I hit the enter key. Regardless of which code I use, yours or mine. I have tried the code on this site and it works fine on my system and I can se no reason why the JTextField should work with that code but not with mine....

I have read in a book that I need use keyevent, but in the example above they did not use keyevnt and it worked fine anyway...
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was just clicking the button, which worked OK.

enter does work from the textfields, changing the variables, but that's all that happens

you can either
1)
add some println's to the actionListener


2) you can remove the actionListeners from the textfields
//engelskfelt.addActionListener(new Lytterengelsk());
//norskfelt.addActionListener(new Lytternorsk());
and add this line to Leggtilord's constructor

getRootPane().setDefaultButton(addGlose);
 
Per Olof Petersson
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you say make no sense, but it works so it is fine with me!

I must say I am really, really impressed with you pedagogic ability and your patience! And I am so grateful for you helping me with this, otherwise I might have stabbed somebody!

Now I think I might sleep well tonight!

Good night!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!