0

How to change font of JTextPane and center text. I tried with textPane.setFont(font); but then i cannot center text and i tried using

    SimpleAttributeSet attribs = new SimpleAttributeSet();  
    StyleConstants.setAlignment(attribs , StyleConstants.ALIGN_CENTER); 

, but then i cannot change font...

user1610362
  • 637
  • 2
  • 9
  • 26
  • Have a look at this [post](http://stackoverflow.com/questions/6949125/font-of-a-styleddocument-associated-with-a-jtextpane) or this [one](http://stackoverflow.com/questions/10585956/jtextpane-how-to-set-the-font-size)... – aymeric Aug 27 '12 at 15:27

1 Answers1

0

I solved my problem with this:

    StyleContext context = new StyleContext();
    StyledDocument document = new DefaultStyledDocument(context);
    Style style = context.getStyle(StyleContext.DEFAULT_STYLE);
    StyleConstants.setAlignment(style, StyleConstants.ALIGN_CENTER);
    JTextPane output = new JTextPane(document);
    output.setFont(font);  
user1610362
  • 637
  • 2
  • 9
  • 26