get filecontent

Questions on using RSyntaxTextArea should go here.

Moderator: robert

get filecontent

Postby bob » Thu Aug 20, 2009 8:53 am

Hi Robert,

Like i said in my last topic, i'm trying to get the content of a remote file.

Java is not my language of choice so I have some difficulties as you can imagine...

in DemoRootPane.java, i added this lines of code

Code: Select all
   private RSyntaxTextArea createTextArea() throws IOException {
      RSyntaxTextArea textArea = new RSyntaxTextArea();
      textArea.setSyntaxEditingStyle(SYNTAX_STYLE_NONE);      
         URL test= new URL("http://www.domaine.com/1.txt");
   BufferedReader in = new BufferedReader(
            new InputStreamReader(
            test.openStream()));

   String inputLine;

   while ((inputLine = in.readLine()) != null)
      textArea.setText(getText(inputLine));
      textArea.setCaretPosition(0);
      textArea.addHyperlinkListener(this);
      textArea.requestFocusInWindow();
      textArea.setMarkOccurrences(true);
   in.close();
      return textArea;
   }


But this error "Caused by: java.lang.ClassCastException: FileContent cannot be cast to java.applet.Applet" is returned.

Can you help me again please ?
bob
 

Re: get filecontent

Postby bob » Thu Aug 20, 2009 11:25 am

It's probably something like that :

Code: Select all
   
private String getText() {
      ClassLoader cl = getClass().getClassLoader();
      BufferedReader r = null;
      URL test = new URL("http://www.domaine.com/1.txt");
      try {   
      
         r = new BufferedReader(new InputStreamReader(
               test.openStream()));
         StringBuffer sb = new StringBuffer();
         
            String inputLine;
         
         while ((inputLine = r.readLine()) != null) {
            sb.append(inputLine).append('\n');
         }
         r.close();
         return sb.toString();
      } catch (RuntimeException re) {
         throw re; // FindBugs
      } catch (Exception e) {
         return "Type here to see syntax highlighting";
      }
   }


...but it's not yet that :lol:

It's not easy Robert ! :roll:
bob
 

Re: get filecontent

Postby bob » Thu Aug 20, 2009 12:26 pm

I've got it ! 8-)

Code: Select all
   private String getText() {
      ClassLoader cl = getClass().getClassLoader();
      BufferedReader r = null;
      try {   
            URL test = new URL("http://www.domaine.com/1.txt");

         r = new BufferedReader(new InputStreamReader(
               test.openStream()));
         StringBuffer sb = new StringBuffer();
         
            String line = null;
         while ((line=r.readLine())!=null) {
            sb.append(line).append('\n');
         }
         r.close();
         return sb.toString();
      } catch (RuntimeException re) {
         throw re; // FindBugs
      } catch (Exception e) {
         return "Type here to see syntax highlighting";
      }
   }



And skip the "res' into all getText() (i forgot that...).

I'm learning, i'm learning....
bob
 

Re: get filecontent

Postby robert » Thu Aug 20, 2009 1:10 pm

Glad you figured it out, bob!

FYI - I see you're taking that code from the demo. I'm not sure why the demo's written that way, there's a better (e.g. faster and more memory-efficient, though you won't notice it on such small files) way to load content into an RSyntaxTextArea:

Code: Select all
private void setText(String resource) {
   ClassLoader cl = getClass().getClassLoader();
   BufferedReader r = null;
   try {
      r = new BufferedReader(new InputStreamReader(
            cl.getResourceAsStream(resource), "UTF-8"));
      textArea.read(r, null);
      r.close();
   } catch (RuntimeException re) {
      throw re; // FindBugs
   } catch (Exception e) {
      textArea.setText("Type here to see syntax highlighting");
   }
}


Here, "textArea" is your RSyntaxTextArea instance (actually, it can be any JTextComponent).

I'm going to update the demo to use this technique, as it's a little cleaner than the way it is now.
User avatar
robert
 
Posts: 801
Joined: Sat May 10, 2008 5:16 pm


Return to Help

Who is online

Users browsing this forum: No registered users and 4 guests