sono riuscito a inserire una pagina html in un JEditorPane il problema è che tutto viene caricato bene tranne le immagini...come può essere?
sono riuscito a inserire una pagina html in un JEditorPane il problema è che tutto viene caricato bene tranne le immagini...come può essere?
ecco il codice:
codice:/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package lupix.school; import java.io.File; import javax.swing.*; import java.io.*; import java.awt.*; /** * * @author giordano */ public class guide { static JEditorPane page; public guide() { JFrame guide = new JFrame("Guida"); JPanel gp = new JPanel(); gp.setLayout(new BorderLayout()); String html = ""; page = new JEditorPane(); page.setContentType("text/html"); Container gc = guide.getContentPane(); gc.add(gp); File g = new File("guide/guide.html"); try { FileInputStream fis1 = new FileInputStream(g); page.read(fis1, null); } catch (FileNotFoundException a) { a.printStackTrace(); } catch (IOException a) { a.printStackTrace(); } page.setEditable(false); JScrollPane st = new JScrollPane(page); st.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); st.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); st.setPreferredSize(new Dimension(800, 600)); gp.add(st, BorderLayout.PAGE_START); guide.pack(); guide.setResizable(false); guide.setLocationRelativeTo(null); guide.setVisible(true); } }
La documentazione di JEditorPane dice:Originariamente inviato da Metflar
sono riuscito a inserire una pagina html in un JEditorPane il problema è che tutto viene caricato bene tranne le immagini...come può essere?
The read method can be used to initialize the component from a Reader. Note that if the content type is HTML, relative references (e.g. for things like images) can't be resolved unless the <base> tag is used or the Base property on HTMLDocument is set. In this case the current EditorKit will be used, and the content type will be expected to be of this type.
La soluzione più semplice è usare un URL e caricare la risorsa con getResouce:
codice:URL url = getClass().getResource("guide/guide.html"); editorPane = new JEditorPane(url);
Andrea, andbin.dev – Senior Java developer – SCJP 5 (91%) • SCWCD 5 (94%)
java.util.function Interfaces Cheat Sheet — Java Versions Cheat Sheet