JTextPane font

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
gavrileAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
objectsConnect With a Mentor Commented:
> 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
 
objectsCommented:
You need to use the FontMetrics class.
0
 
gavrileAuthor Commented:
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
objectsCommented:
you can get style, size and type directly from the Font class.
0
 
gavrileAuthor Commented:
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
 
objectsCommented:
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
 
gavrileAuthor Commented:
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
 
objectsCommented:
> I'm not using the method setFont()

then how do you change the font for the text area?
0
 
gavrileAuthor Commented:
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
 
objectsCommented:
Ahh JTextPane, the mention of JTextArea confused me :)
0
 
gavrileAuthor Commented:
Hi objects,
Do you have any ideas regarding the font changes?
gabi
0
 
objectsCommented:
I'm guessing it is included in the AttributeSet.
Have a look at it's details and see what u can find.
0
 
gavrileAuthor Commented:
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
 
kylarCommented:
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
 
CleanupPingCommented:
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
All Courses

From novice to tech pro — start learning today.