问题
I want to align two sections of texts on the same line: first section should be aligned to the left side, and the other section should be aligned to the right side of the java swing JTextPane. I tried to use style interface and Styleconstants class to align the text, but it didn't work. But when I applied some other styles, such as Styleconstants.setFontSize(), Styleconstants.setForeGroundColor(), on the same text, it's working fine.
Here is my code:
JTextPane pane = new JTextPane();
StyledDocument sdoc = pane.getStyledDocument();
SimpleAttributeSet rightAlign = new SimpleAttributeSet();
StyleConstants.setAlignment(rightAlign, StyleConstants.ALIGN_RIGHT);
StyleConstants.setForeground(rightAlign, Color.lightGray);
StyleConstants.setFontSize(rightAlign, 11);
sdoc.insertString(sdoc.getLength(), "name", null);
sdoc.insertString(sdoc.getLength(), "timeHis" + "\n", rightAlign);
It gives the output like
nametimeHis
but I want output like this
name timeHis
(exact opposite side in JTextpane in same line )
Is there anything wrong in my code? How can I solve this problem?
回答1:
I don't think you can set two different alignments for the same line (AFAIK you can't do that even in professional text editors - just by setting alignment). I rewrote your code like this:
SimpleAttributeSet rightAlign = new SimpleAttributeSet();
SimpleAttributeSet leftAlign = new SimpleAttributeSet();
StyleConstants.setAlignment(rightAlign, StyleConstants.ALIGN_RIGHT);
StyleConstants.setForeground(rightAlign, Color.lightGray);
StyleConstants.setFontSize(rightAlign, 11);
StyleConstants.setAlignment(leftAlign, StyleConstants.ALIGN_LEFT);
StyleConstants.setForeground(leftAlign, Color.black);
StyleConstants.setFontSize(leftAlign, 13);
String left = "name";
String right = "timeHis\n";
sdoc.insertString(0, left, leftAlign);
sdoc.insertString(left.length(), right, rightAlign);
sdoc.setParagraphAttributes(0, left.length(), leftAlign, false);
sdoc.setParagraphAttributes(left.length()+1, sdoc.getLength()-1-left.length(), rightAlign, false);
Size and color are OK but alignment for both strings is right. If you swap alignments in the last two lines like this:
sdoc.setParagraphAttributes(0, left.length(), rightAlign, false);
sdoc.setParagraphAttributes(left.length()+1, sdoc.getLength()-1-left.length(),leftAlign, false);
both will be left aligned, but if you add a new line in your left string:
String left = "name\n";
the alignment also becomes honored. The setParagraphAttributes method does exactly what it says - the point is that alignment is a paragraph attribute - open OO Writer or MS Word(pad), write some text and align it - it will be clearer.
When I want to achieve that alignment in MS Word/OO Writer I make a table with invisible borders, and set the left column the left alignment and to right column the right alignment. That would lead to conclusion that we need the HTMLDocument (because you can make a table in HTML) class which implements the StyledDocument interface. So I tried this:
JTextPane pane = new JTextPane(new HTMLDocument());
which results in no style. It makes sense since Cascading Style Sheets is the proper way to style HTML. This constructor explains it also:
public HTMLDocument(StyleSheet styles)
Here's a link to the StyleSheet constructor argument.
But, this will do the trick:
JTextPane pane = new JTextPane();
pane.setEditorKit(new HTMLEditorKit());
An EditorKit :
Establishes the set of things needed by a text component to be a reasonably functioning editor for some type of text content. The EditorKit acts as a factory for some kind of policy. For example, an implementation of html and rtf can be provided that is replaceable with other implementations.
A few EditorKit methods are:
createCaret()
getContentType()
read(Reader in, Document doc, int pos)
write(Writer out, Document doc, int pos, int len)
so, basically it is a programmable editor (through read and write methods) for a text component's text content, namely the Document:
The Document is a container for text that serves as the model for swing text components. The goal for this interface is to scale from very simple needs (a plain text textfield) to complex needs (an HTML or XML document, for example).
Hence, HTMLEditorKit is a programmable editor for a text component's HTML text content, namely the HTMLDocument - take a close look at the inheritance chain of this class and the implementing interfaces - you'll notice almost all of them throughout this answer.
Now, this is the magic part from the HTMLEditorKit apidoc:
Although the Document provides HTML support by default, there is nothing preventing support of non-HTML tags that result in alternative element structures.
So it looks like the default implementation of this class translated our style attributes to HTML/CSS just the way we wanted them: left and right alignment in the same line.
It makes sense because with HTML/CSS you can do it in at least two ways:
- making a two column table and setting their alignments respectively
- making two floated divs and setting their alignments respectivley
and there is most probably more ways to do it.
来源:https://stackoverflow.com/questions/13069730/textalignment-is-not-working-in-java-swing-jtextpane