如何更新JTextPane中已存在的行?

时间:2014-11-16 23:17:04

标签: java user-interface document jtextpane

我想在JTextPane中加几行,比如Joseph Red,Clarita Red,Bob Red,后来我想更新一个特定行的名称和颜色,比如,我想改变Joseph Red为Rudo Blue,或Bob Red为Molly Blue。有办法吗?我想在每次向JTextPane添加一行时记录每一行,并引用该特定行以便稍后更新,但是想不出办法。

String color = "Red";
JTextPane textPanel = new JTextPane();

public void addToTextPane(String name) throws BadLocationException //Add each line to JTextPane
{
    document = (StyledDocument) textPanel.getDocument();
    document.insertString(document.getLength(), name + "" + color, null);
    document.insertString(document.getLength(), "\n", null);
}

我正在尝试执行以下操作(更新已存在于JTextPane中的特定行的名称和颜色):

if(...){ 
    status = "Blue";
    try
    {
        addTextPane("Jospeh"); //If I do this, it would not update the already exiting line and
                               //simply just add a new line with the name 'Joseph' and color 'Blue'
    }
    catch (BadLocationException e)
    {
        e.printStackTrace();
    }
}

1 个答案:

答案 0 :(得分:1)

for-loopDocument#getTextDocument#remove Document#insertString结合应该可以解决问题...

Text

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JTextPane textPane;
        private String fruit[] = {"Bananas", "Apples", "Oranges", "Kiwis"};
        private int index;

        public TestPane() {

            StringBuilder text = new StringBuilder(64);
            text.append("Bananas in pajamas are coming down the stairs\n").
                            append("Bananas in pajamas are coming down in pairs\n").
                            append("Bananas in pajamas are chasing teddy bears\n").
                            append("Cause on tuesdays they try to catch their man-o-wears");

            textPane = new JTextPane();
            textPane.setText(text.toString());
            setLayout(new BorderLayout());

            add(new JScrollPane(textPane));

            JButton btn = new JButton("Update");
            btn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    index++;
                    String find = fruit[(index - 1) % fruit.length];
                    String replace = fruit[index % fruit.length];
                    System.out.println("Find: " + find);
                    System.out.println("Replace: " + replace);

                    Document doc = textPane.getDocument();

                    try {
                        for (int pos = 0; pos < doc.getLength() - find.length(); pos++) {

                            String text = doc.getText(pos, find.length());
                            if (find.equals(text)) {
                                doc.remove(pos, find.length());
                                doc.insertString(pos, replace, null);
                            }

                        }
                    } catch (BadLocationException exp) {
                        exp.printStackTrace();
                    }

                }
            });

            add(btn, BorderLayout.SOUTH);

        }

    }

}