• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 227
  • 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
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

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