Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback, please email oracle-forums_moderators_us@oracle.com

Fast styled JTextPane editor

807606Jun 8 2006 — edited Feb 13 2007
Dear all,

I'm trying to create an editor which adds word style and make autocompletion when there are recognized.
All is performed correctly nevertheless, when i have a lot a text, each new inserted character or word takes a long time. How to optimize the jtextpane performance ???

Each time a character is typed, my document notify a update event and then
i get the content line to add style to the specific word.

example code :
//here we need to identify position in line for suggestion in popup menu
//and need to validate the contents line for adding recognized word style.
protected void update() {
		
		CommandWordDescriptor wordDescriptor = CommandLineParser.getInstance().getCommandWordDescriptor(getPositionInLine());
		setWordDescriptor(wordDescriptor);
		
		if (document.getLength()>0) {
			validate(getLineContents().substring(commandPrompt.length()));
		}
		
	}

private String getLineContents() {
		
		String contents = null;

		int startOffset = getStartOffset();
		int length = getEndOffset() - startOffset;
		
		try {
			contents = textPane.getText(startOffset, length);
		} catch (BadLocationException e) {
			e.printStackTrace();
		}
		
		return contents;
	}

protected int getNumberOfLines() {
		return document.getDefaultRootElement().getElementIndex(document.getLength());
	}
	
	protected int getCurrentLineNumber() {
		
		int dot = textPane.getCaretPosition();
		//get current line
		int line = document.getDefaultRootElement().getElementIndex(dot);
		
		return line;
	}
	
	protected int getPositionInLine() {
		
		int dot = textPane.getCaretPosition();
		//get current line
		int line = document.getDefaultRootElement().getElementIndex(dot);
		
		//caret position : 0 = value is before prompt
		//				   1 = value is prompt
		//				   2 = value is space after prompt....
		int caretPositionOnLine = dot - document.getDefaultRootElement().getElement(line).getStartOffset();
		//text position after prompt sign from 0 to end of command line
		int textPositionOnLine = caretPositionOnLine - commandPrompt.length();
		
		return textPositionOnLine;
	}
	
	public int getCaretPositionOnLine() {
		
		int dot = textPane.getCaretPosition();
		//get current line
		int line = document.getDefaultRootElement().getElementIndex(dot);
		
		//caret position : 0 = value is before prompt
		//				   1 = value is prompt
		//				   2 = value is space after prompt....
		int caretPositionOnLine = dot - document.getDefaultRootElement().getElement(line).getStartOffset();
		
		return caretPositionOnLine;
	}
	
	private int getStartOffset(){
		
		int dot = textPane.getCaretPosition();
		//get current line
		int line = document.getDefaultRootElement().getElementIndex(dot);
		int startOffset = document.getDefaultRootElement().getElement(line).getStartOffset();
		
		return startOffset;
	}
	
	private int getEndOffset(){
		
		int dot = textPane.getCaretPosition();
		//get current line
		int line = document.getDefaultRootElement().getElementIndex(dot);
		int endOffset = document.getDefaultRootElement().getElement(line).getEndOffset() - 1;
		
		return endOffset;
	}
	
	protected int getAbsolutePosition(int positionInLine) {
		//startoffset in current line
		return getStartOffset() + positionInLine + commandPrompt.length();
	}
Thanks in advance for any helps.....
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 13 2007
Added on Jun 8 2006
6 comments
2,846 views