JAVA PROGRAMMING SWING TEXT

From WeWeWeb Wiki
Jump to navigationJump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

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");
   }