2012-05-12 8 views
5

यह देखते हुए किके साथ या बिना javax.swing.text.Document

JTextArea t = new JTextArea(); 
Document d = t.getDocument(); 
String word1 = "someWord"; 
String word2 = "otherWord" 
int pos = t.getText().indexOf(word1,i); 

बीच क्या अंतर है ...
इस

if(pos!= -1){ 
    t.replaceRange(word2.toUpperCase(), pos, pos+ word1.length()); 
} 

और इस

if(pos!= -1){ 
    d.remove(pos, word1.length()); 
    d.insertString(pos, word2.toUpperCase(), null); 
} 

उत्तर

8

आखिरकार यह वही काम करता है।

JTextAreahere के स्रोत कोड पर जाएं, जहां आप पाते हैं कि यह वही काम कर रहा है। आप कहां मिल सकता है कि यह फोन करने के मामले में

d.remove(pos, word1.length()); 
    d.insertString(pos, word2.toUpperCase(), null); 

कर रहा है मैं यहाँ भी विधि की नकल की है:

t.replaceRange(word2.toUpperCase(), pos, pos+ word1.length()); 

विधि। वर्ग की विधि की

स्रोत कोड के नीचे

public void replaceRange(String str, int start, int end) { 

    490   if (end < start) { 
    491    throw new IllegalArgumentException ("end before start"); 
    492   } 
    493   Document doc = getDocument(); 
    494   if (doc != null) { 
    495    try { 
    496     if (doc instanceof AbstractDocument) { 
    497      ((AbstractDocument)doc).replace(start, end - start, str, 
    498              null); 
    499     } 
    500     else { 
    501      doc.remove(start, end - start); 
    502      doc.insertString(start, str, null); 
    503     } 
    504    } catch (BadLocationException e) { 
    505     throw new IllegalArgumentException (e.getMessage()); 
    506    } 
    507   } 
    508  } 
+1

उत्सुक: क्यों बक्षीस? क्या आप अपने उत्तर से खुश नहीं हैं? – kleopatra

-1

मैं Bhavik से सहमत है, यहां तक ​​कि श्रोताओं दस्तावेज़ कोई अंतर नहीं देख पाएंगे:

txt.getDocument().addDocumentListener(new DocumentListener() { 
    @Override 
    public void removeUpdate(DocumentEvent pE) { 
     System.out.println("removeUpdate: "+pE); 
    } 
    @Override 
    public void insertUpdate(DocumentEvent pE) { 
     System.out.println("insertUpdate: "+pE); 
    } 
    @Override 
    public void changedUpdate(DocumentEvent pE) { 
     System.out.println("changedUpdate: "+pE); 
    } 
}); 

दोनों ही मामलों में उत्पादन करेगा :

removeUpdate: [[email protected] hasBeenDone: true alive: true] 
insertUpdate: [[email protected] hasBeenDone: true alive: true] 
संबंधित मुद्दे