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 / January 2005

Tip: Looking for answers? Try searching our database.

Strange TextPane

Thread view: 
SinusX - 09 Jan 2005 12:20 GMT
One more ;)

   int position = MyTextPane.getCaretPosition();
   Element elem = doc.getCharacterElement(position);
    ((HTMLDocument) MyJTextPane.getDocument()).insertAfterEnd(elem,
<HTML><B><BR>" + "blablabla "+"</HTML></BODY>");

I dont't know why but if I use this code, texts is added on the right site
od last one not under. After the row is fully caret goes in to next line.
Help what's wrong
Thanks for help
Christian Kaufhold - 09 Jan 2005 15:24 GMT
>    int position = MyTextPane.getCaretPosition();
>    Element elem = doc.getCharacterElement(position);
[quoted text clipped - 3 lines]
> I dont't know why but if I use this code, texts is added on the right site
> od last one not under. After the row is fully caret goes in to next line.

That's what you requested to do: the new data is inserted after the end
of the character element at the caret position, which may be further from
the current caret position. If you want to insert at the caret position,
there seems to be no non-trivial way to do it by default.

Try this to create an element boundary at the caret position, then insert
AfterEnd (or BeforeStart) can be used.

import javax.swing.text.*;
import java.io.IOException;

import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.StyleSheet;

public class HTMLDocument2
   extends HTMLDocument
{
   public HTMLDocument2(StyleSheet s)
   {
    super(s);
   }
   

   private void ensureLeafBoundary(int position)
   {
    Element e = getCharacterElement(position);
   
    if (position != e.getStartOffset())
    {
       // somewhat hacky too avoid low-level ElementBuffer stuff
       setCharacterAttributes(position, Math.min(getLength(), e.getEndOffset()) - position, SimpleAttributeSet.EMPTY, false);
    }
   }
       

   public void insertAtLeaf(int position, String htmlText)
   {
    try
    {
       ensureLeafBoundary(position);
       
       insertBeforeStart(getCharacterElement(position), htmlText);
    }
    catch (BadLocationException e)
    {
       throw new IllegalArgumentException(e.getMessage());
    }
    catch (IOException e)
    {
       throw new RuntimeException(e.getMessage());
    }
       
   }
}

class HTMLEditorKit2
   extends HTMLEditorKit
{
   public HTMLEditorKit2()
   {
   }
   
   public Document createDefaultDocument()
   {
    StyleSheet sheet = getStyleSheet();

    StyleSheet styles = new StyleSheet();
   
    styles.addStyleSheet(sheet);
   
    HTMLDocument result = new HTMLDocument2(styles);
   
    result.setParser(getParser());
   
    result.setAsynchronousLoadPriority(4);
   
    result.setTokenThreshold(100);
   
    return result;
   }
}

class HTMLTest
{
   public static void main(String[] args)
   {
    javax.swing.JFrame f = new javax.swing.JFrame();

    final javax.swing.JEditorPane t = new javax.swing.JEditorPane();
    t.setEditorKit(new HTMLEditorKit2());

    final HTMLDocument2 d = (HTMLDocument2)t.getDocument();

    javax.swing.JButton add = new javax.swing.JButton("Add");
    add.addActionListener(new java.awt.event.ActionListener()
    {
       public void actionPerformed(java.awt.event.ActionEvent e)
       {
        d.insertAtLeaf(t.getCaretPosition(), "<B>Text<P>More <em>data");
       }
    });
   
    f.getContentPane().add(new javax.swing.JScrollPane(t));
    f.getContentPane().add(add, "South");

    f.setSize(500, 400); f.show();
   }
}

Christian
SinusX - 09 Jan 2005 16:22 GMT
Maybe I can use other component like TextPane. Is there any other component
without this problem ? (ofecourse component like TextPane)

> >    int position = MyTextPane.getCaretPosition();
> >    Element elem = doc.getCharacterElement(position);
[quoted text clipped - 113 lines]
>
> Christian
Christian Kaufhold - 09 Jan 2005 16:26 GMT
> Maybe I can use other component like TextPane. Is there any other component
> without this problem ? (ofecourse component like TextPane)

If you had understood my reply, you would have noticed that it has nothing
to do with the component at all, but with the fact that you are using the
wrong feature.

[snip bottom-quote]

Christian
SinusX - 09 Jan 2005 19:59 GMT
> to do with the component at all, but with the fact that you are using the
> wrong feature.
So how i should do this properly.
Christian Kaufhold - 11 Jan 2005 10:35 GMT
>> to do with the component at all, but with the fact that you are using the
>> wrong feature.
> So how i should do this properly.

Eh, use the code I posted?

Christian


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.