Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By doWhile

Thread: how can I change font size?

  1. #1
    rippon is offline Member
    Join Date
    Nov 2010
    Posts
    38
    Rep Power
    0

    Default how can I change font size?

    In a JPanel component, I can draw text:

    JPanel myPanel = new JPanel()
    myPanel.getGraphics().drawString("hallo world", 50, 50);

    But I can't see how to change the *size* of the text (which is point size 12).

    There is no 'setFontSize(...)' method.

    There *is* a setFont() method. But that looks like you have to define a new font of your own.

    So I'm thinking along these lines:

    myPanel.getGraphics().setFont(new Font("myFont", PLAIN, 14));

    I hope it's clear what I'm *trying* to do - something very trivial but nevertheless defeating me; and I hope you can help.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: how can I change font size?

    It'd be easier to get a reference to the graphics object and then use something similar to what you have done.

    Java Code:
    Graphics g = myPanel.getGraphics();
    Font f = new Font(Font.SANS_SERIF, Font.PLAIN, 20);
    g.setFont(f);
    g.drawString(...);
    //other graphics stuff

  3. #3
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: how can I change font size?

    Never call getGraphics() on a JComponent...do your drawing by overriding the paintComponent(Graphics g) method and use the parameter Graphics object. To change the font, call setFont on the component, passing the new font as the parameter. If you want to mix fonts, in the paintComponent method, set the font of the graphics object. The Font class has a deriveFont method to get a new reference to a Font object with the appropriate derived font (for example to change the font size - see the API for more information)
    DarrylBurke likes this.

  4. #4
    dmheli is offline Member
    Join Date
    Oct 2011
    Posts
    3
    Rep Power
    0

    Default Re: how can I change font size?

    You know you can actually use html tags on strings. Put the <html> tag at the beginning of the string you want to format.
    For example:

    String text = "<html><font size=4 color=blue> This is a string";

    Closing tags are optional, but you'll need it in some cases.

    Works in all JComponents.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: how can I change font size?

    Quote Originally Posted by dmheli View Post
    You know you can actually use html tags on strings.
    <snip/>
    Works in all JComponents.
    No, it doesn't work in *all* JComponents. And most relevant here, Graphics#drawString does not interpret HTML.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    dmheli is offline Member
    Join Date
    Oct 2011
    Posts
    3
    Rep Power
    0

    Default Re: how can I change font size?

    Quote Originally Posted by DarrylBurke View Post
    No, it doesn't work in *all* JComponents. And most relevant here, Graphics#drawString does not interpret HTML.
    Sorry, my mistake. you can only use HTML formatting on Swing components that display a text string as part of their GUI, like buttons , labels, tabbed panes, tables.

    But anyway, just for additional info.

  7. #7
    rippon is offline Member
    Join Date
    Nov 2010
    Posts
    38
    Rep Power
    0

Similar Threads

  1. Change the Font Size and Text Position
    By stayfierce in forum New To Java
    Replies: 1
    Last Post: 12-03-2009, 05:17 AM
  2. Replies: 5
    Last Post: 04-25-2009, 08:00 PM
  3. Replies: 4
    Last Post: 02-22-2009, 06:31 AM
  4. changing font size
    By diggitydoggz in forum New To Java
    Replies: 1
    Last Post: 12-25-2008, 07:48 AM
  5. how to set font size for a xml value
    By bala_kj in forum Advanced Java
    Replies: 3
    Last Post: 04-01-2008, 11:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •