JTextPane continua a gettare BadLocation
-
06-09-2019 - |
Domanda
Ho un JFrame che contiene un JTextPane. Lo scopo di questo JTextPane è quello di evidenziare le parole come li ho tipo, qualcosa sulla falsariga di editor di testo di un programmatore. Per fare questo, ho esteso JTextPane, ho implementato il KeyListener l'interfaccia, e avevo impostato come listener per i tasti per sé. Il metodo che fa un lavoro importante è keyReleased . Il problema è, posso evidenziare la prima parola tipo I, ma dopo che, continuo a ricevere BadLocation, anche se la inizio e fine sono entro i limiti del documento. Sto inviando alcuni dei miei frammenti di codice:
// 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;
}
}
}
Ho provato a digitare: public static , ma solo pubblico è di colore rosso. Ho anche aggiunto alcune dichiarazioni println per il debug, e questo è l'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)
Sto scrivendo questo codice per ottenere un blocco di JTextPane. Io non sono interessato a qualcosa come JTextPane .
Soluzione
Come hai scoperto, il Document.getText () prende un inizio e lunghezza , non un inizio e end argomento. Ho pensato di dare una risposta e un link al JavaDoc per gli altri a leggere la tua domanda che potrebbe non guardare nei commenti.