本文整理了Java中javax.swing.text.StyledDocument.insertString()
方法的一些代码示例,展示了StyledDocument.insertString()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。StyledDocument.insertString()
方法的具体详情如下:
包路径:javax.swing.text.StyledDocument
类名称:StyledDocument
方法名:insertString
暂无
代码示例来源:origin: stackoverflow.com
JTextPane textPane = new JTextPane();
textPane.setText( "original text" );
StyledDocument doc = textPane.getStyledDocument();
// Define a keyword attribute
SimpleAttributeSet keyWord = new SimpleAttributeSet();
StyleConstants.setForeground(keyWord, Color.RED);
StyleConstants.setBackground(keyWord, Color.YELLOW);
StyleConstants.setBold(keyWord, true);
// Add some text
try
{
doc.insertString(0, "Start of text\n", null );
doc.insertString(doc.getLength(), "\nEnd of text", keyWord );
}
catch(Exception e) { System.out.println(e); }
代码示例来源:origin: stackoverflow.com
public class Main {
public static void main(String[] args) {
JTextPane textPane = new JTextPane();
StyledDocument doc = textPane.getStyledDocument();
Style style = textPane.addStyle("I'm a Style", null);
StyleConstants.setForeground(style, Color.red);
try { doc.insertString(doc.getLength(), "BLAH ",style); }
catch (BadLocationException e){}
StyleConstants.setForeground(style, Color.blue);
try { doc.insertString(doc.getLength(), "BLEH",style); }
catch (BadLocationException e){}
JFrame frame = new JFrame("Test");
frame.getContentPane().add(textPane);
frame.pack();
frame.setVisible(true);
}
}
代码示例来源:origin: groovy/groovy-core
public void actionPerformed(ActionEvent ae) {
JTextComponent tComp = (JTextComponent) ae.getSource();
if (tComp.getDocument() instanceof StyledDocument) {
doc = (StyledDocument) tComp.getDocument();
try {
doc.getText(0, doc.getLength(), segment);
}
catch (Exception e) {
// should NEVER reach here
e.printStackTrace();
}
int offset = tComp.getCaretPosition();
int index = findTabLocation(offset);
buffer.delete(0, buffer.length());
buffer.append('\n');
if (index > -1) {
for (int i = 0; i < index + 4; i++) {
buffer.append(' ');
}
}
try {
doc.insertString(offset, buffer.toString(),
doc.getDefaultRootElement().getAttributes());
}
catch (BadLocationException ble) {
ble.printStackTrace();
}
}
}
代码示例来源:origin: RipMeApp/ripme
/**
* Write a line to the Log section of the GUI
*
* @param text the string to log
* @param color the color of the line
*/
private void appendLog(final String text, final Color color) {
SimpleAttributeSet sas = new SimpleAttributeSet();
StyleConstants.setForeground(sas, color);
StyledDocument sd = logText.getStyledDocument();
try {
synchronized (this) {
sd.insertString(sd.getLength(), text + "\n", sas);
}
} catch (BadLocationException e) { }
logText.setCaretPosition(sd.getLength());
}
代码示例来源:origin: apache/pdfbox
void writeIndent(StyledDocument docu) throws BadLocationException
{
if (needIndent)
{
for (int i = 0; i < indent; i++)
{
docu.insertString(docu.getLength(), " ", null);
}
needIndent = false;
}
}
}
代码示例来源:origin: ron190/jsql-injection
/**
* Add a colored string to the textpane, measure prompt at the same time.
* @param string Text to append
* @param color Color of text
* @param isAddingPrompt Should we measure prompt length?
*/
private void appendPrompt(String string, Color color, boolean isAddingPrompt) {
try {
StyleConstants.setForeground(this.style, color);
this.styledDocument.insertString(this.styledDocument.getLength(), string, this.style);
if (isAddingPrompt) {
this.prompt += string;
}
} catch (BadLocationException e) {
LOGGER.error(e.getMessage(), e);
}
}
代码示例来源:origin: magefree/mage
private void drawText(java.util.List<String> strings) {
text.setText("");
StyledDocument doc = text.getStyledDocument();
try {
for (String line : strings) {
doc.insertString(doc.getLength(), line + '\n', doc.getStyle("regular"));
}
} catch (BadLocationException ble) {
}
text.setCaretPosition(0);
}
代码示例来源:origin: apache/pdfbox
private StyledDocument getDocument(InputStream inputStream, String encoding)
{
StyledDocument docu = new DefaultStyledDocument();
if (inputStream != null)
{
String data = getStringOfStream(inputStream, encoding);
try
{
docu.insertString(0, data, null);
}
catch (BadLocationException e)
{
e.printStackTrace();
}
}
return docu;
}
代码示例来源:origin: magefree/mage
protected void drawText() {
text.setText("");
StyledDocument doc = text.getStyledDocument();
try {
for (String rule : getRules()) {
doc.insertString(doc.getLength(), rule + '\n', doc.getStyle("small"));
}
} catch (BadLocationException e) {
}
text.setCaretPosition(0);
}
代码示例来源:origin: apache/pdfbox
docu.insertString(docu.getLength(), str + " ", NAME_STYLE);
docu.insertString(docu.getLength(), str + " ", null);
docu.insertString(docu.getLength(), "[ ", null);
for (COSBase elem : (COSArray) obj)
docu.insertString(docu.getLength(), "] ", null);
docu.insertString(docu.getLength(), "(", null);
byte[] bytes = ((COSString) obj).getBytes();
for (byte b : bytes)
docu.insertString(docu.getLength(), str, ESCAPE_STYLE);
docu.insertString(docu.getLength(), str, ESCAPE_STYLE);
docu.insertString(docu.getLength(), str, STRING_STYLE);
docu.insertString(docu.getLength(), ") ", null);
docu.insertString(docu.getLength(), str + " ", NUMBER_STYLE);
docu.insertString(docu.getLength(), "<< ", null);
COSDictionary dict = (COSDictionary) obj;
for (Map.Entry<COSName, COSBase> entry : dict.entrySet())
docu.insertString(docu.getLength(), ">> ", null);
代码示例来源:origin: apache/pdfbox
docu.insertString(docu.getLength(), INLINE_IMAGE_BEGIN + "\n", OPERATOR_STYLE);
COSDictionary dic = op.getImageParameters();
for (COSName key : dic.keySet())
docu.insertString(docu.getLength(), "/" + key.getName() + " ", null);
writeToken(value, docu);
docu.insertString(docu.getLength(), "\n", null);
docu.insertString(docu.getLength(), IMAGE_DATA + "\n", INLINE_IMAGE_STYLE);
docu.insertString(docu.getLength(), imageString, null);
docu.insertString(docu.getLength(), "\n", null);
docu.insertString(docu.getLength(), INLINE_IMAGE_END + "\n", OPERATOR_STYLE);
docu.insertString(docu.getLength(), operator + "\n", OPERATOR_STYLE);
代码示例来源:origin: stackoverflow.com
StyleConstants.setFontSize(normal, 72);
StyleConstants.setForeground(normal, Color.blue);
doc.insertString(doc.getLength(), "Test", normal);
jtp.setSelectionStart(doc.getLength());
jtp.insertIcon(UIManager.getIcon("OptionPane.warningIcon"));
代码示例来源:origin: apache/pdfbox
doc.insertString(doc.getLength(), " " + levelText + " ", levelStyle);
doc.insertString(doc.getLength(), " [" + shortName + "]", nameStyle);
doc.insertString(doc.getLength(), " " + message + "\n", null);
代码示例来源:origin: org.netbeans.api/org-openide-text
/** Inserts a text into given offset and marks it guarded.
* @param doc document to insert to
* @param offset offset of insertion
* @param txt string text to insert
* @exception NullPointerException If the <code>doc</code> parameter
* is <code>null</code>.
*/
public static void insertGuarded(StyledDocument doc, int offset, String txt)
throws BadLocationException {
checkDocParameter(doc);
doc.insertString(offset, txt, ATTR_ADD);
}
代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-team-commons
private static void insertString(final StyledDocument doc, final String comment, final int last, final int start, final Style defStyle) {
try {
doc.insertString(doc.getLength(), comment.substring(last, start), defStyle);
} catch (BadLocationException ex) {
Support.LOG.log(Level.SEVERE, null, ex);
}
}
代码示例来源:origin: otros-systems/otroslogviewer
private void fillText() throws BadLocationException {
textArea.setText("");
StyledDocument sd = textArea.getStyledDocument();
sd.insertString(0, "OtrosLogViewer\n", titleStyle);
sd.insertString(sd.getLength(), "Build: " + build + "\n", mainStyle);
sd.insertString(sd.getLength(), "Project web page: https://github.com/otros-systems/otroslogviewer/\n", mainStyle);
sd.insertString(sd.getLength(), "Program documentation: https://github.com/otros-systems/otroslogviewer/wiki/Introduction?tm=6 \n", mainStyle);
sd.insertString(sd.getLength(), "License: Apache Commons 2.0", licenceStyle);
}
代码示例来源:origin: mucommander/mucommander
/**
* Inserts the specified string in the specified document.
* @param doc document in which to insert the text.
* @param string text to insert.
* @throws BadLocationException thrown if something wrong happened to the document.
*/
private static void insertNormalString(StyledDocument doc, String string) throws BadLocationException {
doc.insertString(doc.getLength(), string + LINE_BREAK, doc.getStyle(STYLE_NORMAL));
}
代码示例来源:origin: mucommander/mucommander
/**
* Inserts a line break in the specified document.
* @param doc document in which to insert the text.
* @throws BadLocationException thrown if something wrong happened to the document.
*/
private static void insertLineBreak(StyledDocument doc) throws BadLocationException {
doc.insertString(doc.getLength(), LINE_BREAK, doc.getStyle(STYLE_NORMAL));
}
代码示例来源:origin: mucommander/mucommander
/**
* Inserts the specified URL in the specified document.
* @param doc document in which to insert the text.
* @param url url to insert.
* @throws BadLocationException thrown if something wrong happened to the document.
*/
private static void insertUrl(StyledDocument doc, String url) throws BadLocationException {
doc.insertString(doc.getLength(), " ", doc.getStyle(STYLE_NORMAL));
doc.insertString(doc.getLength(), url + LINE_BREAK, doc.getStyle(STYLE_URL));
}
代码示例来源:origin: stackoverflow.com
try {
// Get the text pane's document
JTextPane textPane = new JTextPane();
StyledDocument doc = (StyledDocument)textPane.getDocument();
// The image must first be wrapped in a style
Style style = doc.addStyle("StyleName", null);
StyleConstants.setIcon(style, new ImageIcon("imagefile"));
// Insert the image at the end of the text
doc.insertString(doc.getLength(), "ignored text", style);
} catch (BadLocationException e) {
}
内容来源于网络,如有侵权,请联系作者删除!