swing
StyledDocument
खोज…
वाक्य - विन्यास
- doc.insertString (सूचकांक, पाठ, गुण); // विशेषताएँ एक विशेषता होनी चाहिए
एक DefaultStyledDocument बनाना
try {
StyledDocument doc = new DefaultStyledDocument();
doc.insertString(0, "This is the beginning text", null);
doc.insertString(doc.getLength(), "\nInserting new line at end of doc", null);
MutableAttributeSet attrs = new SimpleAttributeSet();
StyleConstants.setBold(attrs, true);
doc.insertString(5, "This is bold text after 'this'", attrs);
} catch (BadLocationException ex) {
//handle error
}
DefaultStyledDocuments शायद आपके सबसे अधिक उपयोग किए जाने वाले संसाधन होंगे। वे सीधे बनाए जा सकते हैं, और StyledDocument
एब्सट्रैक्ट क्लास को उपवर्ग बना सकते हैं।
JTextPane के लिए स्टाइलडेलडिमेंट जोड़ना
try {
JTextPane pane = new JTextPane();
StyledDocument doc = new DefaultStyledDocument();
doc.insertString(0, "Some text", null);
pane.setDocument(doc); //Technically takes any subclass of Document
} catch (BadLocationException ex) {
//handle error
}
JTextPane को किसी भी स्विंग GUI फॉर्म में जोड़ा जा सकता है।
DefaultStyledDocument की प्रतिलिपि बनाना
आमतौर पर StyledDocuments
क्लोन को लागू नहीं करते हैं, और इसलिए यदि आवश्यक हो तो उन्हें एक अलग तरीके से कॉपी करना होगा।
try {
//Initialization
DefaultStyledDocument sourceDoc = new DefaultStyledDocument();
DefaultStyledDocument destDoc = new DefaultStyledDocument();
MutableAttributeSet bold = new SimpleAttributeSet();
StyleConstants.setBold(bold, true);
MutableAttributeSet italic = new SimpleAttributeSet();
StyleConstants.setItalic(italic, true);
sourceDoc.insertString(0, "Some bold text. ", bold);
sourceDoc.insertString(sourceDoc.getLength(), "Some italic text", italic);
//This does the actual copying
String text = sourceDoc.getText(0, sourceDoc.getLength()); //This copies text, but loses formatting.
for (int i = 0; i < text.length(); i++) {
Element e = destDoc.getCharacterElement(i); //A Elment describes a particular part of a document, in this case a character
AttributeSet attr = e.getAttributes(); //Gets the attributes for the character
destDoc.insertString(destDoc.getLength(), text.substring(i, i+1), attr); //Gets the single character and sets its attributes from the element
}
} catch (BadLocationException ex) {
//handle error
}
एक DefaultStyledDocument आरटीएफ के लिए सीरियल
AdvancedRTFEditorKit लाइब्रेरी का उपयोग करके आप DefaultStyledDocument
को RTF स्ट्रिंग में क्रमबद्ध कर सकते हैं।
try {
DefaultStyledDocument writeDoc = new DefaultStyledDocument();
writeDoc.insertString(0, "Test string", null);
AdvancedRTFEditorKit kit = new AdvancedRTFEditorKit();
//Other writers, such as a FileWriter, may be used
//OutputStreams are also an option
Writer writer = new StringWriter();
//You can write just a portion of the document by modifying the start
//and end indexes
kit.write(writer, writeDoc, 0, writeDoc.getLength());
//This is the RTF String
String rtfDoc = writer.toString();
//As above this may be a different kind of reader or an InputStream
StringReader reader = new StringReader(rtfDoc);
//AdvancedRTFDocument extends DefaultStyledDocument and can generally
//be used wherever DefaultStyledDocument can be.
//However for reading, AdvancedRTFDocument must be used
DefaultStyledDocument readDoc = new AdvancedRTFDocument();
//You can insert at different values by changing the "0"
kit.read(reader, readDoc, 0);
//readDoc is now the same as writeDoc
} catch (BadLocationException | IOException ex) {
//Handle exception
ex.printStackTrace();
}
Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow