开发者

How to make a specific row visible of a textArea in Java

My JTextArea contains thousands of lin开发者_StackOverflow中文版es but not all of them are visible at a time. I want to programmatically scroll to a specific row of the textArea so that the line is visible. I found that scrollPane has a method scrollRectToVisible but I am not successful with that. Can anyone suggest me how to accomplish the goal. A workable code snippet will be really helpful for me. Thanks.


scrollRectToVisible(...) should work. Make sure you invoke scrollRectToVisible(...) on the text area and not the scrollpane. If that doesn't work then I would guess you are not getting the proper Rectangle to scroll to. Post your SSCCE that demonstrates the problem.

Another approach is to use the gotoStartOfLine(...) method of the Text Utilities. You can also use the centerLineInScrollPane(...) method if you wish.


I guess you've answered this already. I was creating my SSCCE during this time, so I'll post it for others' benefit if not yours.

import java.awt.BorderLayout;
import java.awt.Rectangle;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.text.BadLocationException;

public class TestScrollRectToVisible extends JPanel {
   private static final int MAX_LOOP = 10000;
   private DefaultListModel listModel = new DefaultListModel();
   private JTextArea textarea = new JTextArea(20, 30);
   private JList jList = new JList(listModel);
   JScrollPane textareaScrollPane = new JScrollPane(textarea);


   public TestScrollRectToVisible() {
      jList.addListSelectionListener(new ListSelectionListener() {
         public void valueChanged(ListSelectionEvent e) {
            if (!e.getValueIsAdjusting()) {
               String text = jList.getSelectedValue().toString();
               text += ": ";

               String docText = textarea.getText();
               int index = docText.indexOf(text);
               if (index < 0) {
                  return;
               }
               try {
                  Rectangle rect = textarea.modelToView(index);
                  textarea.scrollRectToVisible(rect);
               } catch (BadLocationException e1) {
                  e1.printStackTrace();
               }
            }
         }
      });

      jList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
      StringBuilder strBuilder = new StringBuilder();
      for (int i = 0; i < MAX_LOOP; i++) {
         String text = String.valueOf(i);
         listModel.addElement(text);
         strBuilder.append(text + ": abcdefghijklmnopqrstuvwxyz" + "\n");
      }
      textarea.setText(strBuilder.toString());

      setLayout(new BorderLayout());
      add(textareaScrollPane, BorderLayout.CENTER);
      add(new JScrollPane(jList), BorderLayout.EAST);
   }

   private static void createAndShowUI() {
      JFrame frame = new JFrame("TestScrollRectToVisible");
      frame.getContentPane().add(new TestScrollRectToVisible());
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      java.awt.EventQueue.invokeLater(new Runnable() {
         public void run() {
            createAndShowUI();
         }
      });
   }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜