Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / October 2007

Tip: Looking for answers? Try searching our database.

JEditorPane and HTML offsets

Thread view: 
timtech - 17 Oct 2007 20:04 GMT
Given the offset from the start of an HTML document, how I can
translate this into the offset when the HTML is rendered in a
JEditorPane?  e.g.  in the HTML document (not strict HTML but still
renders):

<HTML>test</HTML>

"test" starts at offset 6 in the HTML - but in the JEditorPane only
"test" would be displayed so it would start at offset 0 - if I wanted
to highlight it for example.  You can imagine more complicated
documents...

Any ideas greatly apprieciated including if I can use an alternative
to the JEditorPane.

Thanks,
Ed Webb - 18 Oct 2007 10:42 GMT
> Given the offset from the start of an HTML document, how I can
> translate this into the offset when the HTML is rendered in a
[quoted text clipped - 12 lines]
>
> Thanks,

How about taking the text of the html file before your offset (in this
case "<html>" and using replaceAll("<.*?>", "") to remove the tags which
would then give you the number of visible characters before your offset
which is the offset of your text in the JEditorPane. You will need
something more complicated for more complex documents (such as those
with a <head> section or <script></script> tags) but it's a start.

Ed!
Andrew Thompson - 18 Oct 2007 12:37 GMT
>Given the offset from the start of an HTML ..

HTML *offwhats?*  Can you provide an SSCCE that
demonstrates these offsets?

Signature

Andrew Thompson
http://www.athompson.info/andrew/

timtech - 23 Oct 2007 15:10 GMT
> timtechwrote:
> >Given the offset from the start of an HTML ..
[quoted text clipped - 6 lines]
>
> Message posted viahttp://www.javakb.com

By "offsets" I mean the number of characters from the beginning of the
document (including line feeds).

Thanks
Andrew Thompson - 23 Oct 2007 15:44 GMT
...
>By "offsets" I mean the number of characters from the beginning of the
>document (including line feeds).

In that case, you might play with this example.

Mind you, I get a start position of '0' printed, which
makes it quite odd to me that both these operations
should effect the 'e', rather than the 's'.

<sscce>
import javax.swing.*;

class HTMLTextOffset {

 public static void main(String[] args)
   throws Exception {

   String content = "<HTML>test</HTML>";

   JEditorPane jep =
     new JEditorPane("text/html", content);
   jep.setEditable(false);

   System.out.println(
     jep.getDocument().getStartPosition() );

   jep.setCaretPosition(2);
   jep.moveCaretPosition(3);

   JOptionPane.showMessageDialog( null, jep );

   jep.setCaretPosition(0);
   jep.getDocument().remove(2,1);
   JOptionPane.showMessageDialog( null, jep );
 }
}
</sscce>

Signature

Andrew Thompson
http://www.athompson.info/andrew/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.