В JTextPane производительность выделения текста с ~ 50000 строк очень медленная. У меня есть шанс увеличить предварительную подготовку?Очень низкая производительность при подсвечивании слов в JtextPane
Вот ВПЧЭ
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
public class TextFieldUnicode {
private JTextPane textPane;
private JTextField textField;
private Document doc;
private Highlighter hilit = new DefaultHighlighter();
private Highlighter.HighlightPainter painter = new DefaultHighlighter.DefaultHighlightPainter(Color.GREEN);
private TextFieldUnicode() throws BadLocationException {
\t this.textPane = new JTextPane();
\t this.textField = new JTextField();
\t this.doc = new DefaultStyledDocument(); \t
\t
\t generateSomeText(); \t
\t addTextFieldListener();
\t createAndShowGUI(textField);
} //end of constructor
private void generateSomeText() throws BadLocationException {
\t for(int i=0;i<=50000;i++) {
\t \t doc.insertString(doc.getLength(), "hello world, hello stackoverflow, here is some generated text "+i+"\n", null);
\t }
\t textPane.setDocument(doc);
}
//here, the text will be highlighter after a search query is inserted
private void addTextFieldListener() {
\t
\t textField.addActionListener(new ActionListener() { \t \t \t
\t \t \t @Override
\t \t \t public void actionPerformed(ActionEvent arg0) {
\t \t \t \t float start = System.nanoTime(); //start meausuring time
\t \t \t \t
\t \t \t textPane.setHighlighter(hilit); \t
\t \t \t \t String query = textField.getText();
\t \t \t \t String text = textPane.getText();
\t \t \t text = text.replaceAll("[\n]+", "");
\t \t \t \t
\t \t \t \t if(text != null) { \t \t \t \t \t
\t \t \t \t \t int index = text.indexOf(query); //get index of word \t \t \t \t \t \t
\t \t \t \t \t int len = query.length(); \t \t //get length of word
\t \t \t \t \t while (index >= 0) {
\t \t \t \t \t \t try {
\t \t \t \t \t \t \t textPane.getHighlighter().addHighlight(index, index+len, painter);
\t \t \t \t \t \t } catch (BadLocationException e) {
\t \t \t \t \t \t \t e.printStackTrace();
\t \t \t \t \t \t }
\t \t \t \t \t \t \t index = text.indexOf(query, (index+len));
\t \t \t \t \t }
\t \t \t \t }
\t \t \t \t float stop = System.nanoTime(); //stop meausuring time
\t \t \t \t System.out.println("time="+(stop-start)/1000000000+"s");
\t \t \t }
\t \t });
}
private void createAndShowGUI(JTextField textField) {
//Create and set up the window.
JFrame frame = new JFrame("TextFieldDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(textField, BorderLayout.NORTH);
frame.add(new JScrollPane(textPane), BorderLayout.CENTER);
frame.setSize(new Dimension(600, 600));
frame.setVisible(true);
}
public static void main(String[] args) throws BadLocationException {
\t new TextFieldUnicode();
}
}
А вот скриншот моего примера: он занимает ок. 2 секунды на i5 @ 1,9GHz/8GByte Ram, когда я выделяю буквы e. Любые предложения о том, как это сделать лучше?
Возможно, алгоритм, который вы используете, неэффективен? – Sharcoux
Возможно, идея «JTextPane» с 50 000 строк - это плохая идея? – EJP
Проверьте это, чтобы получить некоторые идеи http://java-sl.com/JEditorPanePerformance.html и, пожалуйста, разместите свой код для анализа. – StanislavL