I want to force linewrap in a jtextpane which contains arabic text. Although linewrap is automatically enabled my jtextpane won't linewraps the text.
import java.awt.*;
import javax.swing.*;
import javax.swing.GroupLayout.Alignment;
import javax.swing.text.*;
import static javax.swing.ScrollPaneConstants.*;
public class LineWrapTest {
private JFrame frame = new JFrame();
private String text1 =" أكرة القدم هي رياضة جماعية تلعب بين فريقين يتكون كل منهما من 11 لاعب بكرة مُكوَّرة. يلعب كرة القدم 250 مليون لاعب في أكثر من 200 دولة حول العالم، فهي بهذا الرياضة الأكثر شعبية وانتشاراً في العالم\n";
private String text2 ="لذي يحرز أهدافاً أكثر يكون هو الفائز. إذا أحرز الفريقان أهدافاً متعادلة في نهاية المباراة، فتكون نتيجة المباراة إما التعادل أو تدخل المباراة في نظام الوقت الإضافي و/أو الضربات الترجيحية ويعتمد ذلك على نظام البطولة. وضعت قوانين ً\n";
private String text3 ="كرة القدم في سنة 1863. يترأس لعبة كرة القدم دولياً الإتحاد الدولي لكرة القدم (الفيفا). تنظم بطولة كأس عالم لهذه الرياضة مر";
private Document doc = null;
private JTextPane textPane = new JTextPane();
private JScrollPane scrollPane = null;
private JPanel pane1 = new JPanel();
private JPanel pane2 = new JPanel();
LineWrapTest() throws BadLocationException {
pane1.setBackground(Color.blue);
pane2.setBackground(Color.green);
pane1.setPreferredSize(new Dimension(10,10));
pane2.setPreferredSize(new Dimension(10,10));
StyleContext context = new StyleContext();
Style style = context.getStyle(StyleContext.DEFAULT_STYLE);
StyleConstants.setAlignment(style, StyleConstants.ALIGN_RIGHT);
StyleConstants.setFontSize(style, 14);
StyleConstants.setSpaceAbove(style, 40);
StyleConstants.setSpaceBelow(style, 4);
doc = new DefaultStyledDocument(context);
//generate some text
for(int i=0;i<10;i++) {
doc.insertString(doc.getLength(), text1, style);
doc.insertString(doc.getLength(), text2, style);
doc.insertString(doc.getLength(), text3, style);
}
textPane.setDocument(doc);
//make textpane scrollable
scrollPane = new JScrollPane(textPane);
createAndShowFrame();
}
private void createAndShowFrame() {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(new Dimension(200,200));
frame.add(scrollPane, BorderLayout.CENTER);
frame.add(new JButton("north"), BorderLayout.NORTH);
frame.add(new JButton("south"), BorderLayout.SOUTH);
frame.add(pane1, BorderLayout.WEST);
frame.add(pane2, BorderLayout.EAST);
frame.setVisible(true);
}
public static void main (String [] args) throws BadLocationException {
new LineWrapTest();
}}
Any ideas of how to force my jtextpane to linewrap the text? Where is the mistake?
Try to enable Right-to-Left orientation in the text component; without this Spring refuses to wrap the text because it would wrap on the wrong side:
textPane.setComponentOrientation( ComponentOrientation.RIGHT_TO_LEFT )
If you need to mix RTL and LTR text, see here: https://docs.oracle.com/javase/tutorial/i18n/text/bidi.html