user1433804
user1433804

Reputation: 657

JLabel with Icon aligned to left

I have a JLabel with a Icon and text aligned to bottom, the icon is center to the text, how do i have the Text and the icon aligned to left and the text below the icon.

My current code is as below

label.setIcon(new ImageIcon(fileName));
label.setText("This text is going to be varying size and can be bit long");

label.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
label.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

Upvotes: 4

Views: 1479

Answers (2)

leo08
leo08

Reputation: 1

your just setting the Position of your text in your code..not the Icon it self

Try to separate the icon in the label then format it using set bounds or any formlayout

Upvotes: 0

MadProgrammer
MadProgrammer

Reputation: 347314

I cheated

If you mean someting like this, then I cheated.

Basically, I created two JLabels on a JPanel. Using a GridBagLayout I aligned the two labels onto of each other and anchored to the WEST

It would pretty easy to create a custom component for the purpose if you wanted to reuse it.

Upvotes: 5

Related Questions