views:

252

answers:

2

I have a JFrame that contains a JTextPane. The purpose of this JTextPane is to highlight words as I type them, something along the lines of a programmer's text editor. To accomplish this, I extended JTextPane, I implemented the KeyListener interface, and I had it setup as a key listener to self. The method that does some important work is keyReleased. The problem is, I can highlight the first word I type, but after that, I keep getting BadLocation, even though the start and the end are within document limits. I am posting some of my code snippets :


// this is my highlight method
private void highlight(int start,int end) throws BadLocationException {
      Document doc = getDocument();
      Color c = Color.red;
      String text = doc.getText(start,end);
      StyleContext sc = StyleContext.getDefaultStyleContext();
      AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, c);
      setCharacterAttributes(aset, true);
      setSelectionStart(start);
      setSelectionEnd(end);
      replaceSelection(text);
}

//this is my keyReleased method
public void keyReleased(KeyEvent arg0) {
        char character = arg0.getKeyChar();
        if(wordStarted) { // have I started typing a new word ?
            if(character == ' ') { // end word
                try {
                    int dot = getCaret().getDot();
                    highlight(wordStart, dot - 1);
                    setCaretPosition(dot);
                    wordStarted = false;
                } catch (BadLocationException ex) {
                    ex.printStackTrace();
                }
            }
        }
        else {
            if(Character.isLetter(character)) {
                wordStarted = true;
                wordStart = getCaret().getDot() -1;
            }
        }
    }
I tried to type in : public static but only public is colored red. I even added some println statements for debugging, and this is the output:
this is outputted after writing public
Start param:0
End param:6
Document Length:7
Document START:0
Document END:8
text:public

this is outputted after writing static
Start param:7
End param:13
Document Length:14
Document START:0
Document END:15
text:public static 
javax.swing.text.BadLocationException: Invalid location
        at javax.swing.text.GapContent.getChars(GapContent.java:189)
        at javax.swing.text.GapContent.getString(GapContent.java:167)
        at javax.swing.text.AbstractDocument.getText(AbstractDocument.java:774)
        at ifirst.visual.CodePanel.highlight(CodePanel.java:49)
        at ifirst.visual.CodePanel.keyReleased(CodePanel.java:82)
        at java.awt.Component.processKeyEvent(Component.java:6069)
        at javax.swing.JComponent.processKeyEvent(JComponent.java:2810)
        at java.awt.Component.processEvent(Component.java:5885)
        at java.awt.Container.processEvent(Container.java:2105)
        at java.awt.Component.dispatchEventImpl(Component.java:4469)
        at java.awt.Container.dispatchEventImpl(Container.java:2163)
        at java.awt.Component.dispatchEvent(Component.java:4295)
        at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1881)
        at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:742)
        at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:1007)
        at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:879)
        at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:706)
        at java.awt.Component.dispatchEventImpl(Component.java:4339)
        at java.awt.Container.dispatchEventImpl(Container.java:2163)
        at java.awt.Window.dispatchEventImpl(Window.java:2478)
        at java.awt.Component.dispatchEvent(Component.java:4295)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:604)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:275)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:200)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:185)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:177)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:138)

I am writing this code to get a hang of JTextPane. I am not interested in something like JTextPane.

A: 

My guess with information is that the replaceSelection() call is throwing. There is not enough information here to fully answer this question.

Clint
+2  A: 

As you discovered, theDocument.getText() takes a start and length, not a start and end argument. I thought I'd provide an answer and a link to the JavaDoc for others reading your question who might not look in the comments.

Eddie
This was pretty annoying. 2 hours of debugging can save 10 minutes of reading,right? :)
Geo
Definitely true. And the libraries are sometimes inconsistent, causing the need to check the javadocs, as half of the libraries use start,end+1 and the other half uses start,length. Feh!
Eddie