Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Positioning/scrolling a JTextArea in a JScrollPane

JörgNov 3 2007 — edited Nov 4 2007
Hello,

setting the caretPosition in a JTextArea makes the corresponding line appear
at the bottom of the viewport. I would like to see this line in the middle of the
viewport to show preceding and following context. In the following examples
there are 20 lines in the textarea, and I would like to display line 11 in the
middle.
My first attempt was to use scrollRectToVisible(...), but nothing happens at all.
Where is the error?
import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;

public class TextAreaScroll extends JFrame {
  JScrollPane sp;
  JTextArea ta;
 
  public TextAreaScroll() {
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    setSize(300, 200);
    String s=
       "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20\n";
    ta= new JTextArea(s);
    System.out.println(ta.getLineCount()+" "+ta.getRows());
    ta.setCaretPosition(s.indexOf("11"));
    sp= new JScrollPane(ta);
    int iFontSize= ta.getFont().getSize();
    try {
      int i= ta.getLineOfOffset(ta.getCaretPosition()); // last line of viewport
      System.out.println(i+" "+i*iFontSize);
      sp.scrollRectToVisible(new Rectangle(0, (i+5)*iFontSize, 100,20));
    }
    catch (BadLocationException e) {
      System.out.println(e);
    }
    add(sp);
    setVisible(true);
  }


  public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
      public void run() {
	 new TextAreaScroll();
      }
    });
  }
}
In my second attempt I modified the scrollbar values, and indeed scrolling
happened. But I don't know how to calculate the new scrollBar value. (80 was
found by trial and error.) Any ideas?
import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;

public class TextAreaScroll2 extends JFrame {
  JScrollPane sp;
  JTextArea ta;
 
  public TextAreaScroll2() {
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    setSize(300, 200);
    String s=
       "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20\n";
    ta= new JTextArea(s);
    ta.setCaretPosition(s.indexOf("13"));
    sp= new JScrollPane(ta);
    JScrollBar bar= sp.getVerticalScrollBar();
    System.out.println(bar.getMinimum()+" "+bar.getValue()+" "+bar.getMaximum());
    System.out.println(bar.getModel().getExtent()+" "+bar.getUnitIncrement()+" "+
				bar.getBlockIncrement());
    bar.setValue(80);
    add(sp);
    setVisible(true);
    System.out.println(bar.getMinimum()+" "+bar.getValue()+" "+bar.getMaximum());
    System.out.println(bar.getModel().getExtent()+" "+bar.getUnitIncrement()+" "+
				bar.getBlockIncrement());
  }


  public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
      public void run() {
	 new TextAreaScroll2();
      }
    });
  }
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 2 2007
Added on Nov 3 2007
5 comments
236 views