Here you can find the source of centerText(JTextPane pane)
public static void centerText(JTextPane pane)
//package com.java2s; //License from project: LGPL import javax.swing.JTextPane; import javax.swing.text.SimpleAttributeSet; import javax.swing.text.StyleConstants; import javax.swing.text.StyledDocument; public class Main { public static void centerText(JTextPane pane) { StyledDocument doc = pane.getStyledDocument(); SimpleAttributeSet center = new SimpleAttributeSet(); StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER); doc.setParagraphAttributes(0, doc.getLength(), center, false); }// ww w . jav a2s . c o m }