Search the archives!
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Advanced-java] Java Swing question.
- From: nikolaos@xxxxxxxxx (Nikolaos Giannopoulos)
- Subject: [Advanced-java] Java Swing question.
- Date: Thu, 21 Mar 2002 19:35:19 -0000
Okay now it makes sense.
You need to work with the underlying document model - JTextPane and
JEditorPane use a tree like document model to retain their data. Don't
quote me "but" I'm not sure if new line chars are even retained in the tree
which could very well be your problem - i.e. your problem seems to not be
the component but how you are using it.
What you are doing will work with a JTextArea but that is not what you are
dealing with.
Try this:
try {
jTextPane.getDocument().insertString(
jTextPane.getCaretPosition(), "TEST INSERTION", null);
} catch (BadLocationException blex) {}
Which once you see that it works you can then replace the static text with
the text you obtain from the clipboard. BTW there is no need to deal with
pre, middle, or post insertions - the above code it is all you need for the
3 cases you document.
A final note - until you get used to working with the document model it can
be quite a pain (e.g. getting cursor row:column position is not the most
trivial of code) but once you do things work out fine.
If it doesn't work then I'll look into your sample code more closely.
Good Luck,
--Nikolaos
> -----Original Message-----
> From: Reinstein, Lenny [mailto:Leonard.Reinstein@xxxxxx]
> Sent: Thursday, March 21, 2002 9:07 PM
> To: 'nikolaos@xxxxxxxxx'; Advanced-java@xxxxxxxxxxxxxxxxxxxxxx
> Subject: RE: [Advanced-java] Java Swing question.
>
>
> Here is the example code:
> -------------------------
> // string to be pasted
> String str =
> (String)(system.getContents(this).getTransferData(DataFlavor.strin
> gFlavor));
> int pos = textPane.getCaretPosition();
> // current text
> String text = textPane.getText();
>
> // setting
> if(text == null || text.length() == 0)
> {
> System.out.print("---SETTING---");
> textPane.setText(str);
> }
> // apending
> else if((text.length() - 1) <= pos)
> {
> System.out.print("---APPENDING---");
> textPane.setText(text + str);
> }
> // inserting in the middle
> else
> {
> System.out.print("---INSERTING at ---" + pos);
> textPane.setText(text.substring(0, pos + 1) + str + text.substring(pos +
> 1, text.length()));
> }
>
>
> Say, I have this in the text pane:
>
> hello world
> hello world
> hello world
> hello world
> ---> paste here does not work correctly
> hello
>
> Maybe I am just missing something. Would apreciate any help.
>
> -----Original Message-----
> From: Nikolaos Giannopoulos [mailto:nikolaos@xxxxxxxxx]
> Sent: Thursday, March 21, 2002 11:11 AM
> To: Reinstein, Lenny; Advanced-java@xxxxxxxxxxxxxxxxxxxxxx
> Subject: RE: [Advanced-java] Java Swing question.
>
>
> Are you sure this behaves like this. What is the code snippet
> that you are
> using and what component are you dealing with e.g. JTextPane, JEditorPane.
>
> I'll guess that its not getCaretPosition() that is your problem
> but how you
> are then using that position to index into your document. Post the code
> (including the portion that inserts it into your document) and possibly I
> can help.
>
> Personally I don't think you should have to resort to a buffer or any sort
> of token substitution mechanism.
>
> Think about it if you have a document with say just 5 newlines
> does it make
> any sense that you would only be able to insert into it at only the
> beginning or the end (assuming as you put it getCaretPosition() ignores
> newlines in providing a position)- I don't think so.
>
> Have you tried SUNs notepad example (last time I saw it it was as
> a JWS demo
> app but I'm sure you can find a tutorial on the SUN site - I can't imagine
> that it behaves in this way but I guess the proof would be in the
> pudding).
> If not SUNs and you use any commercial IDE they typically come with a
> notepad example (at least JB does). It would take you all of 30
> seconds to
> prove/disprove your theory (assuming you can get it).
>
> --Nikolaos
>
>
> > -----Original Message-----
> > From: advanced-java-admin@xxxxxxxxxxxxxxxxxxxxxx
> > [mailto:advanced-java-admin@xxxxxxxxxxxxxxxxxxxxxx]On Behalf Of
> > Reinstein, Lenny
> > Sent: Thursday, March 21, 2002 7:23 PM
> > To: 'Tim O'Neil'; 'Advanced-java@xxxxxxxxxxxxxxxxxxxxxx'
> > Subject: RE: [Advanced-java] Java Swing question.
> >
> >
> > getCaretPosition will still return incorrect value because of newlines.
> > getCaretPosition works in a text component only, not in a buffer. The
> > problem is to determine where at any point in time the caret is
> located in
> > the text.
> >
> > -----Original Message-----
> > From: Tim O'Neil [mailto:toneil@xxxxxxxxxx]
> > Sent: Thursday, March 21, 2002 2:09 PM
> > To: 'Advanced-java@xxxxxxxxxxxxxxxxxxxxxx'
> > Subject: RE: [Advanced-java] Java Swing question.
> >
> >
> > You need to copy the text into a buffer, a String object or
> > a byte array, or something. The user only sees the textbox,
> > the magic is happening on the buffer. Simple.
> >
> > From: Reinstein, Lenny [mailto:Leonard.Reinstein@xxxxxx]
> > >This is OK, but how can I make that token invisible? All this has to be
> > >hidden from the user. The user can open the text component with some
> > content
> > >already there. He clicks somewhere in the middle of the text
> and tries to
> > >paste some text in it. I handle paste manually. So, I need to
> > know where he
> > >has the Caret, including newlines. Say, I replace newlines with
> > some tokens
> > >before I load the content, but the user should not see then.
> > He/she should
> > >see paragraphs, as though the newlines are there. I am OK with
> > any solution
> > >as long as it works and can be appplied here. Many thanks in advance.
> >
> > It took 100 monkeys 100 years to come up with:
> >
> >
> > ***********************************************************************
> > This email and any files transmitted with it are confidential and
> > intended solely for the use of the individual or entity to whom they
> > are addressed. Any unauthorized review, use, disclosure or distribution
> > is prohibited. If you are not the intended recipient, please contact
> > the sender by reply e-mail and destroy all copies of the original
> > message.
> > ***********************************************************************
> > _______________________________________________
> > Advanced-java mailing list
> > Advanced-java@xxxxxxxxxxxxxxxxxxxxxx
> > http://lists.xcf.berkeley.edu/mailman/listinfo/advanced-java
> > _______________________________________________
> > Advanced-java mailing list
> > Advanced-java@xxxxxxxxxxxxxxxxxxxxxx
> > http://lists.xcf.berkeley.edu/mailman/listinfo/advanced-java
> >
>
>
- References:
- [Advanced-java] Java Swing question.
- From: Reinstein, Lenny
- [Advanced-java] Java Swing question.
- Prev by Date: [Advanced-java] Java Swing question.
- Next by Date: FW: [Advanced-java] java.lang.LinkageError
- Previous by thread: [Advanced-java] Java Swing question.
- Next by thread: [Advanced-java] improving performance of recursive algorithm
- Index(es):