Dear visitor, welcome to Jabaco - Community.
If this is your first visit here, please read the Help. It explains in detail how this page works.
To use all features of this page, you should consider registering.
Please use the registration form, to register here or read more information about the registration process.
If you are already registered, please login here.
Save RTF File
Hi everyone,
I was thinking about saving RTF Files.
But what is wrong?
|
Source code
|
1
2
3
4
5
6
7
8
9
|
Public Sub SaveRTF(RTB As RichTextBox,RTFFile As String)
Dim Fil As New FileOutputStream
Dim Doc As New DefaultStyledDocument(New StyleContext)
Dim Kit As New RTFEditorKit
Fil=New FileOutputStream(RTFFile)
RTB.Parent.setEditorKit(Kit)
RTB.Parent.setDocument(Doc)
Kit.Write(Fil,Doc,0,0)
End Sub
|
It just saves a blank RTF text document.
It wipes the text in the rich text box
Thanks,
klctal
This post has been edited 2 times, last edit by "klctal" (Jul 23rd 2009, 10:46am)
hi klctal,
how would you do it in VB?
regards
OlimilO
hi OlimilO,
I would just do
RichTextBox1.SaveFile(RTFFileName)
But there isn't a SaveFile in the jabaco's RichTextBox.
Thanks,
klctal
Hi,
my suggestion:
|
Source code
|
1
2
3
4
5
6
7
8
9
|
Public Sub SaveRTF(RTB As RichTextBox, RTFFileName As String)
Dim fos As New FileOutputStream(New File(RTFFileName))
Dim Doc As Document = RTB.Parent.getDocument
Dim kit As EditorKit = RTB.Parent.getEditorKit
Call kit.Write(fos, Doc, 0,0)
fos.close
End Sub
|
You might want to add an "option explicit" as first line of your code to help detecting/preventing errors.
Cheers!
A1880
Hi A1880,
I have tried your source code but there was some bugs. I changed it into this:
|
Source code
|
1
2
3
4
5
6
7
8
9
10
|
Public Sub SaveRTF(RTB As RichTextBox, RTFFileName As String)
Dim fos As New FileOutputStream
fos=New FileOutPutStream(RTFFileName)
Dim Doc As Document
Doc=RTB.Parent.getDocument
Dim kit As EditorKit
kit = RTB.Parent.getEditorKit
Call kit.Write(fos, Doc, 0,0)
fos.close
End Sub
|
Now there was no bugs
but it only would show the common dialog(as I added) a lot of times.
I think it doesn't know the file name (RTFFileName)
Thanks,
klctal
Hi,
my version compiles free of bugs in my Jabaco 1.4.2.
What kind of bugs did you see?
Make sure that you use a full path for your file name. Sample: "c:\myFiles\test.rtf".
Otherwise, you have to rely on your program to be executed in the corrrect working directory.
Success!
A1880
Hi A1880,
There still is a bug on my computer!
The box at the down right corner (in red) says this:
Listening for transport dt_shmem at address: Jabaco33119813x165
Exception in thread "AWT-EventQueue-1" java.lang.InstantiationError: javax.swing.text.EditorKit
at Form1.SaveRTF(Form1.jsrc:220)
at Form1.Command1_Click(Form1.jsrc:22)
at Form1$CommandButton._Click(Form1.jsrc:285)
at VB.CommandButton.actionPerformed(CommandButton.jsrc:96)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at VBA.JabacoEventQueque.dispatchEvent(JabacoEventQueque.java:20)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
It's a little weird.
Thanks,
klctal
Hi,
if you "save" your RichTextBox before it has an EditorKit attached to ist, this exception can happen.
Please consult the LoadRTF() code how to attach a proper RTFEditorKit.
If you "load" before you "save", everything should be OK.
I am no expert in Java Swing. If I look at the Swing samples around the Internet, I'd rather leave it that way!
It could be one of the big merits of Jabaco to hide away the Swing secrets from ordinary developers.
Cheers!
A1880
Hi A1880,
This time, I succeeded.
But it only saves a open document. But thanks. Your source was helpful.
klctal