Help to show a HTML file in Swing

Hiiiiiiiiiii,
During my development Me in a problem . Please help me out.
As per my current context of the project I have to develop a HTML page with Help of CSS and all other things. After that I have to Show the HTML file through Swing. I know how to attach a file with help of URL but need some help to show a HTML file in Swing.
*help to show a HTML file in Swing*

As camickr said Javascript support is not available. If you want to support you have to write code for that. Up to an extent CSS support is provided.
Sample code to display HTML pages in JTexPane (you can use JEditorPane as well):
Construct the URL:
URL url = YourClass.class.getResource("resources/Hello.html");
// If YourClass is in package test.html, then your html page must be available in .....test/html/resources directory.
// And your CSS files must be accessible from your HTML page. This you can test by simply opening the html
// page in your favorite browser.And then call setPage() method of HelpDataPane. Note that, HelpDataPane supports hyperlink activation.
import java.io.IOException;
import java.net.URL;
import javax.swing.JTextPane;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
* A pane to display help pages. Help pages are accessed as URL and displayed
* in this pane.
* @author Mrityunjoy Saha
* @version 1.0
public class HelpDataPane extends JTextPane {
     * Creates a new instance of {@code HelpDataPane}.
    public HelpDataPane() {
        super();
        this.addHyperlinkListener(new HyperlinkListener() {
            public void hyperlinkUpdate(HyperlinkEvent e) {
                if (e.getEventType().equals(HyperlinkEvent.EventType.ACTIVATED)) {
                    setPage(e.getURL());
     * Sets the current URL being displayed.  The content type of the
     * pane is set, and if the editor kit for the pane is
     * non-{@code null}, then
     * a new default document is created and the URL is read into it.
     * @param page The URL to be displayed.
    @Override
    public void setPage(URL page) {
        try {
            super.setPage(page);
        } catch (IOException ex) {
            try {
                URL blankURL = YourClass.class.getResource("resources/Blank.html");
                super.setPage(blankURL);
            } catch (IOException ioe) {
                ioe.printStackTrace();
}Thanks,
Mrityunjoy

Similar Messages

  • How to show an HTML file (more then 1MB size) in JEditorPane

    I an trying to show an HTML file (more then 1 MB size ) in JEditorPane
    but getiing OutOfMemory error.
    if i try the same for size less then 1 MB(i tried till 850KB) then it will work fine.
    I am using setPage() method of JEditorPane.
    please provide solution .

    I meet the same question, and even worse.
    I created a html file with frameset, when i point to the link which is an html file larger than 500kb . it fails. and some error "infinite loop in formatting" is thrown
    in javax. swing.text.FlowView. I read the code of this class, and can't tell what's the error of my application.
    Can someone help and give some answer?

  • Help Needed in opening a HTML File from swing application

    Hi,
    I am opening a HTML file in Internet Explorer from my swing application.
    I am using the code given below
    private final static String WIN_FLAG = "url.dll,FileProtocolHandler";
    private final static String WIN_PATH = "rundll32";
    String cmd = WIN_PATH + " " + WIN_FLAG + " " + url;
    // url is HTML file Path
    Process p = Runtime.getRuntime().exec( cmd );
    The HTML file is opening up,but it always opens behind the swing application,that makes me every time to maximize the HTML file after it being opened up.
    I want to open it in front of the Swing Application.
    Any Help Please ?
    - Manikandan

    Check your Application's JFrame properties (may be ur Frame is set to be in top position ,always),and also check running your application in some other OS.

  • Kindly help me how to view html file in swing using jbutton

    hello..im a begginer at j2se language and im working on my school project..kindly help me how to view html files by clicking button at javax.swing..thanks

    Here you go:public class HTMLViewer implements Runnable, ActionListener
         public void run() {
              JFrame frame = new JFrame("View HTML");
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              JButton button = new JButton("Click me!");
              button.addActionListener(this);
              Container cp = frame.getContentPane();
              cp.add(button, BorderLayout.CENTER);
              frame.pack();
              frame.setLocationRelativeTo(null);
              frame.setVisible(true);
         public void actionPerformed(ActionEvent e) {
              if (Desktop.isDesktopSupported()) {
                   Desktop d = Desktop.getDesktop();
                   try {
                        d.browse(new URI("www.google.com"));
                   } catch (IOException ex) {
                        ex.printStackTrace();
                   } catch (URISyntaxException ex) {
                        ex.printStackTrace();
         public static void main(String[] args) {
              EventQueue.invokeLater(new HTMLViewer());
    }If that's not what you're looking for you need to ask more specific questions.

  • How to create html files using Swing

    I am developing a billing system in java. I want to generate cash memo and other related documents in HTML format. I have explored html package in swing but without result. Please help me.
    Arshad

    Hi,
    I agree with the previous poster. An additional way of generating HTML would be to directly write it. Class SHTMLWriter in package com.lightdev.app.shtm of application SimplyHTML at http://www.lightdev.com/template.php4?id=3 gives this opportunity.
    With this, you can do something such as   /**
       * example of synthesizing HTML code to a Writer using
       * class SHTMLWriter
       * @param fileName the name of the file to write HTML to
      public void writeHTML(String fileName) {
        try {
          FileWriter fw = new FileWriter(fileName);
          SHTMLWriter w = new SHTMLWriter(fw);
          w.startTag(HTML.Tag.HTML.toString(), null);
          w.startTag(HTML.Tag.BODY.toString(), null);
          SimpleAttributeSet set = new SimpleAttributeSet();
          set.addAttribute(CSS.Attribute.FONT_FAMILY, "SansSerif");
          set.addAttribute(CSS.Attribute.FONT_SIZE, "12");
          w.startTag(HTML.Tag.P.toString(), set);
          fw.write("some text");
          w.endTag(HTML.Tag.P.toString());
          w.endTag(HTML.Tag.BODY.toString());
          w.endTag(HTML.Tag.HTML.toString());
        catch(Exception e) {
          e.printStackTrace();
      }The HTML output is hard coded in this example. Certainly any HTML code can be created dynamically/programmatically instead. Class SHTMLWriter is partly meant as a convenience class for writing HTML. One can just through start and end tags as in above example. In the start tag any valid AttributeSet can be given.
    HTH
    Ulrich

  • HTML file in Swing

    I need to call an HTML file in the Swing application. I am not able to so that. Can some one help with that. My basic idea is to open an application on the system start up. The application should contain the HTML file. Any help would be appreicaited.

    try this link n go thru the file:
    http://examples.oreilly.com/jswing2/code/ch23/MiniBrowser.java

  • How to embed html-file into swing

    hello,
    can anybody help me how to embed a html-file into a swing application??
    I try to write a swing-application, that connects to a Internetpage via Sockets.
    The problem is, that I can only see the sourcecode of the InternetPage in my JTextArea, but I want to see the whole Page just like in a browser.
    I hope anybody can help me to solve this problem, thanx mina

    u will need to use the JEditorPane component instead of Jtextarea.here is a url to help u get started
    http://java.sun.com/docs/books/tutorial/uiswing/components/simpletext.html#editorpane

  • Please help -  How to export HTML file into MS word format

    How to export HTML file into MS word file without any change
    in design format?
    As I have been trying to do that but problem is coming in
    formating. Table's witdth and height get disturbed in MS word when
    I import the html file.
    What I do after creating HTML in dreamweaver I save that file
    into .txt format in dreamweaver. and then import it in MS Word. And
    I tried too much for it but invain.
    Please help me in finding the way of solution.
    Please help me as my job is in trouble. It's very urgent
    pleeease help me. I'll be thankful to you.

    Just open the HTML file in Word.
    Walt
    "apollo321" <[email protected]> wrote in
    message
    news:gavbhd$6j4$[email protected]..
    > How to export HTML file into MS word file without any
    change in design
    > format?
    >
    > As I have been trying to do that but problem is coming
    in formating.
    > Table's
    > witdth and height get disturbed in MS word when I import
    the html file.
    >
    > What I do after creating HTML in dreamweaver I save that
    file into .txt
    > format
    > in dreamweaver. and then import it in MS Word. And I
    tried too much for it
    > but
    > invain.
    >
    > Please help me in finding the way of solution.
    >
    > Please help me as my job is in trouble. It's very urgent
    pleeease help me.
    > I'll be thankful to you.
    >

  • Show local html file

    How do you do this. I tried the following which works nice on Windows but fails on MacOSX:
    LrHttp.openUrlInBrowser(_PLUGIN:resourceId "doc.html")
    Ho do YOU show a local helpfile (pdf or html) cross-platform-compatible? Thanks in advance.

    I did not try "execute" yet, because I had in my mind that in windows the parameter for the application can be omitted and on a Mac you must give the application to execute.
    That's correct.  On the Mac, you need to use the "open" program, which will open any file in its native application, e.g.
    open "/Users/username/Library/Application Support/Adobe/Lightroom/Plugins/PluginName.lrdevplugin/help.html"
    Here is the code my Any File plugin uses to open any arbitrary file in its native application:
            if WIN_ENV then
                LrShell.openFilesInApp ({""}, file)
            else
                LrShell.openFilesInApp ({file}, "open")
                end
    It occurs to me that I may not have explicitly tested this with filenames containing spaces on the Mac, so I don't know if the openFilesInApp() function handles spaces correclty.   If it doesn't, then you can always drop down and call LrTasks.execute() directly.

  • I need to show an html file from an application

    Hi there!, I really need your help...
    I have the absolute path in an String variable:
    String fileHTML = "C:\\AllCitiHTML\\GeneralHTML_Files\\Temp\\2002030925General.htm";
    Now, I need to show this page in a browser(explorer or netscape) but the page must be showed in both.
    I tried with this code, but it doesn't show nothing
    String urlName = fileHTML;
    String browser = "iexplore ";
    Runtime.getRuntime().exec(browser + urlName);
    in addition it would only serve for explorer and not for netscape.
    reply this message with all the possible code that you can use...
    Please I need help very urgent, thanks

    Multiple cross-post.

  • Program not showing in open with context for HTML files

    I have a windows 8.1 64 Pro PC with a program called 1stPage.exe installed.
    When I right click a .txt file I get 'Edit with 1st Page' in the context menu and it also shows in the open
    with menu.
    However HTML files do not show these options. I have checked the registry and it would appear that the program
    is set up on the open with menu exactly the same for both file types.
    So it would appear that something is blocking 1stPage from showing against HTML files but I can't figure out
    what I need to change.
    Cheers
    Baz

    OK we have further developments
    I tried installing this program on a Windows 7 32bit machine and I get exactly the same issue.
    I tried it with both IE and chrome as default browsers and I tried changing default html editor.
    I therefore suspect this when this program installs it is not setting all the registry entries windows 7/8
    require.
    I know it's an old program but I have been using it for years and see no reason to change.
    So I need help in understanding what I need to do to get windows 7/8 to display the context menu entry correctly

  • JEditorPane problem with showing html file

    Hey,
    I have a JEditorPane in which I would like to show a HTML file that is stored in the same directory (local, not from the web).
           String url = "index.html";
            try
                JEditorPane htmlPane = new JEditorPane(url);
                htmlPane.setEditable(false);
                frame.add(new JScrollPane(htmlPane));
            catch(IOException ioe)
                System.err.println("Error displaying " + url);
            } It should also work once the whole program is "packaged" into a .jar file.
    With the code above I cant get the index.html file to be displayed in the htmlPane.
    Any suggestions?
    thanks.

    The JEditorPane has a method called setPage that loads a URL into the pane, you can always transform internal jar locations to URLs using e.g. CurrentObject.class.getResource("/internal/jar/location/text.html")

  • Writing html file in a jeditorpane

    I have read one of your threads titled as "How to insert a string in a JEditorPane (Urgent)". according to this thread I am using insertString() method to write in an jeditorpane. what I am writing is an html file. when I used insertString() method it doesn't show the html content in my jeditotpane. it displays the source code of my html file. I have setContentType("text/html"); moreover I used setText() and it shows the html file correctly but it over writes the previous content of jeditorpane. please help me to be able to
    write to a jeditorpane without overwriting and displaying html file properly not its source code.
    following is my code:
             fr = new FileReader("myfile.html");
         BufferedReader br = new BufferedReader(fr);
         while ((lineread = br.readLine()) != null)
              find.append(lineread);
                       do{
                             last = find.lastIndexOf("str");
                          Integer index = myJEditorPane.getSelectionEnd();
                   String temp = index.toString();     
                   start=find.indexOf(temp, end);
                   end=find.indexOf("str", start);
                            Document doc = mynewContentPane.getDocument();
                   try{
                        doc.insertString(doc.getLength(),find.substring(start, end),null);
                   catch(BadLocationException exc){
                        exc.printStackTrace();
                   }Edited by: nasi on May 7, 2010 8:59 PM
    Edited by: nasi on May 8, 2010 12:09 AM
    Edited by: nasi on May 8, 2010 1:37 AM

    Welcome to the Sun forums.
    Subject: writing html file in a jeditorpane
    If you mean a JEditorPane then please use the correct case, so we can be confident you refer to the J2SE class, as opposed to a 3rd party jeditorpane that we neither know, nor support.
    nasi wrote:
    I have read one of your threads titled as "How to insert a string in a JEditorPane (Urgent)". ..I guess you mean [How to insert a string in a JEditorPane (Urgent)|http://forums.sun.com/thread.jspa?threadID=282569] from 2002. A lot has changed since 2002, but I suspect the basic way of appending text to a Document has not.
    BTW - Good to see you arrive with some Google-foo. ;-)
    ..according to this thread I am using insertString() method to write in an jeditorpane. what I am writing is an html file. when I used insertString() method it doesn't show the html content in my jeditotpane. it displays the source code of my html file. I have setContentType("text/html"); moreover I used setText() and it shows the html file correctly but it over writes the previous content of jeditorpane. I've not played much with changing existing documents, but try this source (adapted from code nabbed from the thread you mentioned).
    import javax.swing.text.*;
    import javax.swing.*;
    public class InsertStringTest {
        public static void main(String[] args){
            Runnable r = new Runnable() {
                public void run() {
                    //set GUI
                    JFrame f = new JFrame();
                    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    JEditorPane editorPane = new JEditorPane();
                    editorPane.setContentType("text/html");
                    editorPane.setText("<html><body><h1>Test!</h1><p>");
                    f.getContentPane().add(new JScrollPane(editorPane));
                    String testStr = "This is a test string. " +
                        "This is a test string. This is a test string. \n";
                    f.pack();
                    f.setSize(400,300);
                    f.setVisible(true);
                    //test with insertString()
                    long diff;
                    long startTime = System.currentTimeMillis();
                    Document doc = editorPane.getDocument();
                    for(int i=0; i < 500; i++){
                        try{
                            doc.insertString(doc.getLength(),testStr,null);
                        catch(BadLocationException exc){
                            exc.printStackTrace();
                    diff = System.currentTimeMillis() - startTime;
                    System.out.println("Time spent with doc.insertString() : " + diff);
                    editorPane.setCaretPosition(0);
            SwingUtilities.invokeLater(r);
    }

  • Trying to display a local HTML file

    Hi all,
    My program saves an HTML file to disc. If the user presses a button I want to display a new window that shows the HTML file. I know the file is good because I have viewed it in Internet Explorer. I have trawled the forums and got the following code. But i can't understand why its not working.
    Any help would be much appreciated.
    p.s. The HTML file was saved as a string so is all on one line. But IE doesn't seem to mind
    Cheers
    drb2k2
    import javax.swing.*;
    import java.awt.*;
    import javax.swing.text.StyledEditorKit;
    import java.awt.event.*;
    import java.io.*;
    public class DisplayHTML extends JFrame implements ActionListener
         Color Background = new Color(100, 135, 220);
         Color Buttons     = new Color(140, 170, 230);
         Container content;
         public DisplayHTML()
              UIManager.put("Button.select", Buttons);     
              content = getContentPane();
              content.setBackground(Background);
              BorderLayout border = new BorderLayout();
              content.setLayout(border);
              JPanel Button_Panel = new JPanel();
              BorderLayout buttons = new BorderLayout();
              Button_Panel.setLayout(buttons);
              Button_Panel.setBackground(Background);
              JButton Save = new JButton("Save");
              Save.setBackground(Buttons);
              Save.setToolTipText("Press here to save the results file in HTML format");
              Save.addActionListener(this);
              Save.setActionCommand("Save");
              Button_Panel.add(Save, BorderLayout.CENTER);
              JButton Quit = new JButton("Quit");
              Quit.setBackground(Buttons);
              Quit.setToolTipText("Press here to exit the screen and loose results");
              Quit.addActionListener(this);
              Quit.setActionCommand("Quit");
              Button_Panel.add(Quit, BorderLayout.EAST);
              content.add(Button_Panel, BorderLayout.NORTH);
              String line = "hoof";
              try
                  BufferedReader reader = new BufferedReader(new FileReader("analysis.html"));
                  while ((line = reader.readLine()) != null)
                   System.out.println("Line is: " + line);
                  reader.close();
              catch (IOException e)
                   //CLEAN THIS UP
                  System.out.println(e);
                  System.exit(1);
              JTextPane HTML_Pane = new JTextPane();
              try
                   HTML_Pane.setPage("analysis.html");
              catch(IOException f)
              {System.out.println("uff");}
              content.add(HTML_Pane, BorderLayout.CENTER);
              setVisible(true);
         public void actionPerformed(ActionEvent e)

    <h1>oops</h1> forgot to tell you what the errors are:
    a new window does pop up that is tiny.
    When I drag it out the two buttons are there but the area below is just the blank textArea.
    Not surprisingly this is caused because the IOException is called.
    The command prompt screen shows "uff" so there is an error loading the file. Is there a way that I can just use the "line" variable?
    Can't understand why its not loading because it loads fine for the buffered reader.
    Cheers
    drb2k2

  • HTML file is not being shown properly in the JEditorPane

    Hi,
    I am using JEditorPane to display an HTML file from the local disk. This HTML file contains the html tables. Now when this file is getting displayed in the JEditorPane, one top row grid is not being displayed in the editor pane. content of the row is there...but the column grid is missing. All other rows and columns are being shown but the first row-column grid which contains the heading for column.
    Also when I m printing the content of this JEditorPane using Java Print API then no grid is being printed on the paper. content is coming properly but no table grids. when i have taken the print out of the original html file from the browser then table grids are being printed out properly.
    Please do help me out in showing the HTML file in the JEditorPane properly and printing the same.
    Many Thanks,
    gshankar

    Hi,
    JEditorPane renders HTML with many limitations.
    You can use JDIC for the same. refer: jdic.dev.java.net
    But JDIC does not work on windows 98.
    Anand

Maybe you are looking for

  • How do I add a table to a preformed template that I got ?

    how do I add a table to a preformed template that I got ? I have tried inserting a table but it always ends up inside another table already created. But I want a brand new fresh table but cannot seem to be able to get the cursor in the empty area.

  • Alpha Video... need to convert SWF to FLV

    I am working on a video ad that includes video that was shot using a green screen. The video has been masked and brought into Flash 8, where background effects were added. Everything is working perfectly with flash, but I need the final product as an

  • Wrong information in Metadata Repository

    Hey Guru's, I searched for objects in the BI Metadata Repository and found an error. I looked für a query and the corresponding workbook. But I found another workbook that doesn't exist anymore. As a goal, I'd like to have a screenshot for project do

  • 1-511-035-475-549 Reference # in regards to my palm 111c

    I have a palmIIIc that is no longer able to work.  It does not turn on, it has not been connected to my computer, so I am unable to receive the information I saved on it.  I was wondering if there was an internal battery that I need to replace, so I

  • FRC from Panasonic for HVX-200???

    Hi everyone, I've just downloaded from Panasonic thr FRC(frame rate converter) to work in PN in my FCP 5.1.2 timeline.I have a MacBookpro 17". The problem is it does not appear in my tools menu like it should do. Any suggestions!!! Thanks, Daniel Mac