![]() ![]() |
Apr 7 2005, 06:39 PM
Post
#1
|
|
|
Group: Members Posts: 60 Joined: 12-February 05 Member No.: 6,461 |
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? |
|
|
|
Apr 7 2005, 08:49 PM
Post
#2
|
|
![]() Group: Members Posts: 608 Joined: 14-April 04 Member No.: 2,853 |
See Final Solution below
|
|
|
|
Apr 7 2005, 10:29 PM
Post
#3
|
|
|
Group: Members Posts: 60 Joined: 12-February 05 Member No.: 6,461 |
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? |
|
|
|
Apr 8 2005, 05:48 AM
Post
#4
|
|
![]() Group: Members Posts: 608 Joined: 14-April 04 Member No.: 2,853 |
See Final Solution below
|
|
|
|
Apr 9 2005, 06:42 PM
Post
#5
|
|
|
Group: Members Posts: 60 Joined: 12-February 05 Member No.: 6,461 |
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 |
|
|
|
Apr 10 2005, 10:20 PM
Post
#6
|
|
|
Group: Members Posts: 60 Joined: 12-February 05 Member No.: 6,461 |
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; } |
|
|
|
Apr 10 2005, 10:45 PM
Post
#7
|
|
|
Group: Members Posts: 60 Joined: 12-February 05 Member No.: 6,461 |
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
|
|
|
|
Apr 15 2005, 07:03 AM
Post
#8
|
|
![]() Group: Members Posts: 608 Joined: 14-April 04 Member No.: 2,853 |
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 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); } } |
|
|
|
![]() ![]() |
|
Lo-Fi Version | Time is now: 20th May 2013 - 03:30 AM |