0

I am trying to open a text file in a frame using JEditorPane (in an non-editable mode). However, I believe I am having problems with setting up my input stream and output stream. Kindly, look at my code and tell where I am doing wrong.


import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class TextEditor extends JFrame{

private JEditorPane editorpane;
JScrollPane editorScrollPane;
String filename="D:\\abc.txt";
Reader filereader;

public TextEditor()
{       
        editorpane= new JEditorPane();
        editorpane.setEditable(false);

        if (filename != null) 
        {
            try 
            {
                filereader=new FileReader(filename);
                editorpane.setPage(filename);
            }

            catch (IOException e) 
            {
                System.err.println("Attempted to read a bad file " + filename);
            }
         }

        else
        {
            System.err.println("Couldn't find file");
        }

        //Put the editor pane in a scroll pane.
        editorScrollPane = new JScrollPane(editorpane);
            editorScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        editorScrollPane.setPreferredSize(new Dimension(250, 145));
        editorScrollPane.setMinimumSize(new Dimension(10, 10));

}

public static void main(String[] args) 
{
    TextEditor obj= new TextEditor();
    obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    obj.setSize(600,600);
    obj.setLocation(100,100);
    obj.setVisible(true);
}
}
OneMoreError
  • 7,518
  • 20
  • 73
  • 112
  • 1
    you should rather print the message of the IOException. This will give you a true hint on what is going on: Access denied? File not found? Disk error? – Guillaume Polet Sep 18 '12 at 14:01
  • Thanks for this tip. Now the error message is : java.io.FileNotFoundException: file:\D:\abc.txt (The filename, directory name, or volume label syntax is incorrect) – OneMoreError Sep 18 '12 at 14:07
  • Try "file:///D:/abc.txt" – Andreas Fester Sep 18 '12 at 14:11
  • possible duplicate of [Opening a text file in a frame using swing components](http://stackoverflow.com/questions/12477579/opening-a-text-file-in-a-frame-using-swing-components) – trashgod Sep 18 '12 at 14:12

3 Answers3

5

To get the argument as a URL for JEditorPane.setPage, you could use:

File file = new File(filename);
editorpane.setPage(file.toURI().toURL());

Also don't forget to add your JEditorPane to the frame so that it can be seen:

add(editorScrollPane);

To view the disk error you're getting add:

e.printStackTrace();

to the IOException block.

Reimeus
  • 158,255
  • 15
  • 216
  • 276
2
editorpane.getEditorKit().read(filereader, editorpane.getDocument(), 0);
StanislavL
  • 56,971
  • 9
  • 68
  • 98
1

JEditorPane.setPage() expects an URL, including the protocol: try "file:///D:/abc.txt". The following code should work:

String filename="file:///D:/abc.txt";

public TextEditor()
{
        editorpane= new JEditorPane();
        editorpane.setEditable(false);

        if (filename != null)
        {
            try
            {
                editorpane.setPage(filename);
            }

            catch (IOException e)
            {
                System.err.println("Attempted to read a bad file " + filename );
                e.printStackTrace();
            }
         }

        else
        {
            System.err.println("Couldn't find file");
        }

        //Put the editor pane in a scroll pane.
        editorScrollPane = new JScrollPane(editorpane);
            editorScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        editorScrollPane.setPreferredSize(new Dimension(250, 145));
        editorScrollPane.setMinimumSize(new Dimension(10, 10));
      getContentPane().add(editorScrollPane);

}
Andreas Fester
  • 36,091
  • 7
  • 95
  • 123
  • Even on doing as you said, still there is the same error message : Attempted to read a bad file file:\D:\abc.txt – OneMoreError Sep 18 '12 at 13:55
  • You also need to remove the creation of the FileReader() - you do not use it anyway, and it will throw an IOError since it expects a pure filename, not an URL – Andreas Fester Sep 18 '12 at 14:10
  • Finally, you also need to add `getContentPane().add(editorScrollPane);` at the end of the TextEditor constructor, otherwise you will not see anything... – Andreas Fester Sep 18 '12 at 14:14