如何在 Java 中使文本区域的特定行可见

发布于 2024-11-02 00:18:53 字数 151 浏览 1 评论 0原文

我的 JTextArea 包含数千行,但并非所有行都一次可见。我想以编程方式滚动到 textArea 的特定行,以便该行可见。我发现scrollPane 有一个方法scrollRectToVisible 但我没有成功。谁能建议我如何实现目标。一个可行的代码片段对我来说真的很有帮助。谢谢。

My JTextArea contains thousands of lines 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

海未深 2024-11-09 00:18:53

scrollRectToVisible(...) 应该可以工作。确保在文本区域而不是滚动窗格上调用scrollRectToVisible(...)。如果这不起作用,那么我猜你没有得到正确的矩形来滚动。发布您的 SSCCE 来演示该问题。

另一种方法是使用 文本实用程序< 的 gotoStartOfLine(...) 方法/a>.如果您愿意,您还可以使用 centerLineInScrollPane(...) 方法。

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.

一笔一画续写前缘 2024-11-09 00:18:53

我想你已经回答了这个问题。我在这段时间创建了我的 SSCCE,所以我将其发布是为了其他人的利益(如果不是为了您)。

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();
         }
      });
   }
}

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();
         }
      });
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文