Visualizzazione dei risultati da 1 a 3 su 3
  1. #1
    Utente di HTML.it L'avatar di Metflar
    Registrato dal
    Apr 2007
    Messaggi
    790

    caricare pagina html in JEditorPane[java]

    sono riuscito a inserire una pagina html in un JEditorPane il problema è che tutto viene caricato bene tranne le immagini...come può essere?

  2. #2
    Utente di HTML.it L'avatar di Metflar
    Registrato dal
    Apr 2007
    Messaggi
    790
    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);
        }
    }

  3. #3
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284

    Re: caricare pagina html in JEditorPane[java]

    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?
    La documentazione di JEditorPane dice:

    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.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.