You are not logged in.

klctal

Trainee

  • "klctal" is male
  • "klctal" started this thread

Posts: 70

Date of registration: Jul 13th 2009

Location: Shanghai

Hobbies: Developing software

  • Send private message

1

Thursday, July 23rd 2009, 10:39am

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
:D

This post has been edited 2 times, last edit by "klctal" (Jul 23rd 2009, 10:46am)


OlimilO

Intermediate

  • "OlimilO" is male

Posts: 277

Date of registration: Jan 18th 2009

Location: Germany

Occupation: software engineer

  • Send private message

2

Thursday, July 23rd 2009, 10:49am

hi klctal,



how would you do it in VB?



regards

OlimilO

klctal

Trainee

  • "klctal" is male
  • "klctal" started this thread

Posts: 70

Date of registration: Jul 13th 2009

Location: Shanghai

Hobbies: Developing software

  • Send private message

3

Thursday, July 23rd 2009, 10:54am

hi OlimilO,

I would just do

RichTextBox1.SaveFile(RTFFileName) :D

But there isn't a SaveFile in the jabaco's RichTextBox.



Thanks,

klctal
:D

A1880

Intermediate

  • "A1880" is male

Posts: 500

Date of registration: Jan 1st 2009

Location: Hanover, Germany

Occupation: Software Engineer

Hobbies: Hilbert Curves

  • Send private message

4

Thursday, July 23rd 2009, 12:12pm

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

klctal

Trainee

  • "klctal" is male
  • "klctal" started this thread

Posts: 70

Date of registration: Jul 13th 2009

Location: Shanghai

Hobbies: Developing software

  • Send private message

5

Thursday, July 23rd 2009, 12:57pm

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
:D

A1880

Intermediate

  • "A1880" is male

Posts: 500

Date of registration: Jan 1st 2009

Location: Hanover, Germany

Occupation: Software Engineer

Hobbies: Hilbert Curves

  • Send private message

6

Thursday, July 23rd 2009, 1:12pm

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

klctal

Trainee

  • "klctal" is male
  • "klctal" started this thread

Posts: 70

Date of registration: Jul 13th 2009

Location: Shanghai

Hobbies: Developing software

  • Send private message

7

Thursday, July 23rd 2009, 1:39pm

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
:D

A1880

Intermediate

  • "A1880" is male

Posts: 500

Date of registration: Jan 1st 2009

Location: Hanover, Germany

Occupation: Software Engineer

Hobbies: Hilbert Curves

  • Send private message

8

Thursday, July 23rd 2009, 2:00pm

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

klctal

Trainee

  • "klctal" is male
  • "klctal" started this thread

Posts: 70

Date of registration: Jul 13th 2009

Location: Shanghai

Hobbies: Developing software

  • Send private message

9

Thursday, July 23rd 2009, 2:40pm

Hi A1880,

This time, I succeeded.

But it only saves a open document. But thanks. Your source was helpful.



klctal
:D

Dani

Intermediate

Posts: 325

Date of registration: Nov 19th 2009

Location: GERMANY

  • Send private message
Rate this thread
WoltLab Burning Board