Link to home
Start Free TrialLog in
Avatar of dshrenik
dshrenikFlag for United States of America

asked on

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!
Avatar of ksivananth
ksivananth
Flag of United States of America image

by default icon is aligned center in the label!
Avatar of dshrenik

ASKER

I dont have any text. I'm doing this:

          JLabel label = new JLabel();
          label.setIcon(icon);
Im doing that inside the method "getTableCellRendererComponent()" in a class that extends DefaultTableCellRenderer
Then should not be any problem
But, It is being left aligned!
the DefaultTableCellRenderer is itself a JLable, you don't have to create one, just calling setIcon( icon  ) would do!
check your image size and and display area!
ASKER CERTIFIED SOLUTION
Avatar of ksivananth
ksivananth
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I had to do
label.setHorizontalAlignment(SwingConstants.CENTER);

Thanks!