title: How to read plain text file using RTFEditorKit?
body: <p>I am creating a simple Wordpad editor application. I am using JTextPane. I have added code to read '.rtf' file using RTFEditorKit. Initialization code: </p>
  
  <pre><code>RTFEditorKit rtfKit = new RTFEditorKit();
  JTextPane textPane = new JTextPane();
  textPane.setEditorKit(rtfKit);
  </code></pre>
  
  <p>Now I need to read plain text file '.txt' using 'RTFEditorKit' into the same JTextPane, so that I can view plain text file and rtf file in the same application. How can I achieve this?</p>
  
  <p>My application minimal code:</p>
  
  <pre><code>import static java.awt.event.InputEvent.CTRL_DOWN_MASK;
  import java.awt.BorderLayout;
  import java.awt.Dimension;
  import java.awt.Font;
  import java.awt.Insets;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.awt.event.KeyEvent;
  import java.io.BufferedReader;
  import java.io.File;
  import java.io.FileInputStream;
  import java.io.FileReader;
  import java.io.InputStream;
  
  import javax.swing.JFileChooser;
  import javax.swing.JFrame;
  import javax.swing.JLabel;
  import javax.swing.JMenu;
  import javax.swing.JMenuBar;
  import javax.swing.JMenuItem;
  import javax.swing.JOptionPane;
  import javax.swing.JPanel;
  import javax.swing.JScrollPane;
  import javax.swing.JTextPane;
  import javax.swing.KeyStroke;
  import javax.swing.UIManager;
  import javax.swing.filechooser.FileNameExtensionFilter;
  import javax.swing.text.Document;
  import javax.swing.text.MutableAttributeSet;
  import javax.swing.text.Style;
  import javax.swing.text.StyleContext;
  import javax.swing.text.StyledDocument;
  import javax.swing.text.rtf.RTFEditorKit;
  
  public class MyNotepad implements ActionListener {
      public JFrame frame;
      public JPanel panel;
      public JTextPane textPane;
      public RTFEditorKit rtf;
      public StyleContext styleContext;
      public Document document;
      public JScrollPane scrollPane;
      public JMenuBar menuBar;
      public JMenu fileMenu;
      public JMenuItem newSubMenu;
      public JMenuItem openSubMenu;
      public JFileChooser fc;
      public boolean openFileExtFlag = true;
      public boolean saveFileExtFlag = true;
      public File openFile;
      public File saveFile;
      public boolean saveWindowTitle = false;
      public boolean openFileFlag;
      public boolean saveFileFlag;
      public boolean saved = true;
      public boolean dontSaveOption;
      public BufferedReader br;
      public boolean saveForNewOpenExitListener;
      public boolean saveAsFlag;
      public int returnVal;
      public String filePath;
      public boolean flagForOpenListener;
      public StyledDocument styledDocument;
      public Style defaultStyle;
      public MutableAttributeSet mas;
  
      public MyNotepad() {
          try {
              UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
          } catch (Exception e) {
              e.printStackTrace();
          }
          frame = new JFrame("My Notepad");
          panel = new JPanel(new BorderLayout());                                                      
  
          rtf = new RTFEditorKit();
          textPane = new JTextPane();
          textPane.setEditorKit(rtf);
          textPane.setMargin(new Insets(10,5,5,5));
          styleContext = new StyleContext();
          mas = textPane.getInputAttributes();
          styledDocument = textPane.getStyledDocument();
          textPane.setDocument(styledDocument);
  
          scrollPane = new JScrollPane();
          scrollPane.getViewport().add(textPane);
          menuBar = new JMenuBar();
  
          fileMenu = new JMenu("File");
          fileMenu.setMnemonic(KeyEvent.VK_F);
          newSubMenu = new JMenuItem("New");
          newSubMenu.setAccelerator(KeyStroke.getKeyStroke('N', CTRL_DOWN_MASK));
          openSubMenu = new JMenuItem("Open...");
          openSubMenu.setAccelerator(KeyStroke.getKeyStroke('O', CTRL_DOWN_MASK));
  
          defaultStyle = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);
          fc= new JFileChooser();
  
          openSubMenu.addActionListener(this);
          newSubMenu.addActionListener(this);
  
          scrollPane.setPreferredSize(new Dimension(700,500));
  
          fileMenu.add(newSubMenu);
          fileMenu.add(openSubMenu);
  
          menuBar.add(fileMenu);
  
          textPane.setFont(new Font("Arial", Font.PLAIN, 12));
          panel.add(scrollPane, BorderLayout.CENTER);
          panel.add(new JLabel(" "), BorderLayout.EAST);
          panel.add(new JLabel(" "), BorderLayout.WEST);
          panel.add(new JLabel(" "), BorderLayout.SOUTH);
  
          frame.setJMenuBar(menuBar);
          frame.add(panel);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setMinimumSize(new Dimension(900,200));
          frame.pack();
          textPane.requestFocus();                                                
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
      }
  
      public static void main(String[] args) {
          new MyNotepad();
      }
  
      public void actionPerformed(ActionEvent ae) {
          if ((ae.getSource() == openSubMenu)) {      
              openActionListener();
          } 
      }
  
      public boolean openActionListener() {
          if (openFileExtFlag &amp;&amp; saveFileExtFlag) {
              fc.setAcceptAllFileFilterUsed(false);
              fc.addChoosableFileFilter(new FileNameExtensionFilter("Rich Text File (*.rtf)", "rtf"));
              fc.addChoosableFileFilter(new FileNameExtensionFilter("Text File (*.txt)", "txt"));
              openFileExtFlag = false;
          }
  
          do {
              returnVal = fc.showOpenDialog(frame);                               
              if (returnVal == JFileChooser.APPROVE_OPTION) {
                  openFile = fc.getSelectedFile();
                  filePath = openFile.getPath();
                  if (openFile.exists()) {
                      break;
                  }
                  JOptionPane.showMessageDialog(frame, "File not found, please verify the file name and the path", "Cannot open", JOptionPane.OK_OPTION);
              } else {
                  return false;
              }
          } while (true);
  
  
          try {
              System.out.println("---opening document...");
  
              textPane.setText("");
              InputStream in = new FileInputStream(filePath);
              System.out.println("Opening file - " + filePath);
  
              if (filePath.endsWith(".rtf")) {
                  rtf.read(in, textPane.getDocument(), 0);
                  in.close();
              } else if (filePath.endsWith(".txt")) {
                  textPane = new JTextPane();
                  FileReader fileReader = new FileReader(filePath);
                  textPane.read(fileReader, openFile);
                  fileReader.close();
              }
              textPane.requestFocus();
              textPane.setCaretPosition(0);
              frame.setTitle("My Notepad  " + "- " + filePath);
              System.out.println("----File opened successfully");
          } catch (Exception e) {
              JOptionPane.showMessageDialog(frame, "Cannot open, Invalid RTF file", "Cannot open", JOptionPane.OK_OPTION);
          }
          return true;
      }
  }
  </code></pre>