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

JTextComponent.modelToView(int offset) retruns null with a valid offset

XMLWordPrintable

    • generic
    • solaris_2.5.1

      JTextCompoent.modelToView(int pos) where pos is offset in Document Model
      returns null instead of a rectangle. thus cannot use JViewPort.scrollRectToVisible(rectangle) to scroll to view port to position
      pos.


      model to view retruns null in such a case shown
      by the sample code. The sample code extends JTextArea
      and sets the Doc Model to Plain Document. a file is open
      and read into the JTextArea via the OpenFile method
      call. The call is made to mdoelToView(int pos)
      where pos is the begining offset of line 90. The
      call returns null every time, thogh the pos is a
      valid offset within the document. Consequently the call to
      JViewPort.ScrollRectToVisible(rectangle) fails.

      1. Compile with JDK115 on solaris 2.5 Sparc
      with Swing 1.0.2 and watch the following trace info.

      doc length: 3554
      modelToView returns null
      are we focusing yet? true
      caret is at: 3014

      2. Since modelToView call returns Null i than
      attempt to set the Cursor to the begining of line
      90. This works on solaris sparc but completely fails on
      solaris x86. Since the cursor is now set to a
      position outside the view port on as soon as I start
      entering keys the view port jumps to the show
      the cursor position, but the view port should have moved
      before i started typing. As mentioned in
      ...swing.text.DefaultCaret.java which implements the
      setCaretPosition call and claims that it makes sure
      the caret is within the visible region of the view.

      Here is the code.

      import java.awt.*;
      import java.beans.*;
      import java.net.*;
      import java.awt.event.*;
      import java.io.*;
      import java.util.*;
      import com.sun.java.swing.*;
      import com.sun.java.swing.text.*;
      import com.sun.java.swing.plaf.*;
      import com.sun.java.swing.event.*;

      public class ScrollRectTest extends JTextArea {

      ScrollRectTest () {
      setDocument(new PlainDocument());

           }

      public void openFile(String fileName) {

              File f = new File(fileName);

      try {
      Reader in = new FileReader(f);
              read(in, f);
              } catch (IOException e) {
                      System.err.println(e.toString());
                }
        }

         public static void main(String[] args) {

      int offset = 0;
      Rectangle rect = new Rectangle();

             ScrollRectTest editor = new ScrollRectTest();
             JScrollPane scroller = new JScrollPane();

             JViewport vp = scroller.getViewport();
             vp.add(editor);
      vp.setBackingStoreEnabled(true);

              JPanel panel = new JPanel();
              panel.setLayout(new BorderLayout());
              panel.add("Center", scroller);


             JFrame f = new JFrame("JavaEditorKit: ");
             f.getContentPane().setLayout(new BorderLayout());
             f.getContentPane().add("Center", panel);
             f.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {System.exit(0);}
      });

             f.pack();
             f.setSize(600, 600);
             f.setVisible(true);

             editor.setBackground(Color.white);
             editor.setFont(new Font("Courier", 0, 12));
             editor.openFile("NOTES.txt");
             editor.setEditable(true);

      Document doc=editor.getDocument();
      System.out.println("doc length: "+doc.getLength());


      /* get the start offset at line 90 */
      try {
      offset = editor.getLineStartOffset(90);
      } catch (BadLocationException ex) {
                      ex.printStackTrace();
      }

      try {
      rect = editor.modelToView(offset);
      } catch (BadLocationException ex) {
      ex.printStackTrace();
      }

      System.out.println("modelToView returns " + rect);

      if ( rect != null)
      {
      vp.scrollRectToVisible(rect);
      System.out.println("rect is: "+rect.x +" and " + rect.y);
      }
      else
      {
      //Ok. set the caret postion to see if we scroll
      editor.setRequestFocusEnabled(true);
      editor.requestFocus();
      System.out.println("are we focusing yet? "+editor.isRequestFocusEnabled());
      editor.setCaretPosition(offset);
      System.out.println("caret is at: "+editor.getCaretPosition());
      }
         }
      }

            tprinzing Tim Prinzing (Inactive)
            duke J. Duke
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: