After calling JTextField().setText(""), the text refuses to accept any keyboard input. The attached test case demonstrates the problem. If I set the non empty field content, it is possible to enter text inside the non-empty area (the characters being right from the cursor shifts correctly to the right). However if I click the mouse right from the last character in the field, no input is possible. It may be logical that the empty string results no initial text area - and the input is not possible. This is potentially severe bug, as adding input into an initially empty area is between the main functions of this component. The bug blocks the new set of the RMI/IIOP examples. Reported agains Classpath 0.18+CVS as it was on November 3, 2005, under JamVm 1.3.3 and Fedora Linux.
Created attachment 10117 [details] The test case.
The input seeks blocked when the cursor is right to the last character in the content of the text field.
The "should never happen" exception is thrown and swallowed inside DefaultEditorKit.DefaultTypedAction.actionPerformed: javax.swing.text.BadLocationException: the where argument cannot be greater than the content length at javax.swing.text.GapContent.insertString (GapContent.java:314) at javax.swing.text.AbstractDocument.insertString (AbstractDocument.java:543) at javax.swing.text.PlainDocument.insertString (PlainDocument.java:172) at javax.swing.text.DefaultEditorKit$DefaultKeyTypedAction.actionPerformed (DefaultEditorKit.java:223) at javax.swing.SwingUtilities.notifyAction (SwingUtilities.java:1102) at javax.swing.JComponent.processKeyBinding (JComponent.java:2214) at javax.swing.JComponent.processKeyEvent (JComponent.java:2155) at java.awt.Component.processEvent (Component.java:2978) at java.awt.Container.processEvent (Container.java:908) at java.awt.Component.dispatchEventImpl (Component.java:4846) at java.awt.Container.dispatchEventImpl (Container.java:1647) at java.awt.Component.dispatchEvent (Component.java:2342) at java.awt.KeyboardFocusManager.redispatchEvent (KeyboardFocusManager.java:1111) at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent (DefaultKeyboardFocusManager.java:280) at java.awt.DefaultKeyboardFocusManager.dispatchEvent (DefaultKeyboardFocusManager.java:253) at java.awt.EventDispatchThread.run (EventDispatchThread.java:74)
Created attachment 10124 [details] Proposed patch to fix this bug (under discussion). Our JTextField contains an "imaginary" end of line character (0xA), but the new text must be always inserted before it, not after. When clicking right from the last character in the field, the PlainTextView.viewToModel should return the position before the end of line character and not after the end of line character. Otherwise the inputs blocks. If the empty field receives focus by the mouse click, the caret position is always set after the 0xA and, a result, it is never possible to enter any text in the field. The bug http://gcc.gnu.org/bugzilla/show_bug.cgi?id=24650 has the uploaded test case, demonstrating this problem. This path fixes the problem by introducing the following rule: if the last character under the mouse click is 0xA, it should not be taken into consideration, the pre-last character must be returned instead (the 0xA is a dimensionless character anyway). With this patch, I "revived" the text fields in my application. In the swing activity board, now it is possible to enter the text after clicking the mouse RIGHT from the "Hello world" in the text field demo. The work of the rest of Swing seems not broken, despite it would be nice if somebody else could check this as well. 2005-11-03 Audrius Meskauskas <AudriusA@Bioinformatics.org> PR swing/24650 * javax/swing/text/PlainView.java (viewToModel)): The end of line symbol (0xA), if being the last member in the obtained text, should not be counted.
Subject: Bug 24650 CVSROOT: /cvsroot/classpath Module name: classpath Branch: Changes by: Audrius Mekauskas <audriusa@savannah.gnu.org> 05/11/03 19:29:00 Modified files: . : ChangeLog javax/swing/text: PlainView.java Log message: 2005-11-03 Audrius Meskauskas <AudriusA@Bioinformatics.org> PR swing/24650 * javax/swing/text/PlainView.java (viewToModel)): The end of line symbol (0xA), if being the last member in the obtained text, should not be counted. CVSWeb URLs: http://savannah.gnu.org/cgi-bin/viewcvs/classpath/classpath/ChangeLog.diff?tr1=1.5450&tr2=1.5451&r1=text&r2=text http://savannah.gnu.org/cgi-bin/viewcvs/classpath/classpath/javax/swing/text/PlainView.java.diff?tr1=1.30&tr2=1.31&r1=text&r2=text
Created attachment 10140 [details] Simplified patch It looks like the fix was practically correct. Roman simplified the patch a little, in PlainView we can assume that every line ends in a newline (that is possible the whole point of this one implicit newline) and exclude that from the calculation in viewToModel.
Fixed by subsequently applying the two attached patches.