File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes add text into JTextPane through JButton, but not replace other text Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "add text into JTextPane through JButton, but not replace other text" Watch "add text into JTextPane through JButton, but not replace other text" New topic
Author

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

Ben Hultin
Ranch Hand

Joined: Aug 17, 2009
Posts: 135
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
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2169
    
    7
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


Ben Hultin
Ranch Hand

Joined: Aug 17, 2009
Posts: 135
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

Lee Quinlan
Greenhorn

Joined: Aug 25, 2009
Posts: 9
Do you need to use a TextPane ? You could use TextArea.append() if not.
Ben Hultin
Ranch Hand

Joined: Aug 17, 2009
Posts: 135
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
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
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?
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2169
    
    7
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.
Ben Hultin
Ranch Hand

Joined: Aug 17, 2009
Posts: 135
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
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2169
    
    7


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.
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
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.
Ben Hultin
Ranch Hand

Joined: Aug 17, 2009
Posts: 135
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
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
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);
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: add text into JTextPane through JButton, but not replace other text