JEditorPane:Como abrir arquivos html locais?

9 respostas
M
Eu estou fazendo uma aplicação que exibe uma ajuda em html.

O arquivo está no diretorio do projeto.

Estou tentando:  JEditorPane tab= new JEditorPane();

tab.setEditable(false);

tab.setPage(ajuda.htm);

Mas, tah dando erro!!!

O que faço???me ajudem!!!

9 Respostas

M

ei galera dar uma força!!!

F

Maryrose,
dei uma pesquisada na net e achei um fonte que pode te ajudar!!!

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

public class DisplayHtml{

  public DisplayHtml(String urlString){
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container con = frame.getContentPane();

    JEditorPane jep = new JEditorPane();
    JScrollPane jsp = new JScrollPane(jep);
    con.add(jsp);

    jep.setContentType("text/html");
    try{
      jep.setPage(urlString);
    }
    catch (Exception e){
      e.printStackTrace();
    }

    frame.setBounds(50, 50, 600, 800);
    frame.setVisible(true);
  }

  public static void main(String[] args){
    String ustr = "http://homepage1.nifty.com/algafield/";

    if (args.length > 0){ // local file URL should begin with file://
      ustr = args[0];     // ex.  file:///root/mytest.html
    }                     // ex.  file://C:\mytest.html
    new DisplayHtml(ustr);
  }
}

Espero que tenha ajudado!!!

[]´s

T

Ela quer ler de um arquivo local. Isso é possível, e o modo mais fácil é a partir de um “resource”.

http://java.sun.com/docs/books/tutorial/uiswing/components/editorpane.html

http://www.apl.jhu.edu/~hall/java/Swing-Tutorial/Swing-Tutorial-JEditorPane.html

M
 tentei usar:

Url ender= getClass().getResource(ajuda.html);

JEditorPane edit= new JEditorPane();

edit.setPage(ender);//não é aceito

não funciona!!!

Continuem mandando sugestões!!!
Agradeço!!!

M
JEditorPane editorPane = new JEditorPane();

editorPane.setEditable(false);

[color=red]java.net.URL helpURL = TextSamplerDemo.class.getResource(

TextSamplerDemoHelp.html);[/color]

if (helpURL != null) {

try {

editorPane.setPage(helpURL);

} catch (IOException e) {

System.err.println("Attempted to read a bad URL: " + helpURL);

}

} else {

System.err.println(Couldnt find file: TextSamplerDemoHelp.html);

}

//Put the editor pane in a scroll pane.

Acho que o trecho no codigo acima vai me ajuda.
Depois que testar digo se funcionou ou não.

Obrigado Faiter e thingol

JScrollPane editorScrollPane = new JScrollPane(editorPane);

editorScrollPane.setVerticalScrollBarPolicy(

JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

editorScrollPane.setPreferredSize(new Dimension(250, 145));

editorScrollPane.setMinimumSize(new Dimension(10, 10));

[color=blue][/color]

M

Obrigada, realmente funcionou!!!

Url ende= Aplicacao.class.getResource(“ajuda.html”);

JEditorPane edit= new JEditorPane(ende);

A

Opa pessoal… só um adendo à dúvida deste tópico, eu gostaria de abrir um arquivo html no browser padrão do sistema…

Existe algum comando em java pra isso?

F


Opa pessoal… só um adendo à dúvida deste tópico, eu gostaria de abrir um arquivo html no browser padrão do sistema…

Existe algum comando em java pra isso?

cara, tem sim:

try{
    Runtime.getRuntime().exec("C:/WINDOWS/system32/calc.exe");
}catch(Exception re){
    JOptionPane.showMessageDialog(null, "Seu computador não possui uma calculadora instalada!");
}

bruxo, faz tempo que não mecho nisso. ai em cima eu uso pra abrir a calc do rwindows, mas te digo q o caminho é esse mesmo ;)
W

Sei que o tópico é antigo mas usa algo assim cara:

Windows:

           Runtime.getRuntime().exec("rundll32 SHELL32.DLL, ShellExec_RunDLL "+temp.getAbsolutePath());
Criado 20 de agosto de 2006
Ultima resposta 12 de mar. de 2010
Respostas 9
Participantes 6