I've been trying to do this for weeks now, and can't seem to get it to work. I have a JTextPane called textArea2, and I have a variable called doc that gets the document from the textpane. I want to get the selected text but I can't get no code to work. I've posted in a lot of forums and no one will help me.

This is the code I have so far:

public void processBoldText()
 {
	 MutableAttributeSet mas = new SimpleAttributeSet();
	Font f = new Font ("SansSerif", Font.BOLD, 20);
	StyleConstants.setBold(mas,f.isBold());
	int offset = textArea2.getSelectionStart();
	int end = textArea2.getSelectionEnd();
	int length = end - offset;
	doc.setCharacterAttributes(offset,length,mas,false);


 }

I get an error message saying cannot find symbol, method setcharacter attributes(and ithen it lists the arguments)

Recommended Answers

All 17 Replies

had no idea sorry.. :sad:

Hi everyone,

This is what you can do

StyleContext sc = new StyleContext();
DefaultStyledDocument dse = new DefaultStyledDocument(sc);
textarea2.setDocument(dse);

  public void processBoldText( )
 {
	MutableAttributeSet mas = new SimpleAttributeSet();
	StyleConstants.setBold(mas, true);
	int offset = textarea2.getSelectionStart();
	int end = textarea2.getSelectionEnd();
	int length = end - offset;
	dse.setCharacterAttributes(offset,length,mas,false);


 }

The fault is that you did not set a styled document type

I hope this helps you

Richard West

Thanks, Richard. This worked great after I fixed a simple problem. The problem was I accidently was using a JEditorPane instead of a JTextPane. :mrgreen:

When I changed, everything worked!!! :lol:

Hi everyone,

Actually even if you used JEditorPane it should still work. Did you try installing the rtf editor kit?

Richard West

No I haven't installed the rtf editor kit. I don't think the JEditorPane supported the setCharacteristis method because I kept getting an error message saying cannot resolve symbol: method setCharacteristics or something like that.

One thing I don't understand, is that Im getting different results just switching from the textArea to the textpane. For instance, I had a find, find next , find and replace method that worked fine on the JTextArea, but it does really weird things on the JTextPane.

Hi everyone,

Server Crash you said "I had a find, find next , find and replace method that worked fine on the JTextArea, but it does really weird things on the JTextPane"

You know something i had the same problem with this find/replace function of mine but here is the funny thing,

I have two computers both running on windows 98 and both installed with java sdk
1.4.2 but my program runs perfectly on one computer but does weird things on the other computer. I really don't know why but i think this is rather weird, don't you think so?

Richard West

I see what your saying, but the code is correct and should work. This is what I don't understand. The only thing I can think of, is maybe there are specific methods for this.

Ill give you an example:

Say I type this in the text area:

1 2


Now say in my find and replace method that I choose to find "1". It would work fine

In the textPane it might select stuff inbetween 1 and 2 and sometimes it will even select the 2?

Can your code somehow become corrupt or something switching Objects like I did?

anyone? Is it possible?

Hi everyone,

It's possible but only if you use a lot of threads but other than that it is highly unlikely.

Richard West

I posted on the java site and found out what it was. It was something like offset was different for the textpane than it was for the textarea. I can post the link explaining it, if anyone is interested.

Hi everyone,

Sure i am interested post the link here.
For now i am trying to print the contents of a JTextPane including its selected content
So far i have had little success. Do you know of a link to such a source???

Richard West

http://forum.java.sun.com/thread.jspa?threadID=587153
It's something about the offset is different.


I personally have never tryed to print in java. I've been working on this text editor and that is my next job, which I will hate!

I did search the java forums and found some people with similar problems, but unfortunately, no one posted with answers on any of the questions.

I found a link that looked like it my do you some good, but it might not. It looked like they were using something to count the pages and then using a book object declaring what was to be printed, and it looked like you could specify the number of pages to be printed. But I think what you wanted was the user selection.

http://www.javaworld.com/javaworld/jw-06-1999/jw-06-step.html


If it's the user selection you want printed, how are you getting the selected text and appending it to a printjob?
Does the print dialog even have an option for printing the selected text? I didn't see one on some of examples they had. This makes me wonder if you would have to print the selection without a dialog.


Note: I really don't know much about the printing, but hopefully some of the suggestions will help.

Note: Area you using a DocumentRenderer for the printing of the JTextPane?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.