简体   繁体   English

如何在JTextArea中的行之间切换

[英]How to switch between lines in JTextArea in swing

Am trying to set the goto line number for switching between lines in my swing application. 我正在尝试设置goto行号以在我的swing应用程序中的行之间切换。 I got the number of lines in the JTextArea instance using getLineCount() but don't know how to switch between lines? 我使用getLineCount()获得了JTextArea实例中的行数,但不知道如何getLineCount()之间切换?

Could any one suggest me? 有人可以建议我吗?

You can set the caret position with textArea.setCaretPosition . 您可以使用textArea.setCaretPosition设置插入符位置。 Something like this would work 这样的事情会起作用

textArea.setCaretPosition(textArea.getDocument().getDefaultRootElement()
                         .getElement(index).getStartOffset());

In the example below, I just use a JComboBox that I populate with the indexes using the line numbers of the JTextArea . 在下面的示例中,我只使用一个JComboBox ,并使用JTextArea的行号填充索引。 When you select on an number in combo box, the caret will move to that line of the JTextArea . 当您在组合框中选择一个数字时,插入符号将移动到JTextArea那一行。

The program is not a great program though. 该程序虽然不是一个很好的程序。 You would probably want to populate the ComboBoxModel dynamically with the addition and removal of lines. 您可能希望通过添加和删除行来动态填充ComboBoxModel But this should give you the answer you're looking for. 但这应该为您提供所需的答案。

Disclaimer 免责声明

I haven't added any functionality to bring the scrollpane focus the the current line. 我没有添加任何功能来使滚动窗格聚焦于当前行。 You may want to look at @Balder comment below for help with that. 您可能需要查看下面的@Balder评论,以寻求帮助。

在此处输入图片说明

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class TestCaret {

    public TestCaret() {
        JTextArea textArea = createTextArea();
        JComboBox cBox = createComboBox(textArea);

        JFrame frame = new JFrame();
        frame.add(new JScrollPane(textArea));
        frame.add(cBox, BorderLayout.SOUTH);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private JComboBox createComboBox(final JTextArea textArea) {
        DefaultComboBoxModel<Integer> model  = new DefaultComboBoxModel<>();
        int lines = textArea.getLineCount();
        for (int i = 0; i < lines; i++) {
            model.addElement(i);
        }
        final JComboBox cBox = new JComboBox(model);
        cBox.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                int index = (Integer)cBox.getSelectedItem();
                textArea.setCaretPosition(
                        textArea.getDocument().getDefaultRootElement().getElement(index).getStartOffset());
                textArea.requestFocusInWindow();
            }
        });
        return cBox;
    }

    private JTextArea createTextArea() {
        JTextArea textArea = new JTextArea(10, 50);
        textArea.setMargin(new Insets(15, 15, 15, 15));
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);
        String text 
                = "0 Hello World\n" + 
                "1 Hello World\n" + 
                "2 Hello World\n" + 
                "3 Hello World\n" + 
                "4 Hello World\n" + 
                "5 Hello World\n" + 
                "6 Hello World\n" +
                "7 Hello World\n" + 
                "8 Hello World\n" + 
                "9 Hello World\n"; 
        textArea.setText(text);
        return textArea;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                new TestCaret();
            }
        });
    }
}

One way would be to set up a while statement in the following manner: 一种方法是通过以下方式建立while语句:

String line = null;
while ((line = text.readLine())!=null && line.getLineNumber() < desiredNumber) {}

At the end, line will refer to the line with the desired line number. 最后,行将引用具有所需行号的行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM