Icon in JTabbedPane not showing

I have a problem with my tabs:

JTabbedPane tab = new JTabbedPane(); frame.add(tab, BorderLayout.CENTER); JPanel contact = new JPanel(); contact.add(backgroundContact); tab.add("Contacto", contact); //tab.addTab("Contacto",new ImageIcon("images/image2.gif"), contact,""); JPanel schedule = new JPanel(); schedule.add(backgroundSchedule); tab.add("Horario", schedule); //tab.addTab("Horario", new ImageIcon("images/image2.gif"), schedule,""); JPanel cost = new JPanel(); cost.add(backgroundCost); tab.add("Tarifas", cost); //tab.addTab("Tarifas", new ImageIcon("images/image3.gif"), cost,""); // Los iconos tab.setIconAt(0, new ImageIcon("images/image1.gif")); tab.setIconAt(1, new ImageIcon("images/image2.gif")); tab.setIconAt(2, new ImageIcon("images/image3.gif")); 

I tried both options, but the icons are not showing. Why is this happening?

I also tried: new ImageIcon("images/im.gif") , which does not exist and I have some kind of error

0
source share
1 answer

Try this instead:

 URL urlToImage3 = this.getClass().getResource("/" + "images/image3.gif"); ... new ImageIcon(urlToImage3); 

You could concatenate "/" + "images/image3.gif" - I just wanted to highlight the leading / , since it is more reliable for searching from the root of the class path.

If these images are an โ€œembedded resource,โ€ as I suspect, they will not be available in File at runtime, but should be in the class path in one of the applicationโ€™s banners. Therefore, an accessible URL .

+6
source

Source: https://habr.com/ru/post/914915/


All Articles