Question

J'ai un JFrame qui contient un JTextPane. Le but de cette JTextPane est de mettre en évidence les mots que je les saisis, quelque chose le long des lignes de l'éditeur de texte d'un programmeur. Pour ce faire, j'étendu JTextPane, je mis en œuvre le KeyListener interface, et je l'avais configuré comme un écouteur de touche à l'auto. La méthode qui fait un travail important est keyReleased . Le problème est, je peux mettre en évidence le premier mot de type I, mais après cela, je continue à me BadLocation, même si le start et fin sont dans les limites du document. Je posterai quelques-uns de mes extraits de code:


// 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;
            }
        }
    }

J'ai essayé de taper: public static mais publique est de couleur rouge. J'ai même ajouté quelques instructions println pour le débogage, et c'est la sortie:

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)

Je suis en train d'écrire ce code pour obtenir un coup de JTextPane. Je ne suis pas intéressé par quelque chose comme JTextPane .

Était-ce utile?

La solution

Comme vous le découvriez, Document.getText () prend un début et longueur , pas un début et end argument. Je pensais fournir une réponse et un lien vers la JavaDoc pour d'autres la lecture de votre question qui pourrait ne pas regarder dans les commentaires.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top