How to add text + icon in the label?

0

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

What would be the correct 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);

    }
}
    
asked by anonymous 11.05.2017 / 07:06

2 answers

1

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

You can do this using setIcon 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("Teste");
    label.setIcon(icone);
    add(label);
}

Or as below:

To load the image from resource I like to use A pache Commons IO , after doing the import of this lib in 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));
}

After the load method is created, you will 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 the getImage method returns and convert it to Icon and then put this icon on the label with 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 LabelIcone constructor looks like this:

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);
}
    
11.05.2017 / 13:32
2

Try 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

    
11.05.2017 / 13:34