0

I am using JTextPane with StyledDocument, is it possible to set length of its paragraph? My goal is when the length of text which is inserted in the JTextPane is longer than 400px, exceed go to new line.

Edit:

I use method below to set style for textPane

public Style getstyle(String message){
    Style style = context.addStyle("mystyle", null);
    StyleConstants.setForeground(style, Color.GRAY);
    StyleConstants.setBackground(style, new Color(162, 234, 167));
    SimpleAttributeSet mystyle = new SimpleAttributeSet();
    StyleConstants.setFontFamily(style, "SansSerif");
    document.setParagraphAttributes(document.getLength(), message.length(), mystyle, false);
    return style;
}

and use following to insert text to my JTextPane:

try {
    document.insertString(document.getLength(), " "+message+"\n", getStyle(message));
} catch (BadLocationException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

I want to set length of paragraph.

Edit2:

I use different style, for different type of my text.

Edit3:

I add my JTextPane to JPanel with scrollbar.

context = new StyleContext();
document = new DefaultStyledDocument(context);
Pane = new JTextPane(document);
Pane.setEditable(false);

Panel.setLayout(new BoxLayout(Panel, BoxLayout.PAGE_AXIS));

JScrollPane scroll = new JScrollPane(Pane);
Panel.add(scroll , BorderLayout.CENTER);
user90
  • 73
  • 3
  • 13

1 Answers1

0

Try this:

public static void main(String[] args) throws Exception {
    final JTextPane pane = new JTextPane();
    pane.setContentType("text/html");
    final JFrame frm = new JFrame("Text editor");
    pane.setText("<html><table><tr><td width=\"400\"></td></tr></table</html>");
    frm.add(new JScrollPane(pane));
    frm.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frm.setSize(500, 500);
    frm.setVisible(true);
}

Your text will go to new line after 400px

Sergiy Medvynskyy
  • 11,160
  • 1
  • 32
  • 48
  • If I set StyledDocument to be HTML, I couldn't use my style which I define for each type of my text. – user90 Aug 08 '14 at 08:33
  • @user90 You can define your style using HTML notation. – Sergiy Medvynskyy Aug 08 '14 at 08:34
  • `..` 400 what? Characters, pixels, ..? – Andrew Thompson Aug 08 '14 at 09:27
  • @Sergiy Medvynskyy also I add some component to my JTextPane which their alignment should set to right or left depends on their type. How can I set their alignment by set content type to html? – user90 Aug 08 '14 at 09:30
  • @AndrewThompson I think px is default. – Sergiy Medvynskyy Aug 08 '14 at 09:56
  • 1
    Nope. According the the W3C a width is either `px` or `em` or a `%` (or invalid). And we should not trust 'defaults' in the Swing toolkit. E.G. `JFrame` used to default to `FlowLayout`. That really stuffed up a lot of apps. when Sun decided to ***change*** the default layout to **`BorderLayout`**. – Andrew Thompson Aug 08 '14 at 10:12
  • appearance of css style is different in JTextPane. (color, alignment). how can I set diffrent alignment? – user90 Aug 08 '14 at 10:32