• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 223
  • Last Modified:

Center align a JLabel

I am using a JLabel andassigning it an Icon. Please let me know how I can center it within a JTable cell.

Thanks!
0
dshrenik
Asked:
dshrenik
  • 4
  • 4
  • 2
1 Solution
 
ksivananthCommented:
by default icon is aligned center in the label!
0
 
for_yanCommented:

example aligning image and text on the label;

http://download.oracle.com/javase/tutorial/uiswing/components/label.html
0
 
dshrenikAuthor Commented:
I dont have any text. I'm doing this:

          JLabel label = new JLabel();
          label.setIcon(icon);
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
dshrenikAuthor Commented:
Im doing that inside the method "getTableCellRendererComponent()" in a class that extends DefaultTableCellRenderer
0
 
for_yanCommented:
Then should not be any problem
0
 
dshrenikAuthor Commented:
But, It is being left aligned!
0
 
ksivananthCommented:
the DefaultTableCellRenderer is itself a JLable, you don't have to create one, just calling setIcon( icon  ) would do!
0
 
ksivananthCommented:
check your image size and and display area!
0
 
ksivananthCommented:
and call below line after seticon,

setHorizontalAlignment(SwingConstants.CENTER);
0
 
dshrenikAuthor Commented:
I had to do
label.setHorizontalAlignment(SwingConstants.CENTER);

Thanks!
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 4
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now