Article View: borland.public.jbuilder.java.swing
Article #9673JTextPane with Alignment Attributes not written correctly into RTF File
From: Laurent Kubanski
Date: Mon, 14 Feb 2000 00:00
Date: Mon, 14 Feb 2000 00:00
128 lines
5001 bytes
5001 bytes
Hi,
The following java code source produces a JTextPane and writes it to a RTF
file.
In the JTextPane there are Alignment Attributes.
In the written RTF file the Alignment Attributes is loss.
Thanks for your help.
Best Regards.
Laurent
JAVA SOURCE CODE :
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.text.rtf.*;
public class MyFrame extends JFrame implements ActionListener
{
public JTextPane myTextPane;
public MyFrame()
{
super("MyFrame");
getContentPane().setLayout(new BorderLayout());
JPanel myPanel = new JPanel(new FlowLayout());
getContentPane().add(BorderLayout.SOUTH, myPanel);
Button myButton = new Button("Create RTF File");
myButton.addActionListener(this);
myPanel.add(myButton);
myTextPane = new JTextPane();
myTextPane.setContentType("text/rtf");
StyleContext myStyleContext = new StyleContext();
try
{
Style myStyle1 = myStyleContext.addStyle(null, null);
StyleConstants.setFontFamily(myStyle1, "dialog");
StyleConstants.setFontSize(myStyle1, 14);
StyleConstants.setBold(myStyle1, true);
StyleConstants.setForeground(myStyle1, Color.red);
myTextPane.getStyledDocument().insertString(myTextPane.getStyledDocument().g
etLength(), "dialog 14 Bold Red Left", myStyle1);
myTextPane.getStyledDocument().insertString(myTextPane.getStyledDocument().g
etLength(), "\n", null);
myTextPane.select(0, 0);
MutableAttributeSet myMutableAttributeSet1 = new
SimpleAttributeSet();
StyleConstants.setAlignment(myMutableAttributeSet1, 0);
myTextPane.setParagraphAttributes(myMutableAttributeSet1, true);
Style myStyle2 = myStyleContext.addStyle(null, null);
StyleConstants.setFontFamily(myStyle2, "serif");
StyleConstants.setFontSize(myStyle2, 16);
StyleConstants.setItalic(myStyle2, true);
StyleConstants.setForeground(myStyle2, Color.blue);
myTextPane.getStyledDocument().insertString(myTextPane.getStyledDocument().g
etLength(), "serif 16 Italic Blue Center", myStyle2);
myTextPane.getStyledDocument().insertString(myTextPane.getStyledDocument().g
etLength(), "\n", null);
myTextPane.select(30, 30);
MutableAttributeSet myMutableAttributeSet2 = new
SimpleAttributeSet();
StyleConstants.setAlignment(myMutableAttributeSet2, 1);
myTextPane.setParagraphAttributes(myMutableAttributeSet2, true);
Style myStyle3 = myStyleContext.addStyle(null, null);
StyleConstants.setFontFamily(myStyle3, "dialoginput");
StyleConstants.setFontSize(myStyle3, 18);
StyleConstants.setUnderline(myStyle3, true);
StyleConstants.setForeground(myStyle3, Color.green);
myTextPane.getStyledDocument().insertString(myTextPane.getStyledDocument().g
etLength(), "dialoginput 18 Underline Green Right", myStyle3);
myTextPane.getStyledDocument().insertString(myTextPane.getStyledDocument().g
etLength(), "\n", null);
myTextPane.select(60, 60);
MutableAttributeSet myMutableAttributeSet3 = new
SimpleAttributeSet();
StyleConstants.setAlignment(myMutableAttributeSet3, 2);
myTextPane.setParagraphAttributes(myMutableAttributeSet3, true);
}
catch (BadLocationException exc) {}
getContentPane().add(BorderLayout.CENTER, new
JScrollPane(myTextPane));
setBounds(20, 20, 500, 300);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
try
{
RTFEditorKit myRTFEditorKit = new RTFEditorKit();
StyledDocument myStyledDocument =
myTextPane.getStyledDocument();
FileOutputStream myFileOutputStream = new
FileOutputStream("MyFile.rtf");
myRTFEditorKit.write(myFileOutputStream, myStyledDocument, 0,
myStyledDocument.getLength()-1);
myFileOutputStream.close();
}
catch (Exception exc) {}
}
public static void main(String arg[])
new MyFrame();
}
}
Technical Details
Message-ID:
<888v8g$hlt$1@minus.oleane.net>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 111d45,a64a87f3d33a8ed4,start X-Google-Attributes: gid111d45,public From: "Laurent Kubanski" <lkubanski@quartz-system.fr> Subject: JTextPane with Alignment Attributes not written correctly into RTF File Date: 2000/02/14 Message-ID: <888v8g$hlt$1@minus.oleane.net> X-Deja-AN: 585718943 X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3612.1700 X-Complaints-To: abuse@oleane.net X-Trace: minus.oleane.net 950534224 18109 62.160.166.95 (14 Feb 2000 13:17:04 GMT) Organization: Guest of OLEANE NNTP-Posting-Date: 14 Feb 2000 13:17:04 GMT Newsgroups: borland.public.jbuilder.java.swing