Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4849868

JTextArea.getSelectionEnd does not work correctly

XMLWordPrintable

    • tiger
    • x86
    • windows_2000

      In our swing applicaton, there is a TextArea with a vertical scroll bar. When you type some text in a TextArea and make a selection of part of the text. Don't release the left mouse button, drag the mouse horizontally to the right until the mouse button reaches the upper arrow of the vertical scroll bar. Then release the mouse button, invoke the getSelectionEnd method of the JTextArea object, the value it returns is greater than the text length.

      The code is like below:

      import java.awt.*;
      import java.awt.event.*;
      import javax.swing.*;
      public class TestTextArea extends JDialog implements ActionListener
      {
          private JTextArea textArea;
          private JLabel label;
          /** Creates a new instance of TestTextArea */
          public TestTextArea()
          {
              setTitle("Test Dialog");
              textArea = new JTextArea("1234");
              textArea.setLineWrap(true);
              JScrollPane pane = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
              JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
              Container p = getContentPane();
              p.add(pane);
              JButton b = new JButton("Get Selection Status");
              b.addActionListener(this);
              p.add(b, BorderLayout.SOUTH);
              label = new JLabel("Ready");
              p.add(label, BorderLayout.NORTH);
              setBounds(150, 150, 400, 300);
          }
          protected void processWindowEvent(WindowEvent e)
          {
              if (e.getID() == e.WINDOW_CLOSING)
              {
                  System.exit(0);
              }
              super.processWindowEvent(e);
          }
          /** Invoked when an action occurs.
           */
          public void actionPerformed(ActionEvent e)
          {
              int start = textArea.getSelectionStart();
              int end = textArea.getSelectionEnd();
              int length = textArea.getDocument().getLength();
              label.setText("Selection Start:"+start+", end"+end+", Document length:"+length);
          }
          public static void main(String[] args)
          {
              new TestTextArea().show();
          }
      }

            idk Igor Kushnirskiy (Inactive)
            duke J. Duke
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: