How to add text + icon to the label?

Asked

Viewed 788 times

0

I’m in trouble, so I can add a JLabel with a word before an icon. I put the image in my package, and passed the way, the problem is that the label is concatenating with the String (the word I put before), and instead of leaving the word + the image, it shows me the path of the image.

What would be the right way to do it ?

what I did:

package teste;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class LabelIcone extends JFrame {

    public LabelIcone() {
        setSize(400, 200);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        ImageIcon icone = new ImageIcon(getClass().getResource("/imagens/icone.png"));
        JLabel label = new JLabel("Teste" + icone);
        add(label);
    }

    public static void main(String[] args) {
        LabelIcone lbIc = new LabelIcone();
        lbIc.setVisible(true);

    }
}

2 answers

2

Try it like this:


public LabelIcone() {
        setSize(400, 200);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        ImageIcon icone = new ImageIcon(getClass().getResource("/imagens/icone.png"));
        JLabel label = new JLabel(icone);
        label1.setHorizontalTextPosition(JLabel.LEFT);
        label1.setVerticalTextPosition(JLabel.BOTTOM);
        add(label);
    }

Label Text Position

  • this way there, you are not pasting a word/text before, only the icon I can put. Thanks more for responding !

1


To insert an icon into the label you need to load the image, convert to icone and then insert it into the label.

You can do using the setIcon in this way:

public LabelIcone() {
    setSize(400, 200);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    ImageIcon icone = new ImageIcon(getClass().getResource("/imagens/icone.png"));
    JLabel label = new JLabel("Teste");
    label.setIcon(icone);
    add(label);
}

Or as below:

To load the image from the resource I like to use the APache Commons IO, after importing this lib into your project, the first step is to create the method that will load the file:

public InputStream getStream(String pathResource) throws IOException {
    ClassLoader classLoader = getClass().getClassLoader();
    return IOUtils.toBufferedInputStream(classLoader.getResourceAsStream(pathResource));
}

Once you’ve created the method that loads, you’ll now need to create the method that converts the stream to Image:

public Image getImage(String path) throws IOException {
    InputStream stream = getStream(path);
    BufferedImage img = ImageIO.read(stream);
    return img;
}

Now the last step is to get this image that method getImage return and convert it to Icon and then put this icon on the label by the command setIcon, I created a method that does just this:

public void setIcon(String path, JLabel label) throws IOException {
    Image img = getImage(path);
    ImageIcon icone = new ImageIcon(img);
    label.setIcon(icone);
}

Now with some modifications your builder LabelIcone is this way:

public LabelIcone() {
    setSize(400, 200);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    JLabel label = new JLabel("Teste");

    try {
        setIcon("imagens/icone.png", label);
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }

    add(label);
}
  • a detail so small, that was the label.setIcon(icone); and I did not put. Solved my problem with the first way you answered, thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.