Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

help with understanding code  RSS feed

 
Candy Bortniker
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have some code in my program that I copied from my instructor's example. From what he said in class, it is suppose to move the cursor to the next textfield when you press the enter key. (At least that was my understanding.) That is not what is happening and since I don't completely understand the code I can't figure out what I need to fix. Can you help me understand what is going on and give a solution to fix the problem?
[ edited to format code to help preserve formatting using the [code] and [/code] UBB tags -ds ]
[ March 01, 2004: Message edited by: Dirk Schreckmann ]
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving this to the Swing / JFC / AWT forum...
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Candy this what I can see in the code:
myText is added an actionListener which if value is between minimum and maximum creates a LimitiedTextEvent. This event has as the object source of it to LimitedTextContainer. This event is notified to every LimitedTextListener previously added (registered) to listeners Vector. Each of such listeners would have its enterPressed method called with the mentioned event as argument.
Thus the example shows the construction of an event and its dispatching to the registered listeners. It is a home-made example of the Delegation Event Model.
When Enter is pressed the focus should move to the next UI widget, unless it is retained by other element; for instance by calling requestFocus.
 
Candy Bortniker
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

When Enter is pressed the focus should move to the next UI widget, unless it is retained by other element; for instance by calling requestFocus

The problem I'm having is that the focus isn't moved when enter is pressed. Listeners is empty. How do I put in the code to add something to listeners?
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
listeners is a Vector. You can add an Element via listeners.addElement(Object)
What happens when Enter is pressed? nothing? All is frozen? maybe another thread is holding the lock of the listeners object. That is, there is another thread executing a synchronized(listeners) block, at the time Enter is pressed and value is between minimum and maximum. That would cause the Event Dispatching-thread (the one executing the actionPerformed) to block waiting for the other thread to release the lock (ending its synchronized block)
Try finding out if the actionperformed is finished by placing a println as the last sentence in it.
A change of the focus could be vetoed by adding an implementation of VetoableChangeListener to DefaultKeyboardManager. Try searching for such clases in the program. Also search for any requestFocus or requestFocusInWidow calls.
A FocusTraversalPolicy object could be in charge of the sequence in which components get the focus. It could have been set to either a Container or DefaultKeyboardManager.
 
Candy Bortniker
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have added the statement: listeners.addElement(evt);and now I get a CastClassException at: ltl = (LimitedTextListener)(listCopy.elementAt(i));
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Candy look at the sentence ltl = (LimitedTextListener)(listCopy.elementAt(i));
This makes sure only classes implementing the interface LimitedtextListener are hold by listCopy. Otherwise there will be an Exception. After all, we are going to call later the method declared in that interface.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!