OneMoreError
OneMoreError

Reputation: 7728

Using JEditorPane to open a text file

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);
}
}

Upvotes: 1

Views: 5346

Answers (3)

Reimeus
Reimeus

Reputation: 159764

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.

Upvotes: 6

Andreas Fester
Andreas Fester

Reputation: 36630

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);

}

Upvotes: 1

StanislavL
StanislavL

Reputation: 57381

editorpane.getEditorKit().read(filereader, editorpane.getDocument(), 0);

Upvotes: 2

Related Questions