2

Im trying to add some html code to JButtons which later i make them look as cards, which look like bootstrap cards. I was able to add h3 h1 tittles but when i tried to add some imgs to my JButton Cards it is not printing it, instead, its printing some example picture from Java library or W/E.

My code is this:

JButton btn = new JButton("<html><img src=/" + j.getListaCartas().get(i).getUrlImagen() + "/  height=\"64px\" width=\"64px\"><h3>" + j.getListaCartas().get(i).getNombre() + "</h3><br>" + j.getListaCartas().get(i).getTipo() + "<br>" + j.getListaCartas().get(i).getATK() + "<br>" + j.getListaCartas().get(i).getDEF() + "<br>" + j.getListaCartas().get(i).getElemento() + "<br>" + "</html>");

the "getListaCartas().get(i).getUrlImagen()" returns the following:

C:\Users\sportak\Documents\NetBeansProjects\ThunderCards\ThunderCards\fondoCartas.png

which is the correct absolute route for the img, what is the problem then? JButton cant display img? Is not compatible?

Thank you team!

Jarvan Jarvencio
  • 689
  • 2
  • 12
  • 19
  • @Andrew - Though root-cause was similar in both the question but contexts are different. So marking duplicate will not much of help for the user. Maybe you can give a link to the other answer to get the solution. – Shahid Sep 22 '18 at 23:34

2 Answers2

1

Try This

URL url = getClass().getResource("xyz.png"); // If image is on the same folder of class
String text = "<html><body  border='0' width='48' height='48' background='" + url.toExternalForm() + "'></body></html>";
btn = new JButton(text);

I noticed that that by using url.toExternalForm, it also append file:/ along with the path. So it may needed to load an external image. Not sure though. However using toExernalForm does work.

Shahid
  • 481
  • 1
  • 8
  • 22
  • ive tried it but it throws me NullPointerException, also i tried to use the relative route of an example img instead of my class atributte url but nothing the exception – Jarvan Jarvencio Sep 23 '18 at 09:49
  • 1
    Anyway i've added an icon and it looks pretty great, its not the best hotfix but it will work for now, i will research more when i have more time, thnak you Shahid! – Jarvan Jarvencio Sep 23 '18 at 10:05
  • Good to know. It should work also. BTW did you change the name of image file or is there any image on your class path. The above code will work when you have xyz.png image on your class folder. You can change the name or path as you please. – Shahid Sep 23 '18 at 10:47
0

Looks like the file name needs to be specified as a URL resource name.

I found two ways to do this:

  1. Get the file name as a URL resource and use the string representation of the URL in the HTML.
  2. Specify the base parameter in the header of the HTML so all files will be relative to the base.

An example of both approaches is below:

import java.awt.*;
import javax.swing.*;
import java.net.*;

public class HTMLLocalImage
{
    private static void createAndShowGUI()
    {
        //  Convert file name to a URL resource

        URL url = HTMLLocalImage.class.getResource("bird.gif"); // If image is on the same folder of class
        String urlText =  "<html><body><h1>Image using path from URL</h1><br><img src='" + url.toExternalForm() + "'></body></html>";
        JLabel urlLabel = new JLabel( urlText );

        //  Spcify the base to be used for all file names

        String base = HTMLLocalImage.class.getResource("").toString();
        String baseText = "<html><head><base href='" + base + "'></head><body><h1>Image using path from BASE</h1><br><img src='bird.gif'></body></html>";
//      String baseText = "<html><head><base href='file:/c:/java/'></head><body><h1>Image using path from BASE</h1><br><img src='bird.gif'></body></html>";
        JButton baseButton = new JButton(baseText);

        JFrame frame = new JFrame("HTML Local Image");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout( new FlowLayout() );
        frame.add( urlLabel );
        frame.add( baseButton );
        frame.pack();
        frame.setLocationByPlatform( true );
        frame.setVisible( true );
    }

    public static void main(String[] args) throws Exception
    {
        EventQueue.invokeLater( () -> createAndShowGUI() );
/*
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowGUI();
            }
        });
*/
    }
}
camickr
  • 321,443
  • 19
  • 166
  • 288