Results 1 to 5 of 5
  1. #1
    Zman3359's Avatar
    Zman3359 is offline Member
    Join Date
    Jan 2011
    Posts
    42
    Rep Power
    0

    Question Center text in a JComponent?

    How would you go about centering a String in a JComponent? I'm drawing to the JComponent using a Graphics object, so the String is drawn using drawString().

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    The easiest way is to add a JLabel to it that has its horizontalAlignment property set to SwingConstants.CENTER. If you give the JComponent a BorderLayout and add the JLabel to the BorderLayout.CENTER position then it will fill the component. Otherwise, you may need to muck with FontMetrics, something I've not yet done.

  3. #3
    Zman3359's Avatar
    Zman3359 is offline Member
    Join Date
    Jan 2011
    Posts
    42
    Rep Power
    0

    Default

    The JComponent isn't going to hold any other components though. The JComponent is just a canvas for the program to draw on, that's why im using drawString() to put the message on the screen. I also don't like messing with layout's I never got the hang of 'em. So I will look into FontMetrics and see what I can find about that. Thanks.

    EDIT: Found a way using FontMetrics. Here is an example for future reference:
    fontName = the name of the font you want to use
    g = the graphics object your drawing on
    width = the width of the area you want the text centered in
    height = the height of the area you want the text centered in

    Java Code:
    Font font = new Font(fontName,Font.PLAIN,16);
    FontMetrics fm = g.getFontMetrics(font);
    g.drawString("Hello",(width - fm.stringWidth("Hello")) / 2,(height - font.getSize()) / 2);
    Last edited by Zman3359; 04-17-2011 at 10:29 PM.

  4. #4
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    I also don't like messing with layout's I never got the hang of 'em.
    Learning to use Swing properly by using the provided components and the layout managers is far easier than reinventing the wheel and creating your own component every time.

    I agree with Fubarable, a JLabel is the way to go.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    Quote Originally Posted by Fubarable View Post
    The easiest way is to add a JLabel to it that has its horizontalAlignment property set to SwingConstants.CENTER. If you give the JComponent a BorderLayout and add the JLabel to the BorderLayout.CENTER position then it will fill the component. Otherwise, you may need to muck with FontMetrics, something I've not yet done.
    Another easy way is to set a GridBagLayout and add a JLabel without specifying a GridBagConstraint.

    db

Similar Threads

  1. Replies: 2
    Last Post: 10-01-2010, 08:18 PM
  2. Replies: 0
    Last Post: 09-28-2010, 10:52 AM
  3. Replies: 2
    Last Post: 08-08-2010, 09:07 PM
  4. Java: How do i align the text in the center?
    By nicacutenica in forum New To Java
    Replies: 5
    Last Post: 06-29-2010, 07:50 PM
  5. Replies: 4
    Last Post: 06-08-2007, 07:15 AM

Tags for this Thread

Posting Permissions

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