Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.help > #2530 > unrolled thread

JTextPane.modelToView() exception

Started byFred Kleinschmidt <fred.l.kleinschmidt@gmail.com>
First post2013-02-22 13:52 -0800
Last post2013-02-25 09:08 -0800
Articles 4 — 3 participants

Back to article view | Back to comp.lang.java.help


Contents

  JTextPane.modelToView() exception Fred Kleinschmidt <fred.l.kleinschmidt@gmail.com> - 2013-02-22 13:52 -0800
    Re: JTextPane.modelToView() exception Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2013-02-22 15:33 -0800
      Re: JTextPane.modelToView() exception Fred Kleinschmidt <fred.l.kleinschmidt@gmail.com> - 2013-02-25 07:48 -0800
        Re: JTextPane.modelToView() exception Knute Johnson <nospam@knutejohnson.com> - 2013-02-25 09:08 -0800

#2530 — JTextPane.modelToView() exception

FromFred Kleinschmidt <fred.l.kleinschmidt@gmail.com>
Date2013-02-22 13:52 -0800
SubjectJTextPane.modelToView() exception
Message-ID<d04155b6-9b24-42d5-9f82-d24fae55982b@googlegroups.com>
I am getting a BadLocationException from a call to textpane.modelToView() 
when the position I pass should be legal.

I create a JTextPane and get its document:
   JTextPane jtp = new JTextPane();
   Document doc = jtp.getDocument();
Then I add a document listener to the document.

This is inside a DocumentListener's insertUpdate() method:
public void insertUpdate( DocumentEvent event ) {
   Document doc = event.getDocument();
   int len = doc.getLength();
   if ( len > 0 ) {
      try {
         Rectangle view = jtp.modelToView(len-1);
         // do other things here
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

Everything is fine until I enter the first character after a newline.
Then I get a BadLocationException: Position not represented by view
(I am using Java 1.6, on a Windows 7 platform)

[toc] | [next] | [standalone]


#2533

FromKnute Johnson <nospam@rabbitbrush.frazmtn.com>
Date2013-02-22 15:33 -0800
Message-ID<kg8v6a$e38$1@dont-email.me>
In reply to#2530
On 2/22/2013 13:52, Fred Kleinschmidt wrote:
> I am getting a BadLocationException from a call to textpane.modelToView()
> when the position I pass should be legal.
>
> I create a JTextPane and get its document:
>     JTextPane jtp = new JTextPane();
>     Document doc = jtp.getDocument();
> Then I add a document listener to the document.
>
> This is inside a DocumentListener's insertUpdate() method:
> public void insertUpdate( DocumentEvent event ) {
>     Document doc = event.getDocument();
>     int len = doc.getLength();
>     if ( len > 0 ) {
>        try {
>           Rectangle view = jtp.modelToView(len-1);
>           // do other things here
>        } catch (Exception e) {
>           e.printStackTrace();
>        }
>     }
> }
>
> Everything is fine until I enter the first character after a newline.
> Then I get a BadLocationException: Position not represented by view
> (I am using Java 1.6, on a Windows 7 platform)
>

An SSCCE would have been really nice here.  But I think the behavior you 
are seeing is normal.  I'm curious as to what you are really trying to 
do with the result of the JTextComponent.modelToView() call?  Best I can 
tell the purpose of that method is to facilitate cursors.

knute...

[toc] | [prev] | [next] | [standalone]


#2555

FromFred Kleinschmidt <fred.l.kleinschmidt@gmail.com>
Date2013-02-25 07:48 -0800
Message-ID<fa62a9b5-79b1-4a8e-91f0-34468b97b263@googlegroups.com>
In reply to#2533
On Friday, February 22, 2013 3:33:28 PM UTC-8, Knute Johnson wrote:
> On 2/22/2013 13:52, Fred Kleinschmidt wrote: > I am getting a BadLocationException from a call to textpane.modelToView() > when the position I pass should be legal. > > I create a JTextPane and get its document: > JTextPane jtp = new JTextPane(); > Document doc = jtp.getDocument(); > Then I add a document listener to the document. > > This is inside a DocumentListener's insertUpdate() method: > public void insertUpdate( DocumentEvent event ) { > Document doc = event.getDocument(); > int len = doc.getLength(); > if ( len > 0 ) { > try { > Rectangle view = jtp.modelToView(len-1); > // do other things here > } catch (Exception e) { > e.printStackTrace(); > } > } > } > > Everything is fine until I enter the first character after a newline. > Then I get a BadLocationException: Position not represented by view > (I am using Java 1.6, on a Windows 7 platform) > An SSCCE would have been really nice here. But I think the behavior you are seeing is normal. I'm curious as to what you are really trying to do with the result of the JTextComponent.modelToView() call? Best I can tell the purpose of that method is to facilitate cursors. knute...


I'm trying to track the screen position of each newline so that I can 
keep text in an adjacent text pane aligned with this text pane. It the
text in the main pane wraps dut to the user changing the window size,
I want the text in the adjacent pane to wrap, too.

The main pane is the output area of an executed command; the adjacent 
pane (on the left, in this instance) contains the prompts.
So this is similar to an Xterm except that the prompts are not 
displayed in the same text area as the output (this facilitates 
cut/paste from the output area without grabbing the prompts).
Fred...

[toc] | [prev] | [next] | [standalone]


#2556

FromKnute Johnson <nospam@knutejohnson.com>
Date2013-02-25 09:08 -0800
Message-ID<kgg5oj$4k2$1@dont-email.me>
In reply to#2555
On 2/25/2013 7:48 AM, Fred Kleinschmidt wrote:
> On Friday, February 22, 2013 3:33:28 PM UTC-8, Knute Johnson wrote:
>> On 2/22/2013 13:52, Fred Kleinschmidt wrote: > I am getting a BadLocationException from a call to textpane.modelToView() > when the position I pass should be legal. > > I create a JTextPane and get its document: > JTextPane jtp = new JTextPane(); > Document doc = jtp.getDocument(); > Then I add a document listener to the document. > > This is inside a DocumentListener's insertUpdate() method: > public void insertUpdate( DocumentEvent event ) { > Document doc = event.getDocument(); > int len = doc.getLength(); > if ( len > 0 ) { > try { > Rectangle view = jtp.modelToView(len-1); > // do other things here > } catch (Exception e) { > e.printStackTrace(); > } > } > } > > Everything is fine until I enter the first character after a newline. > Then I get a BadLocationException: Position not represented by view > (I am using Java 1.6, on a Windows 7 platform) > An SSCCE would have been really nice here. But I think the behavior you are seeing is normal. I'm curious as to what you are re
ally trying to do with the result of the JTextComponent.modelToView() call? Best I can tell the purpose of that method is to facilitate cursors. knute...
>
>
> I'm trying to track the screen position of each newline so that I can
> keep text in an adjacent text pane aligned with this text pane. It the
> text in the main pane wraps dut to the user changing the window size,
> I want the text in the adjacent pane to wrap, too.
>
> The main pane is the output area of an executed command; the adjacent
> pane (on the left, in this instance) contains the prompts.
> So this is similar to an Xterm except that the prompts are not
> displayed in the same text area as the output (this facilitates
> cut/paste from the output area without grabbing the prompts).
> Fred...
>

I think I understand what you are trying to do but I don't know how to 
do it.  I looked through the docs a little bit but I'm not finding 
anything that looks promising.  The only thought I did have was to 
design a new component where you write to half of it as the input side 
and the other half as the output side.  You would have to do your own 
line wrap that way but it might be doable.

Sorry I wasn't more help.

-- 

Knute Johnson

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.help


csiph-web