1

I tried to put a simple icon in a JPanel formatted with the BoxLayout.

    JPanel panel_4 = new JPanel();
    contentPane.add(panel_4, BorderLayout.CENTER);
    panel_4.setLayout(new BoxLayout(panel_4, BoxLayout.X_AXIS));

    ImageIcon seven= new ImageIcon("‪C:\\Users\\alewe\\workspace\\SlotMachine\\Lucky_Seven-128.png");

    JLabel lblNewLabel_1 = new JLabel(seven);
    panel_4.add(lblNewLabel_1);

When I ran the code it gave me the error "Some characters cannot be mapped using "Cp1252" character encoding", I saved by UTF-8, now it starts but I can't see the icon.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Yuri
  • 39
  • 6
  • [this](http://stackoverflow.com/questions/1242581/display-a-jpg-image-on-a-jpanel) might help – LuKyY Nov 22 '16 at 10:14

2 Answers2

1

Maybe if you use setIcon will help you:

ImageIcon seven= new ImageIcon("‪C:\\Users\\alewe\\workspace\\SlotMachine\\Lucky_Seven-128.png");
JLabel lblNewLabel_1 = new JLabel();
//Set your icon to your label
lblNewLabel_1.setIcon(seven);
panel_4.add(lblNewLabel_1);

You can read more about icons here

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
0

you will need a inputstream to read the picture. use it like this:

File f = new File("filepath");
        InputStream in=new FileInputStream(f);
            if (in != null) {
                ImageIcon imageIcon = new ImageIcon(ImageIO.read(in));
                label.setIcon(imageIcon);
            } else {
                LOG.debug("No icon found...");
            }
XtremeBaumer
  • 6,275
  • 3
  • 19
  • 65