I've tried everything...setting font of selected text

Reply

Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

I've tried everything...setting font of selected text

 
0
  #1
Jan 8th, 2005
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:
  1. public void processBoldText()
  2. {
  3. MutableAttributeSet mas = new SimpleAttributeSet();
  4. Font f = new Font ("SansSerif", Font.BOLD, 20);
  5. StyleConstants.setBold(mas,f.isBold());
  6. int offset = textArea2.getSelectionStart();
  7. int end = textArea2.getSelectionEnd();
  8. int length = end - offset;
  9. doc.setCharacterAttributes(offset,length,mas,false);
  10.  
  11.  
  12. }

I get an error message saying cannot find symbol, method setcharacter attributes(and ithen it lists the arguments)
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 22
Reputation: geeeez is an unknown quantity at this point 
Solved Threads: 0
geeeez's Avatar
geeeez geeeez is offline Offline
Newbie Poster

Re: I've tried everything...setting font of selected text

 
0
  #2
Jan 8th, 2005
had no idea sorry..
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 609
Reputation: freesoft_2000 is an unknown quantity at this point 
Solved Threads: 7
freesoft_2000 freesoft_2000 is offline Offline
Practically a Master Poster

Re: I've tried everything...setting font of selected text

 
0
  #3
Jan 8th, 2005
Hi everyone,

This is what you can do

  1.  
  2. StyleContext sc = new StyleContext();
  3. DefaultStyledDocument dse = new DefaultStyledDocument(sc);
  4. textarea2.setDocument(dse);
  5.  
  6. public void processBoldText( )
  7. {
  8. MutableAttributeSet mas = new SimpleAttributeSet();
  9. StyleConstants.setBold(mas, true);
  10. int offset = textarea2.getSelectionStart();
  11. int end = textarea2.getSelectionEnd();
  12. int length = end - offset;
  13. dse.setCharacterAttributes(offset,length,mas,false);
  14.  
  15.  
  16. }

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

I hope this helps you

Richard West
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: I've tried everything...setting font of selected text

 
0
  #4
Jan 9th, 2005
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:
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 609
Reputation: freesoft_2000 is an unknown quantity at this point 
Solved Threads: 7
freesoft_2000 freesoft_2000 is offline Offline
Practically a Master Poster

Re: I've tried everything...setting font of selected text

 
0
  #5
Jan 9th, 2005
Hi everyone,

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

Richard West
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: I've tried everything...setting font of selected text

 
0
  #6
Jan 9th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: I've tried everything...setting font of selected text

 
0
  #7
Jan 9th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 609
Reputation: freesoft_2000 is an unknown quantity at this point 
Solved Threads: 7
freesoft_2000 freesoft_2000 is offline Offline
Practically a Master Poster

Re: I've tried everything...setting font of selected text

 
0
  #8
Jan 10th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: I've tried everything...setting font of selected text

 
0
  #9
Jan 10th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: I've tried everything...setting font of selected text

 
0
  #10
Jan 10th, 2005
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?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC