Author Topic: Java App Freezes When Using Textfield  (Read 4367 times)

totsubo

  • Jr. Member
  • **
  • Posts: 60
    • View Profile
Java App Freezes When Using Textfield
« on: April 07, 2005, 10:39:44 pm »
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?

Omicron

  • Hero Member
  • *****
  • Posts: 608
    • View Profile
    • http://
Java App Freezes When Using Textfield
« Reply #1 on: April 08, 2005, 12:49:16 am »
[span style=\'font-size:14pt;line-height:100%\']See Final Solution below[/span]
« Last Edit: April 15, 2005, 11:07:49 am by Omicron »
"You Shall Not Pass"    
....Gandalf, Lord Of The Rings
--------------------------------------------------------------
C-860 (Cacko), 3x4gb MD  
DLINK 660W, 1GB SD,  
Upgraded Archos AV320 w/80GB HDD
Pocketop and Targus IR keyboards
Favorite Deal Site: SaveCity.net (pretty cool, good deals daily on one page)

totsubo

  • Jr. Member
  • **
  • Posts: 60
    • View Profile
Java App Freezes When Using Textfield
« Reply #2 on: April 08, 2005, 02:29:38 am »
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:

answerTF.getText();
System.out.println(answerTF);

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?

Omicron

  • Hero Member
  • *****
  • Posts: 608
    • View Profile
    • http://
Java App Freezes When Using Textfield
« Reply #3 on: April 08, 2005, 09:48:44 am »
[span style=\'font-size:14pt;line-height:100%\']See Final Solution below[/span]
« Last Edit: April 15, 2005, 11:08:11 am by Omicron »
"You Shall Not Pass"    
....Gandalf, Lord Of The Rings
--------------------------------------------------------------
C-860 (Cacko), 3x4gb MD  
DLINK 660W, 1GB SD,  
Upgraded Archos AV320 w/80GB HDD
Pocketop and Targus IR keyboards
Favorite Deal Site: SaveCity.net (pretty cool, good deals daily on one page)

totsubo

  • Jr. Member
  • **
  • Posts: 60
    • View Profile
Java App Freezes When Using Textfield
« Reply #4 on: April 09, 2005, 10:42:54 pm »
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

totsubo

  • Jr. Member
  • **
  • Posts: 60
    • View Profile
Java App Freezes When Using Textfield
« Reply #5 on: April 11, 2005, 02:20:31 am »
FYI here is the exact code that declares the KeyListener:

Frame:

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) {
         controller.checkAnswer();
       }     
     }
   });      
   return answerTF;
}

And here is the code that fails:

Controller:

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);
  }
  k.answer(correct);      
  k.setKana(getCurrentKana());
  k.setEnglish(getCurrentEnglish());
  currentEntry.updateStats(correct);
}

Frame:

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

totsubo

  • Jr. Member
  • **
  • Posts: 60
    • View Profile
Java App Freezes When Using Textfield
« Reply #6 on: April 11, 2005, 02:45:01 am »
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

Omicron

  • Hero Member
  • *****
  • Posts: 608
    • View Profile
    • http://
Java App Freezes When Using Textfield
« Reply #7 on: April 15, 2005, 11:03:37 am »
[span style=\'font-size:17pt;line-height:100%\']Final Solution[/span]

Here is a version of the code that seems to solve the oddity of the Z loking on enter in a KeyListener (working sample code...works 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 {
                key.consume();
                System.out.println("Key Listener Consumed ENTER");

            }

        }

    }

    public class actionListener implements ActionListener {
        public void actionPerformed(ActionEvent action) {
            textField.setText(textField.getText() + "|ENTER|");
            textField.setCaretPosition(textField.getText().length());
            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.setLayout(null);
        k.setSize(200, 100);
        k.setVisible(true);
        k.add(textField);
        textField.addKeyListener(new keyListener());
        textField.addActionListener(new actionListener());
        k.show();
        textField.requestFocus();
    }


    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 {
                Thread.sleep(100);
            }
            catch (InterruptedException ex) {
                System.out.println("HERE " + ex);
            }
        }
        k_class.k.dispose();
        System.exit(0);
    }
}
« Last Edit: April 15, 2005, 11:09:35 am by Omicron »
"You Shall Not Pass"    
....Gandalf, Lord Of The Rings
--------------------------------------------------------------
C-860 (Cacko), 3x4gb MD  
DLINK 660W, 1GB SD,  
Upgraded Archos AV320 w/80GB HDD
Pocketop and Targus IR keyboards
Favorite Deal Site: SaveCity.net (pretty cool, good deals daily on one page)