Sono un problema con l'aggiunta di un testo a un oggetto JTextArea dopo l'uso di un filtro documenti, Ho bisogno di aggiungere una stringa su JTextArea dopo che il testo è stato caricato da un file e restituisce anche una stringa da JTextArea di un altro JFrame alla JTextArea specificataAggiunta di un testo a un oggetto JTextArea dopo l'uso di un filtro documenti
Tutto ha funzionato perfettamente quando non ho utilizzato DocumentFilter.FilterBypass fino a quando non l'ho aggiunto. Funziona ancora un po 'ma solo quando la virgola (,) o lo spazio ("") non vengono aggiunti. Quale non è per la specifica che mi è stata data.
Come posso risolvere questo? O c'è qualche algoritmo o implementazione che non dà questo problema?
Questo è il codice insertString per filtrare la lunghezza, e consentire solo lo spazio e virgola
public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException {
// if (string == null || string.trim().equals("") || string.equals(","))
// {
// return;
// }
if (isNumeric(string)) {
// if (this.length > 0 && fb.getDocument().getLength() +
// string.length()
// > this.length) {
// return;
// }
if (fb.getDocument().getLength() + string.length() > this.length || string.trim().equals("") || string.equals(",")) {
this.insertString(fb, offset, string, attr);
}
// if (string == null || string.trim().equals("") ||
// string.equals(",")) {
// return;
// }
super.insertString(fb, offset, string, attr);
}
else if (string == null || string.trim().equals("") || string.equals(",")) {
super.insertString(fb, offset, string, attr);
}
}
@Override
public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
if (isNumeric(text)) {
if (this.length > 0 && fb.getDocument().getLength() + text.length() > this.length) {
return;
}
super.insertString(fb, offset, text, attrs);
}
}
/**
* This method tests whether given text can be represented as number. This
* method can be enhanced further for specific needs.
*
* @param text
* Input text.
* @return {@code true} if given string can be converted to number;
* otherwise returns {@code false}.
*/
private boolean isNumeric(String text) {
if (text == null || text.trim().equals("") || text.equals(",")) {
return true;
}
for (int iCount = 0; iCount < text.length(); iCount++) {
if (!Character.isDigit(text.charAt(iCount))) {
return false;
}
}
return true;
}
Le altre due funzioni (aggiungono da file & accodare da un fotogramma diverso) Voglio realizzare innocentemente semplicemente aggiungendo i loro valori stringa su JTextArea filtrati usando questo. Ma viene rifiutato da super.insertString (.....)
Quale codice sei usando al momento che non funziona? Mente postarla? – sealz
Nessun problema, ma è comunque lungo. Devi allegare i codici dei due telegrammi – nnanna
forse solo lo snippet in cui stai cercando di aggiungere "," e ""? – sealz