Java Swing load file into JEditorPane

I am trying to write a text editor type application in Java / Swing. I have FileChooser running and I can print the contents of the file to the console. I want to upload a file to JEditorPane

When I call setText (), it updates the text value (I can print the getText result, but the actual Editor pane is not updated). I've tried renaming / revalidate to a JEditorPane encapsulating a JScrollPane, but nothing will update the text to what I sent to setText.

Did I miss something?

PS The JEditorPane is wrapped inside a JScrollPane and I have a method in my mainEditor that passes a string to the setText method for the JEditorPane.

      if (r == JFileChooser.APPROVE_OPTION) 
      {
          FileInputStream fis;
          BufferedReader br;
          try
            {
                fis = new FileInputStream( 
                      chooser.getSelectedFile() ) ;
                br  = new BufferedReader( 
                      new InputStreamReader( fis ) ) ;
                String read ;
                StringBuffer text = new StringBuffer() ;
                while( ( read = br.readLine() ) != null ) 
                {
                   text.append( read ).append( "\n" ) ;
                }
                Main.frame.mainEditor.setText( text.toString() ) ;
                Main.frame.mainEditor.revalidate();
            }
            catch( IOException e1 ) 
            {
                JOptionPane.showMessageDialog( this , 
                    "Error in File Operation" ,
                    "Error in File Operation" , 
                    JOptionPane.INFORMATION_MESSAGE) ;
            }             
      }

      

0


source to share


2 answers


Compose String

to a link File

, convert it to URL

, then call setPage(URL)

.

See here for an example.



IUXmd.png

+4


source


use built-in methods on InputStream for JTextComponents family

JTextCompoents # read ();



JTextComponents # write ();

+3


source







All Articles