【问题标题】:Very slow performance when highlighting words in a JtextPane在 JtextPane 中突出显示单词时性能非常慢
【发布时间】:2015-04-24 01:59:36
【问题描述】:

在 JTextPane 中,突出显示约 50000 行文本的性能非常缓慢。我有机会提高性能吗?

这是一个 SSCE

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 {
    	this.textPane = new JTextPane();
    	this.textField = new JTextField();
    	this.doc = new DefaultStyledDocument();    	
   	
    	generateSomeText();    	
    	addTextFieldListener();  
    	createAndShowGUI(textField);
    } //end of constructor
     
    private void generateSomeText() throws BadLocationException {
    	for(int i=0;i<=50000;i++) {
    		doc.insertString(doc.getLength(), "hello world, hello stackoverflow, here is some generated text "+i+"\n", null);
    	}
    	textPane.setDocument(doc);
    }
    
    //here, the text will be highlighter after a search query is inserted
    private void addTextFieldListener() {
    	
    	textField.addActionListener( new ActionListener() {			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				float start = System.nanoTime(); //start meausuring time
				
		    	textPane.setHighlighter(hilit);    	

				String query = textField.getText();
				String text = textPane.getText();
	    		text = text.replaceAll("[\n]+", "");
				
				if(text != null) {					
					int index = text.indexOf(query); //get index of word						
					int len = query.length(); 		 //get length of word

					while ( index >= 0 ) {
						try {
							textPane.getHighlighter().addHighlight(index, index+len, painter);
						} catch (BadLocationException e) {
							e.printStackTrace();
						}   
							index = text.indexOf(query, (index+len));
					}        
				}
				float stop = System.nanoTime(); //stop meausuring time
				System.out.println("time="+(stop-start)/1000000000+"s");
			}
		});
    }
 
   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 {
    	new TextFieldUnicode();
    }
}

这是我的示例的屏幕截图:大约需要。当我突出显示 e 字母时,在 i5@1,9GHz/8GByte Ram 上需要 2 秒。关于如何以更好的方式做到这一点的任何建议?

【问题讨论】:

  • 也许你使用的算法效率不高?
  • 也许 JTextPane 有 50,000 行的想法已经是个坏主意了?
  • 检查这里以获得一些想法java-sl.com/JEditorPanePerformance.html 并请发布您的代码以进行分析
  • 在我的机器上没有花费 2 秒。时间=0.26843545s,时间=0.13421772s,时间=0.13421772s。并为 SSCCE +1
  • @user3300710:也许您可以通过仅突出显示actually visible 的行来改进算法?

标签: java performance swing highlight jtextpane


【解决方案1】:

正如 cmets 中的某个人指出的那样,您可以尝试仅突出显示可见文本。这结束得更快。这是一个使用您的代码的示例(可能可以改进):

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.JViewport;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultHighlighter;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.Document;
import javax.swing.text.Highlighter;

public class TextFieldUnicode {

  private JScrollPane scrollPane;

  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 {
    this.textPane = new JTextPane();
    this.textField = new JTextField();
    this.doc = new DefaultStyledDocument();
    this.scrollPane = new JScrollPane(this.textPane);
    this.textPane.setHighlighter(this.hilit);

    generateSomeText();
    addTextFieldListener();
    createAndShowGUI(this.textField);
  } // end of constructor

  private void generateSomeText() throws BadLocationException {
    for (int i = 0; i <= 50000; i++) {
      doc.insertString(doc.getLength(),
          "hello world, hello stackoverflow, here is some generated text " + i
              + "\n", null);
    }
    textPane.setDocument(doc);
  }

  // here, the text will be highlighter after a search query is inserted
  private void addTextFieldListener() {
    textField.addActionListener(new ActionListener() {

      @Override
      public void actionPerformed(ActionEvent arg0) {
        paintHighlights();
      }
    });
    textPane.addComponentListener(new ComponentListener() {

      @Override
      public void componentShown(ComponentEvent e) {
      }

      @Override
      public void componentResized(ComponentEvent e) {
      }

      @Override
      public void componentMoved(ComponentEvent e) {
        paintHighlights();
      }

      @Override
      public void componentHidden(ComponentEvent e) {
      }
    });
  }

  private void paintHighlights() {
    float start = System.nanoTime(); // start measuring time

    textPane.getHighlighter().removeAllHighlights();

    String query = textField.getText();
    String text = null;

    JViewport viewport = scrollPane.getViewport();
    Rectangle viewRect = viewport.getViewRect();

    Point p = viewRect.getLocation();
    int startIndex = textPane.viewToModel(p);

    p.x += viewRect.width;
    p.y += viewRect.height;
    int endIndex = textPane.viewToModel(p);

    DefaultStyledDocument document = (DefaultStyledDocument) textPane
        .getDocument();
    try {
      text = document.getText(startIndex, endIndex - startIndex);
    } catch (BadLocationException ex) {
      ex.printStackTrace();
    }

    if (text != null) {
      int index = text.indexOf(query); // get index of word
      int len = query.length(); // get length of word

      while (index >= 0) {
        try {
          textPane.getHighlighter().addHighlight(startIndex + index,
              startIndex + index + len, painter);
        } catch (BadLocationException e) {
          e.printStackTrace();
        }

        index = text.indexOf(query, (index + len));
      }
    }
    float stop = System.nanoTime(); // stop meausuring time
    System.out.println("time=" + (stop - start) / 1000000000 + "s");
  }

  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(this.scrollPane, BorderLayout.CENTER);
    frame.setSize(new Dimension(600, 600));
    frame.setVisible(true);
  }

  public static void main(String[] args) throws BadLocationException {
    new TextFieldUnicode();
  }
}

【讨论】:

    猜你喜欢
    • 2013-10-25
    • 1970-01-01
    • 1970-01-01
    • 2013-11-14
    • 2011-08-06
    • 1970-01-01
    • 2014-08-07
    • 2012-11-01
    相关资源
    最近更新 更多