w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Center alignment of a word in JTextPane

You can try to center it manually.

Calculate the word's width and calculate the JTextPane's widht. Then get additional shift and set paragraph's left indent to the shift.

Another potentially better solution would be to use TabStop with ALIGN_CENTER. So you can define the TabStop with position equals the center x and set the alignment to be CENTER. Then insert char just before the word.

UPDATE: TabStop clarification.

You can define TabSet paragraph attribute. See StyleConstants

public static void setTabSet(MutableAttributeSet a, TabSet tabs)

TabSet has the constructor

public TabSet(TabStop[] tabs)

Then see TabStop constructor

public TabStop(float pos, int align, int leader)

where you can set center align

Then just call

doc.setParagraphAttributes(theAttrsWithTabSet);




© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.