JAVA PROGRAMMING SWING TEXT

From WeWeWeb Wiki
Jump to: navigation, search

StyledDocument insert text:

   StyleContext context = new StyleContext();
   StyledDocument document = new DefaultStyledDocument(context);

   Style style = context.getStyle(StyleContext.DEFAULT_STYLE);
   StyleConstants.setAlignment(style, StyleConstants.ALIGN_RIGHT);
   StyleConstants.setFontSize(style, 14);
   StyleConstants.setSpaceAbove(style, 4);
   StyleConstants.setSpaceBelow(style, 4);

   // Insert content
   try {
     document.insertString(document.getLength(), message, style);
   } catch (BadLocationException badLocationException) {
     System.err.println("Oops");
   }

   SimpleAttributeSet attributes = new SimpleAttributeSet();
   StyleConstants.setBold(attributes, true);
   StyleConstants.setItalic(attributes, true);

   // Insert content
   try {
     document.insertString(document.getLength(), "Hello Java",
         attributes);
   } catch (BadLocationException badLocationException) {
     System.err.println("Oops");
   }