I want to append text in JTextPane , i make every line have different color, but when i tried to make line from left to right and right to left it's not workding
this is my code for method
private void appendToPane(JTextPane tp, String msg, Color c , String rientation)
{
ComponentOrientation ornt = ComponentOrientation.LEFT_TO_RIGHT;
if(orientation.equals("left"))
ornt = ComponentOrientation.LEFT_TO_RIGHT;
StyleContext sc = StyleContext.getDefaultStyleContext();
AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, c);
AttributeSet or = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Orientation , StyleConstants.ALIGN_RIGHT );
AttributeSet fnt = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Background , Color.YELLOW);
AttributeSet fontSize = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.FontSize, 25);
// aset = sc.addAttribute(aset, StyleConstants.FontFamily, "Lucida Console");
// aset = sc.addAttribute(aset, StyleConstants.Alignment, StyleConstants.ALIGN_JUSTIFIED);
int len = tp.getDocument().getLength();
tp.setCaretPosition(len);
tp.setCharacterAttributes(aset, false);
tp.setCharacterAttributes(or , false);
tp.setCharacterAttributes(fnt, false);
tp.setCharacterAttributes(fontSize, false);
// if(orientation.equals("left"))
// tp.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
// else
// tp.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
// if(orientation != null)
// tp.setAlignmentX(Component.RIGHT_ALIGNMENT);
// else
// tp.setAlignmentX(orientation);
// tp.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
tp.replaceSelection(msg);
}
So, if your goal is to change the alignment, here is your code sample:
package test;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
public class Test {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame("test");
JTextPane testPane = new JTextPane();
testPane.setText("left\nright");
StyledDocument doc = testPane.getStyledDocument();
//left alignment
SimpleAttributeSet myAttributeSet = new SimpleAttributeSet();
StyleConstants.setAlignment(myAttributeSet, StyleConstants.ALIGN_LEFT);
doc.setParagraphAttributes(0, 4, myAttributeSet, false);
//right alignment
StyleConstants.setAlignment(myAttributeSet, StyleConstants.ALIGN_RIGHT);
doc.setParagraphAttributes(5, 9, myAttributeSet, false);
frame.getContentPane().setLayout(new BorderLayout());
frame.getContentPane().add(testPane,BorderLayout.CENTER);
frame.setSize(200,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
If your goal is to change orientation of the text, check this about Bidirectional text.
If your goal is to just write text from right to left, you can just add a DocumentFilter to your document and reverse the logic of your document from there (backward will become forward, the caret will be moved before the insertion instead of after, inserted text will be mirrored, etc)
If you could show more clearly what is your expected result, we would probably be of more help to you.