简体   繁体   English

JTextArea - 如何在指定偏移处设置文本?

[英]JTextArea - How to set text at a specified offset?

I want to set some text at a specified offset in my JTextArea .我想在我的JTextArea中的指定偏移处设置一些文本。 Let's say I have already in my edit "aaa bbb" and I want to overwrite "bbb" with "house", how can I do that in Java?假设我已经在我的编辑"aaa bbb"中,我想用“house”覆盖"bbb" ,我怎么能在 Java 中做到这一点?

You could use replaceRange()你可以使用replaceRange()

public void replaceRange(String str, int start, int end)

Replaces text from the indicated start to end position with the new text specified.用指定的新文本替换从指定开始到结束 position 的文本。 Does nothing if the model is null. Simply does a delete if the new string is null or empty.如果 model 为 null,则不执行任何操作。如果新字符串为 null 或为空,则只需执行删除操作。

This method is thread safe, although most Swing methods are not.此方法是线程安全的,尽管大多数 Swing 方法不是。 Please see Threads and Swing for more information.请参阅线程和 Swing 了解更多信息。

You need to take a look at three methods setSelectionStart(...) , setSelectionEnd(...) and replaceSelection(...) .您需要查看三种方法setSelectionStart(...)setSelectionEnd(...)replaceSelection(...)

Here is a small sample program to help your cause:这是一个小示例程序,可以帮助您解决问题:

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

public class TextAreaSelection
{
    private JTextField replaceTextField;
    private JTextField startIndexField;
    private JTextField endIndexField;

    private void createAndDisplayGUI()
    {
        final JFrame frame = new JFrame("JTextArea Selection");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setLocationByPlatform(true);

        JPanel contentPane = new JPanel();
        contentPane.setLayout(new BorderLayout(5, 5));
        contentPane.setOpaque(true);

        final JTextArea tarea = new JTextArea(10, 10);
        tarea.setText("aaa bbb");

        final JButton updateButton = new JButton("UPDATE TEXT");
        updateButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                //tarea.setSelectionStart(4);
                //tarea.setSelectionEnd(7);
                //tarea.replaceSelection("house");
                int selection = JOptionPane.showConfirmDialog(null, getPanel());
                if (selection == JOptionPane.OK_OPTION)
                {
                    if (replaceTextField.getDocument().getLength() > 0
                        && startIndexField.getDocument().getLength() > 0
                        && endIndexField.getDocument().getLength() > 0)
                    {   
                        String text = replaceTextField.getText().trim();
                        int start = Integer.parseInt(startIndexField.getText().trim());
                        int end = Integer.parseInt(endIndexField.getText().trim());
                        tarea.replaceRange(text, start, end);
                    }
                }
            }
        });

        contentPane.add(tarea, BorderLayout.CENTER);
        contentPane.add(updateButton, BorderLayout.PAGE_END);

        frame.getContentPane().add(contentPane);
        frame.pack();
        frame.setVisible(true);
    }

    private JPanel getPanel()
    {
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(0, 2, 2, 2));

        JLabel replaceLabel = new JLabel("Enter new String : "
                                                , JLabel.CENTER);
        replaceTextField = new JTextField(10);

        JLabel startIndexLabel = new JLabel("Enter Start Index : "
                                                , JLabel.CENTER);
        startIndexField = new JTextField(10);   

        JLabel endIndexLabel = new JLabel("Enter End Index : ");
        endIndexField = new JTextField(10); 

        panel.add(replaceLabel);
        panel.add(replaceTextField);
        panel.add(startIndexLabel);
        panel.add(startIndexField);
        panel.add(endIndexLabel);
        panel.add(endIndexField);

        return panel;
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new TextAreaSelection().createAndDisplayGUI();
            }
        });
    }
}

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

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