• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • salvin francis
  • Stephan van Hulst
  • Frits Walraven
  • Carey Brown
  • Jj Roberts

Centering a JComponent in the CENTER area of BorderLayout?

Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I've got a JFrame whose contentPane has the typical BorderLayout setup.

I want to put a JComponent in the BorderLayout.CENTER area, so I do the following:

Naturally, since myJComponent is the ONLY thing that's been added, since it's in the CENTER, the component is expanded to fill the entire contentPane, as the red border will indicate.

HOWEVER, the actual visible part of myJComponent then gets shoved into the upper-left corner, whereas I want it centered both horizontally and vertically.

I've tried using setAlignmentX and setAlignmentY on the myJComponent to no avail. I have a feeling I'm missing something dreadfully obvious, but can't figure out what it is.

Thanks in advance for any guidance anyone can give me on this.
[ February 22, 2008: Message edited by: Joe Vahabzadeh ]
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
simple demo using GridBagLayout and a JLabel (don't know what your JComponent is)

It wasn't my idea to go to some crazy nightclub in the middle of nowhere. I just wanted to stay home and cuddle with this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic