Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a horizontal gap with a JLabel

I have a JLabel (actually, it is a JXLabel).

I have put an icon and text on it.

<icon><text>

Now I wand to add some spacing on the left side of the component, like this:

<space><icon><text>

I DON'T accept suggestion to move the JLabel or add spacing by modifying the image.

I just want to know how to do it with plain java code.

like image 950
michelemarcon Avatar asked Oct 01 '08 10:10

michelemarcon


3 Answers

I have found the solution!

setBorder(new EmptyBorder(0,10,0,0));

Thanks everyone!

like image 71
michelemarcon Avatar answered Sep 21 '22 21:09

michelemarcon


The like this: is not very clear, but you can add spacing by adding a transparent border of a certain width to the label

like image 28
Jasper Avatar answered Sep 21 '22 21:09

Jasper


If you're trying to push the label to one side of it's container, you can add a glue. Something like this:

JPanel panel = new JPanel();
panel.setLayoutManager(new BoxLayout(panel, BoxLayout.LINE_AXIS);

panel.add(new JLabel("this is your label with it's image and text"));

panel.add(Box.createHorizontalGlue());

Though your question isn't very clear.

like image 45
rjohnston Avatar answered Sep 24 '22 21:09

rjohnston