-
Bug
-
Resolution: Duplicate
-
P2
-
None
-
1.1, 1.1.1, 1.1.3, 1.1.4, 1.1.5
-
None
-
x86, sparc
-
solaris_2.6, windows_95, windows_nt
Name: mc57594 Date: 01/29/97
1. Problem with TextArea. The caret seems to count newline as two
characters but the string ( from textArea.getText() ) counts it as one. I
got the following output from a test
Start Condition textArea is empty.
press a (printed from public void keyPressed(KeyEvent))
m_textArea.getCaretPosition() = 0 m_textArea.getText().length() = 0
press b
m_textArea.getCaretPosition() = 1 m_textArea.getText().length() = 1
press enter
m_textArea.getCaretPosition() = 2 m_textArea.getText().length() = 2
press enter
m_textArea.getCaretPosition() = 4 m_textArea.getText().length() = 3
press enter
m_textArea.getCaretPosition() = 6 m_textArea.getText().length() = 4
press a
m_textArea.getCaretPosition() = 8 m_textArea.getText().length() = 5
press a
m_textArea.getCaretPosition() = 9 m_textArea.getText().length() = 6
This is not at all the behaviour I would expect. The TextArea seems to
count a new line as 2 but the string counts it as one. This makes mapping
between regions of the string and selection zones on the text area much
more complicated. I can see no value in this behaviour so I assumed it
was a bug
Here is the program
import java.awt.*;
import java.awt.event.*;
public final class mymain
{
public static void main(String[] p_args){
showWindow();
}
private static void showWindow(){
Frame frame = new myFrame("This is my Frame");
frame.pack(); frame.show();
}
};
class myFrame extends Frame implements KeyListener{
private TextArea m_textArea;
public myFrame(String p_title){
super(p_title);
add(m_textArea = new TextArea());
m_textArea.addKeyListener(this);
}
public void keyPressed(KeyEvent p_event){}
public void keyReleased(KeyEvent p_event){}
public void keyTyped(KeyEvent p_event){
System.out.println("Caret(" + m_textArea.getCaretPosition() +
") bufferSize(" + m_textArea.getText().length() + ")" );
}
};
======================================================================
daniel.indrigo@Canada 1997-08-22 Here is another example
// code to generate the bug.
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.awt.datatransfer.*;
class TextBug extends Frame implements ActionListener
{
TextArea textArea = new TextArea();
Button copyButton = new Button("Copy");
Button pasteButton = new Button("Paste");
Clipboard clipboard = null;
TextBug( ) {
super("Reproduce Selection Bug");
Panel bottomButtonPanel = new Panel();
// set font for individual controls
Font f = new Font("TimesRoman", Font.BOLD, 12);
copyButton.setFont(f);
pasteButton.setFont(f);
copyButton.addActionListener(this);
pasteButton.addActionListener(this);
bottomButtonPanel.setLayout(new GridLayout(1,0, 5, 5));
bottomButtonPanel.add(copyButton);
bottomButtonPanel.add(pasteButton);
setLayout ( new BorderLayout () );
add("North", textArea);
add("South", bottomButtonPanel);
pack();
setSize(800, 450);
enableEvents(AWTEvent.WINDOW_EVENT_MASK |
AWTEvent.ACTION_EVENT_MASK |
AWTEvent.ITEM_EVENT_MASK);
show();
}
// Implement ActionListener
public void actionPerformed(ActionEvent e)
{
if ( e.getActionCommand().equals("Copy") )
{
if (clipboard == null)
clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
// it seems a jdk bug, getSelelectedText() dosen't work.
// workaround: count '\n', then set the selection with this offset
String text = textArea.getSelectedText();
StringSelection data = new StringSelection ( text );
clipboard.setContents( data, data );
}
if ( e.getActionCommand().equals("Paste") )
{
Transferable clipData = clipboard.getContents(this);
String pasteString;
try
{
pasteString = ( String ) (clipData.getTransferData ( DataFlavor.stringFlavor ) ) + "\n";
}
catch ( Exception ex )
{
pasteString = ex.toString() + "\n";
}
int start = textArea.getCaretPosition();
textArea.insert( pasteString, start );
}
}
public static void main ( String argv[] )
{
TextBug bug = new TextBug();
}
}
---------------cut off here-----------------------
When select the last line of a multiple line text from
the text area and then click copy, a java.lang.StringIndexOutBoundsException is thrown
from String.subString().
company - CSG Systems , email - ###@###.###
=========================================
joon.oh@Eng 1997-11-13
1. TextArea with text input including RETURN
2. getSelectedText returns first line correctly.
3. if text in other lines are selected, it returns the text,
with an offset from +1 for every line.
EX:
1. line
2. line
3. line
4. line
select 3. line. getSelectedText returns:
line
4
CONFIG INFO:
code was developed with Visual Age for Java for Windows.
Test within VAJ does not show this problem.
Problem exists on JDK1.1.2 and JDK1.1.4 for Windows NT.
Problem does not appear under JDK1.1.1 and JDK1.1.4 for Solaris 2.5.1
==================================================
1111111111111111111111111111111111111
In keyTyped the caretpPosition is different when I use
different platforms.
Is it Windows NT the position is 1 less then
it is with Solaris.
222222222222222222222222222
import java.awt.* ;
import java.awt.event.* ;
public class KeyListenerTest extends Frame
implements KeyListener
{
TextField text;
public KeyListenerTest ()
{
super () ;
text = new TextField(100);
text.addKeyListener(this);
this.add(text);
}
public static void main (String args[])
{
KeyListenerTest fr = new KeyListenerTest () ;
fr.pack() ;
fr.setVisible(true);
}
public void keyTyped(KeyEvent e)
{
int carPos = text.getCaretPosition();
System.out.println("keyTyped carPos = " + carPos);
}
public void keyReleased(KeyEvent e)
{
int carPos = text.getCaretPosition();
System.out.println("keyReleased carPos = " + carPos);
}
public void keyPressed(KeyEvent e)
{
int carPos = text.getCaretPosition();
System.out.println("keyPressed carPos = " + carPos);
}
}
- duplicates
-
JDK-4072264 getSelectedText, getSelectedStart/End in TextArea class returns wrong
- Closed