?
Solved

JTextPane font

Posted on 2003-03-30
16
Medium Priority
?
434 Views
Last Modified: 2013-12-02
Hi,
I want to get the font charectiristics of each letter in a JTextArea after writing it. How can I do it?
I've tried using the method getFont of JTextPane and got the primal font of the JTextPane.
Thanks,
gabi
0
Comment
Question by:gavrile
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
16 Comments
 
LVL 92

Expert Comment

by:objects
ID: 8236898
You need to use the FontMetrics class.
0
 

Author Comment

by:gavrile
ID: 8236982
Hi objects,
I've tried to use the method getFontMetrics in the following way unsuccessfully:
writingArea.getFontMetrics(writingArea.getFont())

How can I use it to get the font style, size and type?

Thanks,
Gabi
0
 
LVL 92

Expert Comment

by:objects
ID: 8237006
you can get style, size and type directly from the Font class.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:gavrile
ID: 8237034
Yes I know. But the style size and type I'm getting are the primal ones in the JTextPane and as I change the font size, style and type I do not get the new properties.
For example the beggining size of font was 14 and then I changed it to 24, but the font size I'm getting is 14 without changes.

I'm changing the font size in the JTextPane in the following way:
    Action sizeAction[] = new Action[size.length];
    for (int j = 0; j < size.length; j++)
    {
      String sizeName = "font-size-"+size[j];
      sizeAction[j] = (Action)commands.get (sizeName);
    }
Any suggestions?
Thanks,
Gabi
0
 
LVL 92

Expert Comment

by:objects
ID: 8237075
Sounds like you are still accessing the previous font object used by the text area, and not the newly set one.
Are you changing the font using setFont()? And then getting the text area font afterwards using getFont()?
0
 

Author Comment

by:gavrile
ID: 8237084
No, I'm not using the method setFont() and even when I write many characters with the new properties I still get the first characters.
gabi
0
 
LVL 92

Expert Comment

by:objects
ID: 8237154
> I'm not using the method setFont()

then how do you change the font for the text area?
0
 

Author Comment

by:gavrile
ID: 8237184
I'm using objects of class Action that JTextPane holds.
Several of the objects are used to change the font size and others change the font style and type as detailed above. I don't use setFont because it changes the font properties for all of the TextPane.
0
 
LVL 92

Expert Comment

by:objects
ID: 8237460
Ahh JTextPane, the mention of JTextArea confused me :)
0
 

Author Comment

by:gavrile
ID: 8243532
Hi objects,
Do you have any ideas regarding the font changes?
gabi
0
 
LVL 92

Expert Comment

by:objects
ID: 8243653
I'm guessing it is included in the AttributeSet.
Have a look at it's details and see what u can find.
0
 

Author Comment

by:gavrile
ID: 8243833
My problem is that setFont changes the font of all of the text pane and not of single letters.
How can I use setFont of JTextPane to write each letter in the text pane in a different font?
thanks,
gabi
0
 
LVL 92

Accepted Solution

by:
objects earned 80 total points
ID: 8243892
> How can I use setFont of JTextPane to write each letter in the text pane in a different font?

You don't.

http://java.sun.com/docs/books/tutorial/uiswing/components/editorpane.html
0
 
LVL 4

Expert Comment

by:kylar
ID: 8248847
public class FontTest extends JFrame{
  public FontTest(){
    this.setSize(500,500);
    JTextPane jtp = new JTextPane();

    this.getContentPane().add(jtp);
    SimpleAttributeSet sas = new SimpleAttributeSet(jtp.getCharacterAttributes());
    SimpleAttributeSet sas2 = new SimpleAttributeSet(jtp.getCharacterAttributes());

    StyleConstants.setFontFamily(sas, "Monospaced");
    StyleConstants.setFontSize(sas, 36);
    StyleConstants.setForeground(sas, Color.blue);
    StyleConstants.setFontFamily(sas2, "SansSerif");
    StyleConstants.setFontSize(sas2, 36);
    StyleConstants.setForeground(sas2, Color.RED);
    //sas.
    //jtp.setCharacterAttributes();
    try{
      jtp.getDocument().insertString(jtp.getDocument().getLength(), " - - Font 2", sas);
      jtp.getDocument().insertString(jtp.getDocument().getLength(), " - - Font 3", sas2);

    }catch(Exception e){
      e.printStackTrace();
    }
    this.setVisible(true);
    this.getContentPane().repaint();
  }

  public static void main(String args[]){
    new FontTest();
  }
}

Cheers,
Kylar
0
 

Expert Comment

by:CleanupPing
ID: 9058376
gavrile:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses
Course of the Month10 days, 2 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question