How can I justify text in a JTextPane?
Created May 4, 2012
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);