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, 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.
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?
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.
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 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.
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.
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.
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);
Post by:autobot
bacon. tiny ad:
a bit of art, as a gift, that will fit in a stocking