Forums Register Login

add text into JTextPane through JButton, but not replace other text

+Pie Number of slices to send: Send
I am working on a setup that has multiple JButtons calling text into a big JTextPane. The text is coming in fine from the JButton click, although it is currently replacing the text already there from the other button clicks. How could I get it to add onto the already pre-existing text? Ideally I would like the new text to pop in where the cursor is left (currently its popping in at the top).

I would imagine that these settings would be set up in one function pointing at the JTextPane, what I have so far...




I have already done my imports for the components and the font along with creating the objects of each variable

Thanks a lot for your help
+Pie Number of slices to send: Send
 

How could I get it to add onto the already pre-existing text?





Ideally I would like the new text to pop in where the cursor is left



+Pie Number of slices to send: Send
Thanks a lot for your help, the insertString() is currently giving me some trouble, after reading the errors and reading up on them I came to the conclusion for the code below. I am not totally sure about the int, but should it be as many characters that are in the string?



The current error I am getting now is the BadLocationException(), I read up about it and the code below is the best I could understand of it.

The error I am getting on offsetRequested() is the missing return statement, not sure what it is wanting from me.


lastly


is said to be an unused method... how would I use it? I currently have code in it that is not returning errors.

Thanks a lot for your help

+Pie Number of slices to send: Send
Do you need to use a TextPane ? You could use TextArea.append() if not.
+Pie Number of slices to send: Send
Thank you so much, It works PERFECTLY!

places text at current cursor position

adds onto text already there.

Exactly what I was looking for, thanks so much
+Pie Number of slices to send: Send
 

Ben Hultin wrote:Thanks a lot for your help, the insertString() is currently giving me some trouble, after reading the errors and reading up on them I came to the conclusion for the code below. I am not totally sure about the int, but should it be as many characters that are in the string?



The current error I am getting now is the BadLocationException(), I read up about it and the code below is the best I could understand of it.

The error I am getting on offsetRequested() is the missing return statement, not sure what it is wanting from me.


lastly


is said to be an unused method... how would I use it? I currently have code in it that is not returning errors.

Thanks a lot for your help



just curious, what is your purpose of using offsetRequested() in the first place? Do you have a link to a page that says you should use that you could provide?
+Pie Number of slices to send: Send
The append method only exists for a JTextArea.

If you actually look at the source code you will see that it uses the insertString(...) method as I suggested above. So why don't you look at the source code so you will understand your coding error. Because using the insertString method is not complicated at all. Its Java 101 type coding where you use a try/catch block. The sooner you understand this the less grief you will cause yourself in the long run.
+Pie Number of slices to send: Send
I found the info about the offsetrequest() on the java.sun website. I may have been placing it wrong.


http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/text/BadLocationException.html


I also looked into my source code and no other insertString() method could be found. Would this be in the java core library? If so how do I access this source code? Thanks a lot
+Pie Number of slices to send: Send


Yes. You should have a src.zip file located in the directory of your JDK which you can unzip. Then you just look at the JTextArea source.
+Pie Number of slices to send: Send
 

Ben Hultin wrote:I found the info about the offsetrequest() on the java.sun website. I may have been placing it wrong.


http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/text/BadLocationException.html


I also looked into my source code and no other insertString() method could be found. Would this be in the java core library? If so how do I access this source code? Thanks a lot



I'll answer a slight different question. I've used insertString() a lot with JTextPane, and never used offsetRequest. I can tell you it is not a requirement. If you don't need it.

I can tell you that the following will work, keep in mind it is pseudo code


notice my catch block is empty. I am not suggesting an empty catch block as a general practise, but I know in my program an exception would mean a logic error on my part as a programmer. If I program correctly, it will never happen, but I still need the try catch anyway. But it depends on the program, your needs may be different.

I agree with Rob Camick. I litlle effort now to learn basic exception handling will get you a long way. I provided a link in another thread which I found very helpful.

p.s. JtextPane allows you to format your text with colors, font sixe, etc. If plain text will do, then most likely JTextArea will suit your needs.
+Pie Number of slices to send: Send
So I did some snooping around, realized that append was not quite what I need. I need the text to appear at the current caret position and insert text there regardless of other text around it. I checked around and I come up with the following idea, although it has an error.





I imagine its obvious what I am attempting to do. get the caret position, assign it to a variable then access that variables value in the insert() method.

The error I am getting so far:

unexpected type. required: value, found: class. '.class' expected

The error is found in second block of code.

I appreciate any help
+Pie Number of slices to send: Send
 

Ben Hultin wrote:So I did some snooping around, realized that append was not quite what I need. I need the text to appear at the current caret position and insert text there regardless of other text around it. I checked around and I come up with the following idea, although it has an error.





I imagine its obvious what I am attempting to do. get the caret position, assign it to a variable then access that variables value in the insert() method.

The error I am getting so far:

unexpected type. required: value, found: class. '.class' expected

The error is found in second block of code.

I appreciate any help



As I see it, the issue is one of how to properly call a method. I don't think you should be re-declaring caret within the insert method call. Try removing int from code_field.insert("<html>\n\n\n\n</html>\n", int caret);
bacon. tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 9326 times.
Similar Threads
Scaling
DocumentRenderer... Is printing styleddocuments possible?
JTable Cell Renderers
Having different text styles in the same JtextArea
Please need help with calculator program...
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 02:40:18.