How to add horizontal clearance using JLabel

I have JLabel (actually it is JXLabel).

I placed an icon and text on it.

<icon><text>

Now I made a wand to add some space on the left side of the component, for example:

<space><icon><text>

I DO NOT accept the suggestion to move JLabel or add spacing by changing the image.

I just want to know how to do this with simple Java code.

+6
java swing swingx jlabel
source share
4 answers

I have found a solution!

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

Thanks everyone!

+13
source share

Something like this: not very clear, but you can add an interval by adding a transparent border of a certain width to the label

+4
source share

If you are trying to press a label on one side of the container, you can add glue. Something like that:

 JPanel panel = new JPanel(); panel.setLayoutManager(new BoxLayout(panel, BoxLayout.LINE_AXIS); panel.add(new JLabel("this is your label with it image and text")); panel.add(Box.createHorizontalGlue()); 

Although your question is not very clear.

+2
source share

You do not need to change the preferred JLabel size, you can use the GridBagLayout Manager to specify the separation between the components, you only need to use the GridBagLayout in the container and add the JXLabel to it with the GridBagConstraints object containing the inserts on the left:

 JPanel panel=new JPanel(new GridBagLayout()); JLabel label=new JLabel("xxxxx"); GridBagConstraints constraints=new GridBagConstraints(); constraints.insest.left=X; // X= number of pixels of separation from the left component panel.add(label,constraints); 

Please note that I did not specify many configuration properties when setting the restrictions; it is better to read the GridBagLayout documentation

+1
source share

All Articles