Help - Search - Members - Calendar
Full Version: Java App Freezes When Using Textfield
OESF Forums > Distros, Development, and Model Specific Forums > Everything Development > Personal Java/Jeode/J2ME Personal Profile
Long story short, I wrote a Java app thet works fine on my desktop, and works fine on my Zaurus 3000 until it hits KeyAdapter code for a TextField.

Specifically it freezes when I try to access the TextField in the adapter.

I know that it's not the TextFiled nor the adapter per se but the combination of accessing the TextField from inside it's adapter. I can use the TextField other palces in the code fine, and if I remove the use of the TextField from the adapter code everything works fine.

It's only my adapter tries to use the TextField my app freezes. The relevant code looks like this:

answerTF.addKeyListener(new KeyPressed());

private class KeyPressed extends KeyAdapter {
public void keyPressed(java.awt.event.KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
/* the line below causes app to freeze */
System.out.println("Entry is: " + answerTF.getText());

Any ideas?
See Final Solution below
Some answers to your questions:

I'm sure it's the access to the TextField that is the problem. Sysout works fine.

Any of these fail:


I'll try what you suggested and let you know the result.

I hadn't though of making the adapter public. I did try to make it a an anonymous class, i.e. passing it in as an argument to the KeyListener but that didn't help.

Question: why would it makea difference. I.e. why does the app work fine on my desktop but not on the Zaurus?
See Final Solution below
Thanks for the offer. I don't have the source with me rightnow but I'll send it to you on Monday.

As for all JVM's not being the same that's true but they should all respect the Java specs wink.gif
FYI here is the exact code that declares the KeyListener:


private TextField getAnswerTF() {
if (answerTF == null) {
answerTF = new TextField();
answerTF.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
return answerTF;

And here is the code that fails:


public void checkAnswer() {
String correctAnswer, answer;
/* code fails at the line below */
answer = k.getAnswer();
boolean correct = false;
if (answer.equals("")) return;
if (testType.equals("english")) {
correctAnswer = currentEntry.getEnglish();
correct = correctAnswer.indexOf(answer) != -1;
else {
correctAnswer = currentEntry.getKana();
correct = correctAnswer.equals(answer);


public String getAnswer() {
System.out.println("getAnswer() start");
/* code freezes here */
String ret = answerTF.getText();
System.out.println("Answer is " + ret + " getAnswer() end");
return ret;
Quick update, I made the KeyListener an external public class and that didn't solve anything. Omicron I'll be PM'ing you and asking you to try out my code sad.gif
Final Solution

Here is a version of the code that seems to solve the oddity of the Z loking on enter in a KeyListener (working sample in both PC and Z)....

import java.awt.event.*;
import java.awt.*;
import java.util.Date;

public class K {

public TextField textField = null;
public Frame k;
public boolean shutdown = false;

public class keyListener extends KeyAdapter {
public void keyPressed(KeyEvent key) {
if (key.isActionKey() == false && key.getKeyCode() != KeyEvent.VK_ENTER) {

if (key.getKeyChar() == 'q' || key.getKeyChar() == 'Q') {
shutdown = true;
System.out.println("String So Far: " + textField.getText());
} else {
System.out.println("Key Listener Consumed ENTER");




public class actionListener implements ActionListener {
public void actionPerformed(ActionEvent action) {
textField.setText(textField.getText() + "|ENTER|");
System.out.println("String So Far: " + textField.getText());


public K() {
textField = new TextField("");
textField.setSize(150, 20);
textField.setLocation(20, 40);
k = new Frame();
k.setTitle("Type Q to Quit");
k.setSize(200, 100);
textField.addKeyListener(new keyListener());
textField.addActionListener(new actionListener());;

public static void main(String[] args) {
Date start = new Date();
K k_class = new K();
Date end = new Date();

System.out.println("Time to load: " + (end.getTime() - start.getTime()) + " ms");
while (k_class.shutdown == false) {
try {
catch (InterruptedException ex) {
System.out.println("HERE " + ex);
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2015 Invision Power Services, Inc.