views:

182

answers:

1

i have the following code trying to save the contents of a JTextPane as RTF. although a file is created in the following code but it is empty!

any tips regarding what am i doing wrong? (as usual dont forget im a beginner!)

                if (option == JFileChooser.APPROVE_OPTION) {


                ////////////////////////////////////////////////////////////////////////
                //System.out.println(chooser.getSelectedFile().getName());

                //System.out.println(chooser.getSelectedFile().getAbsoluteFile());
                ///////////////////////////////////////////////////////////////////////////


                StyledDocument doc = (StyledDocument)textPaneHistory.getDocument();

                RTFEditorKit kit = new RTFEditorKit();

                BufferedOutputStream out;

                try {
                    out = new BufferedOutputStream(new FileOutputStream(chooser.getSelectedFile().getName()));

                    kit.write(out, doc, doc.getStartPosition().getOffset(), doc.getLength());

                } catch (FileNotFoundException e) {

                } catch (IOException e){

                } catch (BadLocationException e){

                }

            }

EDIT: HTMLEditorKit if i use HTMLEditorKit it works and thats what i really wanted. SOLVED!

+1  A: 
            if (textPaneHistory.getText().length() > 0){

            JFileChooser chooser = new JFileChooser();
            chooser.setMultiSelectionEnabled(false);

            int option = chooser.showSaveDialog(ChatGUI.this);

            if (option == JFileChooser.APPROVE_OPTION) {

                StyledDocument doc = (StyledDocument)textPaneHistory.getDocument();

                HTMLEditorKit kit = new HTMLEditorKit();

                BufferedOutputStream out;

                try {
                    out = new BufferedOutputStream(new FileOutputStream(chooser.getSelectedFile().getAbsoluteFile()));

                    kit.write(out, doc, doc.getStartPosition().getOffset(), doc.getLength());

                } catch (FileNotFoundException e) {

                } catch (IOException e){

                } catch (BadLocationException e){

                }
            }
        }

here is the solution. it works if HTMLEditorKit is used.

iEisenhower