Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JtextField----URGENT!!!!!!  RSS feed

 
Remi Dubey
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
The problem is related to the handling of the Textfield. I am implementing the KeyTyped method of the KeyListerner Interface. Now whenever a key is pressed and the KeyTyped method is fired. NOw extracting the contents of the TextField at that moment does not pass the updated TextField contents rather the old values which creates lot of problems in subsequent handling of the code.
I am listing down my KeyTyped method so that the problem becomes clear.
Here Inodestf is a JTextField
Problem is the contents of String s which gets the old value of the TextField after the Key is pressed. Suggest a solution to get the updated value in the String s after the KeyListerner event is fired.
public void keyTyped(KeyEvent ke)
{
try
{
if ( (ke.getSource() == Inodestf) )
{ char c=ke.getKeyChar();
String s = Inodestf.getText().trim(); //Contents of TextField not updated
if (Character.isISOControl(c)) // for non printable chars...
{
lv.numinputneurons = Integer.parseInt(s);
}else // for printable chars...
{
lv.numinputneurons = Integer.parseInt(s+c);
currInodes = lv.numinputneurons;
}
}
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Remi,
I think you might be better to look into DocumentListeners when using JTextFields.

The java.awt.TextField could be monitored for changes by adding a TextListener for TextEvent's. In the JTextComponent based
components, changes are broadcasted from the model via a DocumentEvent to DocumentListeners. The DocumentEvent gives the location of
the change and the kind of change if desired. The code fragment might look something like:
DocumentListener myListener = ??;
JTextField myArea = ??;
myArea.getDocument().addDocumentListener(myListener);

Regards,
Manfred.
 
ashok_dara
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I saw the code.
Instead of String use StringBuffer .
If u'r problem is not solved contact me at ashok_dara@rediffmail.com
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!