R
Ross
I can't seem to modify paragraph properties when creating RTF in java.
I can change text to bold, italic, etc. But, if I try to set a
paragraph attribute, e.g. line indent, nothing happens. It's not only
when I display the document in a JEditorPane, the .rtf file I save to
disk doesn't have any line indent either.
What am I doing wrong?
import javax.swing.text.Document;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.rtf.RTFEditorKit;
import java.io.FileOutputStream;
import javax.swing.*;
import java.awt.*;
public class RTFTest extends JFrame
{
private JEditorPane jep;
public RTFTest()
{
setBounds( 50, 50, 500, 500 );
setLayout( new BorderLayout() );
add( jep = new JEditorPane(), BorderLayout.CENTER );
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
jep.setEditable( false );
jep.setContentType( "text/rtf" );
}
public void setDocument( Document d )
{
jep.setDocument( d );
}
public static void main( String args[] ) throws Exception
{
RTFEditorKit aKit = new RTFEditorKit();
RTFTest r2 = new RTFTest();
Document d = aKit.createDefaultDocument();
d.insertString( 0, "Hello world\n", SimpleAttributeSet.EMPTY );
MutableAttributeSet mas = new SimpleAttributeSet();
StyleConstants.setBold( mas, true );
d.insertString( d.getLength(), ".... and this in bold\n", mas );
StyleConstants.setItalic( mas, true );
StyleConstants.setBold( mas, false );
// The following line seems to have no effect. Why?
StyleConstants.setLeftIndent( mas, 150.0F );
d.insertString( d.getLength(), "\nThis is another paragraph which
should be in italic and should be indented a bit", mas );
FileOutputStream out = new FileOutputStream( "test.rtf" );
aKit.write( out, d, 0, d.getLength() );
out.close();
r2.setDocument( d );
r2.setVisible( true );
}
}
I can change text to bold, italic, etc. But, if I try to set a
paragraph attribute, e.g. line indent, nothing happens. It's not only
when I display the document in a JEditorPane, the .rtf file I save to
disk doesn't have any line indent either.
What am I doing wrong?
import javax.swing.text.Document;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.rtf.RTFEditorKit;
import java.io.FileOutputStream;
import javax.swing.*;
import java.awt.*;
public class RTFTest extends JFrame
{
private JEditorPane jep;
public RTFTest()
{
setBounds( 50, 50, 500, 500 );
setLayout( new BorderLayout() );
add( jep = new JEditorPane(), BorderLayout.CENTER );
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
jep.setEditable( false );
jep.setContentType( "text/rtf" );
}
public void setDocument( Document d )
{
jep.setDocument( d );
}
public static void main( String args[] ) throws Exception
{
RTFEditorKit aKit = new RTFEditorKit();
RTFTest r2 = new RTFTest();
Document d = aKit.createDefaultDocument();
d.insertString( 0, "Hello world\n", SimpleAttributeSet.EMPTY );
MutableAttributeSet mas = new SimpleAttributeSet();
StyleConstants.setBold( mas, true );
d.insertString( d.getLength(), ".... and this in bold\n", mas );
StyleConstants.setItalic( mas, true );
StyleConstants.setBold( mas, false );
// The following line seems to have no effect. Why?
StyleConstants.setLeftIndent( mas, 150.0F );
d.insertString( d.getLength(), "\nThis is another paragraph which
should be in italic and should be indented a bit", mas );
FileOutputStream out = new FileOutputStream( "test.rtf" );
aKit.write( out, d, 0, d.getLength() );
out.close();
r2.setDocument( d );
r2.setVisible( true );
}
}