How can I justify text in a JTextPane?

Sandip Chitale

You will have to use the following classes -

  • javax.swing.text.StyledDocument
  • javax.swing.text.Style
  • javax.swing.text.StyleConstants
  • javax.swing.text.StyleConstants
  • javax.swing.text.SimpleAttributeSet
  • javax.swing.JTextPane

Specifically you will have to use the StyleConstants.Alignment attribute with a value of StyleConstants.ALIGN_JUSTIFY constant for the paragraph.

Here is an article which explains how to use the AttributeSets and Styles

http://java.sun.com/products/jfc/tsc/articles/text/attributes/

The actual code will look something like this


SimpleAttributeSet sa = new SimpleAttributeSet();
StyleConstants.setAlignment(sa, StyleConstants.ALIGN_JUSTIFY);

textPane.getStyledDocument().setParagraphAttributes(0,/*document length*/,sa,false);

0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact