Re: RTFEditorKit doesn't
To: comp.lang.java.gui
In article <491a6bc9$0$28826$b9f67a60@news.newsdemon.com>,
Knute Johnson <nospam@rabbitbrush.frazmtn.com> wrote:
John B. Matthews wrote:
In article <491a4608$0$28825$b9f67a60@news.newsdemon.com>,
Knute Johnson <nospam@rabbitbrush.frazmtn.com> wrote:
Anybody know how to use an RTFEditorKit and get it to save the
alignment when you write out the data? If I save a file from my
RTFEditorKit all alignment data is lost and everything is left
aligned.
I don't know RTF, but I'll be glad to try opening your output. Can you
tell if the alignment tags are missing or malformed?
[...]
Apparently the RTFEditorKit doesn't put alignment tags into the output
so I guess that is going nowhere.
It looks like the StyledEditorKit.AlignmentAction nested in RTFEditorKit
is _supposed_ to call setParagraphAttributes() on the StyledDocument.
FWIW, I added alignment commands to createStyleMenu() in the demo, and
they seem to work. Sadly, the RTFEditorKit only sets \ql, never \qc or \qr:
<http://java.sun.com/docs/books/tutorial/uiswing/examples/zipfiles/componen
ts-TextComponentDemoProject.zip>
<diff -u>
--- ../../TextComponentDemo.old.java 2008-11-12 12:24:38.000000000 -0500
+++ TextComponentDemo.java 2008-11-12 12:47:10.000000000 -0500
@@ -37,11 +37,13 @@
import java.awt.*;
import java.awt.event.*;
+import java.io.IOException;
import java.util.HashMap;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.event.*;
+import javax.swing.text.rtf.RTFEditorKit;
import javax.swing.undo.*;
public class TextComponentDemo extends JFrame {
@@ -73,7 +75,7 @@
System.exit(-1);
}
JScrollPane scrollPane = new JScrollPane(textPane);
- scrollPane.setPreferredSize(new Dimension(200, 200));
+ scrollPane.setPreferredSize(new Dimension(400, 200));
//Create the text area for the status log and configure it.
changeLog = new JTextArea(5, 30);
@@ -194,6 +196,14 @@
((changeLength == 1) ? ". " : "s. ") +
" Text length = " + document.getLength() +
"." + newline);
+ RTFEditorKit kit = new RTFEditorKit();
+ try {
+ kit.write(System.out, document, 0, document.getLength());
+ } catch (IOException ex) {
+ ex.printStackTrace(System.err);
+ } catch (BadLocationException ex) {
+ ex.printStackTrace(System.err);
+ }
}
}
@@ -247,7 +257,24 @@
protected JMenu createStyleMenu() {
JMenu menu = new JMenu("Style");
- Action action = new StyledEditorKit.BoldAction();
+ Action action = new StyledEditorKit.AlignmentAction(
+ "Left", StyleConstants.ALIGN_LEFT);
+ action.putValue(Action.NAME, "Left");
+ menu.add(action);
+
+ action = new StyledEditorKit.AlignmentAction(
+ "Center", StyleConstants.ALIGN_CENTER);
+ action.putValue(Action.NAME, "Center");
+ menu.add(action);
+
+ action = new StyledEditorKit.AlignmentAction(
+ "Right", StyleConstants.ALIGN_RIGHT);
+ action.putValue(Action.NAME, "Right");
+ menu.add(action);
+
+ menu.addSeparator();
+
+ action = new StyledEditorKit.BoldAction();
action.putValue(Action.NAME, "Bold");
menu.add(action);
@@ -322,7 +349,8 @@
StyleConstants.setItalic(attrs[2], true);
attrs[3] = new SimpleAttributeSet(attrs[0]);
- StyleConstants.setFontSize(attrs[3], 20);
+ StyleConstants.setForeground(attrs[3], Color.blue);
+ StyleConstants.setAlignment(attrs[3], StyleConstants.ALIGN_LEFT);
attrs[4] = new SimpleAttributeSet(attrs[0]);
StyleConstants.setFontSize(attrs[4], 12);
@@ -427,8 +455,8 @@
SwingUtilities.invokeLater(new Runnable() {
public void run() {
//Turn off metal's use of bold fonts
- UIManager.put("swing.boldMetal", Boolean.FALSE);
- createAndShowGUI();
+ //UIManager.put("swing.boldMetal", Boolean.FALSE);
+ createAndShowGUI();
}
});
}
</dif -u>
I've tried playing with HTMLEditorKit but I'm having alignment problems
with that too.
Table alignment renders OK, but I've never tried to create a document:
<http://robotchase.svn.sourceforge.net/viewvc/robotchase/trunk/help.html?re
vision=37&view=markup>
I'm a little out of my element with this.
Same here.
--
John B. Matthews
trashgod at gmail dot com
http://home.roadrunner.com/~jbmatthews/
---
* Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24