Results 1 to 7 of 7

Thread: Component align

  1. #1
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default Component align

    I tryed few layouts to get a simple component to the middle of the screen and I always failed.
    My single goal is to get it to the middle by X and Y.
    How do I do that?

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

    Default Re: Component align

    Quote Originally Posted by tnrh1 View Post
    I tryed few layouts to get a simple component to the middle of the screen and I always failed.
    components are placed relative to their container, not the screen. Exactly what are you trying to do? Center a component in a container? Center a Window on the desktop (screen)? The details matter.

    My single goal is to get it to the middle by X and Y.
    How do I do that?
    middle by X and Y?

    Again, please clarify, again please tell the details.

  3. #3
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default Re: Component align

    I have a JFrame and a label.
    I want the label to be in the middle of the JFrame.
    The JFrame is set to full screen with no borders so I assume that the middle of the JFrame is the middle of the screen.
    "By x and y" I mean that I want it to be centered x alignment and y alignment.
    Thanks!

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

    Default Re: Component align

    The easiest way I know to center a component in the container is to have the container user GridBagLayout, and add the component to the container without any GridBagConstraints. So for your situation, call on your JFrame, getContentPane().setLayout(new GridBagLayout()), and then add the JLabel to the JFrame.

  5. #5
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default Re: Component align

    Thank you so much!

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

    Default Re: Component align

    You're quite welcome!

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

    Default Re: Component align

    Moved from New to Java

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

Similar Threads

  1. How to align in java
    By deemu in forum New To Java
    Replies: 2
    Last Post: 02-06-2012, 08:50 PM
  2. Align Row to Right in JTable
    By shomid in forum AWT / Swing
    Replies: 2
    Last Post: 11-25-2011, 10:23 PM
  3. Align left
    By britto_bicsjohn in forum AWT / Swing
    Replies: 2
    Last Post: 09-09-2009, 04:05 AM
  4. How to align the output on console?
    By sfe23 in forum New To Java
    Replies: 5
    Last Post: 03-30-2009, 03:28 AM
  5. JTextArea - text align
    By bradder in forum AWT / Swing
    Replies: 1
    Last Post: 11-29-2007, 07:08 PM

Posting Permissions

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