Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: refreshing JEditorPane

  1. #1
    Member
    Join Date
    Mar 2010
    Posts
    111
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default refreshing JEditorPane

    I need to refresh the content of a JEditor Pane multiple times, I am using the following code for this purpose
    java.net.URL Content =  file.toURI().toURL();
    		    	if (Content != null) {
    					tArea[i].setPage(Content);
    					i++;		
    					}
    but it only loads "Content" to my JEditorPane for the first time and it doesn't work properly any more althogh when I check the "Content", it is refreshed.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: refreshing JEditorPane

    My guess is the problem lies outside the code you posted. Where are you calling this code: from a loop? In the event dispatching thread?

  3. #3
    Member
    Join Date
    Mar 2010
    Posts
    111
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: refreshing JEditorPane

    I call it in a while loop which is in a method as a part of mouse listener class.

  4. #4
    Member
    Join Date
    Mar 2010
    Posts
    111
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: refreshing JEditorPane

    this is the loop

     
    File file = new File("result.html");
    while(r>0 & r<l & e>0){
     
    		    	fos = new FileOutputStream(file);
    		    	dos = new DataOutputStream(fos);
    		    	dos.writeBytes(Results.substring(r, e));
    		    	java.net.URL Content =  file.toURI().toURL();
    		       	if (Content != null) {
    					tArea[i].setPage(Content);
    					i++;		
    					}
     
    		    	r = Results.indexOf("<a href=",e);
    		    	e = Results.indexOf("Cached",r);
    		    }

    I am trying to write the content of "result.html" file on myjeditorpane

  5. #5
    Member
    Join Date
    Mar 2010
    Posts
    111
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: refreshing JEditorPane

    I solved the problem by clearing the stream description property but now I got this exception

    Exception in thread "Thread-14" java.lang.NullPointerException
    	at javax.swing.JEditorPane$PageLoader.run(Unknown Source)

    what is the reason?

  6. #6
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: refreshing JEditorPane

    I have moved this thread to the correct forum -

    AWT / Java Swing - Java Programming Forums
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  7. #7
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: refreshing JEditorPane

    Hello nasi,

    Can you please post the full version of your code? We can attempt to compile it and generate the error then..
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  8. #8
    Member
    Join Date
    Mar 2010
    Posts
    111
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: refreshing JEditorPane

    it is multiple classes, I can't post all of it. the following is the code that generates this thread

    while(r>0 & r<l & e>0){
     
    		    	dos.writeBytes(Results.substring(r, e));
    		    	java.net.URL Content =  file.toURI().toURL();
    		    	tArea[i].setPage(Content);
     
    				r = Results.indexOf("<a href=",e);
    		    	e = Results.indexOf("Cached",r);
    		    	i++;
    		    }
    		    for (int j=0; j<10; j++){
    		    	Document doc = tArea[j].getDocument();
    		    	doc.putProperty(Document.StreamDescriptionProperty, null);
    		    }

    I need to clear the stream description property to be able to reload my jeditorpane but it causes a NullPointerException.

  9. #9
    Member
    Join Date
    Mar 2010
    Posts
    111
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: refreshing JEditorPane

    This is similar to what I am doing and generates nullPointer thread. more over it doesn't write the Content to my JEditorPane (tArea) properly

    package test;
     
    import java.io.DataOutputStream;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.UnsupportedEncodingException;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.URLConnection;
    import java.net.URLEncoder;
     
    import javax.swing.JEditorPane;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.text.Document;
     
     
    public class test extends JFrame{
    	/**
    	 * 
    	 */
     
    	private String query="university" ;
    	private String collection = " site:wikipedia.org";
    	private String searchEngine = "http://www.google.com";
    	private StringBuilder Results = new StringBuilder();
    	private JEditorPane[] tArea = new JEditorPane[10];
    	private File file ;
    	private FileOutputStream fos;
    	private DataOutputStream dos;
    	private JPanel panel = new JPanel();
     
    	public  test(){
     
    		for (int y=0;y<10;y++){
    			tArea[y]= new JEditorPane();
    			panel.add(tArea[y]);
    		}
     
    		this.add(panel);
    		URL url = null;
    		try {
    			url = new URL(searchEngine + "/search?hl=en&source=hp&q="+  URLEncoder.encode(query + collection, "UTF-8")+ "&meta=&aq=f&aqi=g10&aql=&oq=&gs_rfai=" );
    		} catch (MalformedURLException e1) {
    			// TODO Auto-generated catch block
    			e1.printStackTrace();
    		} catch (UnsupportedEncodingException e1) {
    			// TODO Auto-generated catch block
    			e1.printStackTrace();
    		}
    	    URLConnection Connection = null;
    		try {
    			Connection = (URLConnection)url.openConnection();
    		} catch (IOException e1) {
    			// TODO Auto-generated catch block
    			e1.printStackTrace();
    		}
    	    Connection.setRequestProperty("User-Agent", "Mozilla 5.0");
    		    InputStream inputStream = null;
    			try {
    				inputStream = Connection.getInputStream();
    			} catch (IOException e1) {
    				// TODO Auto-generated catch block
    				e1.printStackTrace();
    			}
     
    		    int ch=0; 
    		    Results.delete(0, Results.length());
     
    	    try {
    			while ((ch=inputStream.read()) > 0) {
    			    	Results.append((char)ch);
    			}
    		} catch (IOException e1) {
    			// TODO Auto-generated catch block
    			e1.printStackTrace();
    		}
    	//    System.out.println(Results);
    	   	int l = Results.lastIndexOf("<a href="/ last occurrence of <a href
    	    int f = Results.indexOf("Search Results");//beginning of search results
    	    int r = Results.indexOf("<a href=",f);//first <a href after f
    	    int e = Results.indexOf("Cached",r);//first similar(end of results of one link) after r
    	    int i=0;
     
     
    	    while(r>0 & r<l & e>0){
    	    	file = new File("result.html");
    			try {
    				fos = new FileOutputStream(file);
    			} catch (FileNotFoundException e1) {
    				// TODO Auto-generated catch block
    				e1.printStackTrace();
    			}
    			dos = new DataOutputStream(fos);
    			try {
    				dos.flush();
    			} catch (IOException e1) {
    				// TODO Auto-generated catch block
    				e1.printStackTrace();
    			}
    			try {
    				dos.writeBytes(Results.substring(r, e));
    			} catch (IOException e1) {
    				// TODO Auto-generated catch block
    				e1.printStackTrace();
    			}	
    			try {
    				fos.close();
    			} catch (IOException e1) {
    				// TODO Auto-generated catch block
    				e1.printStackTrace();
    			}
    		    try {
    				dos.close();
    			} catch (IOException e1) {
    				// TODO Auto-generated catch block
    				e1.printStackTrace();
    			}
    	    	java.net.URL Content = null;
    			try {
    				Content = file.toURI().toURL();
    			} catch (MalformedURLException e1) {
    				// TODO Auto-generated catch block
    				e1.printStackTrace();
    			}
    	    	System.out.println(Results.substring(r, e));
    	    	try {
    				tArea[i].setPage(Content);
    			} catch (IOException e1) {
    				// TODO Auto-generated catch block
    				e1.printStackTrace();
    			}
    	    	r = Results.indexOf("<a href=",e);
    	    	e = Results.indexOf("Cached",r);
    	    	i++;
    	    	file.delete();
     
    	    }
     
    	    for (int j=0; j<10; j++){
    	    	Document doc = tArea[j].getDocument();
    	    	doc.putProperty(Document.StreamDescriptionProperty, null);
    	    } 
     
     
     
    	}
    public static void main(String[] args) {
     
    		JFrame frame= new test();
     
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.pack();
    		frame.setVisible(true);  
    }
     
    }

  10. #10
    Member
    Join Date
    Mar 2010
    Posts
    111
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: refreshing JEditorPane

    no answer

Similar Threads

  1. writting to jeditorpane
    By nasi in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 5th, 2010, 05:49 AM
  2. create buttons in a JEditorPane
    By seeker in forum AWT / Java Swing
    Replies: 4
    Last Post: December 5th, 2009, 09:01 AM
  3. Convert contents of JTextArea / JEditorPane to PDF
    By rangarajank in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: September 30th, 2009, 02:38 PM

Tags for this Thread