Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Show html page in Java application

Status
Not open for further replies.

dtqui

Programmer
Sep 25, 2000
62
0
0
SG
Hi,

after looking through the forum, there doesn't seem to have a thread that tries to show html page directly in Java application without opening the internet explorer.

I've tried the example listed in but it shows in another window.
I've also tried example in which uses JEditorPane but it only opens local file.

What I want is to show html directly in application without showing the usual toolbar (e.g address bar). Can anyone please help? I really tried to find the answers myself with no results.
 
So you basically mean you want to write your own browser ?

Download the html from the URL using this :

Code:
import java.io.*;
import java.net.*;


public class Download {

    public void download(String u, String dest) throws IOException {
            URL url = new URL(u);
            HttpURLConnection huc = (HttpURLConnection)url.openConnection();
            huc.setRequestMethod("GET");
            huc.setDoInput(true);
            huc.setUseCaches(false);
            huc.setFollowRedirects(true);
            System.err.println("[Download] from : " +huc.getURL() +", to : " +dest);

            int cl = huc.getContentLength();
            DataInputStream dis = new DataInputStream(huc.getInputStream());
            FileOutputStream fos = new FileOutputStream(dest);

            // content-length header not set - read the stream a byte at a time
            if (cl == -1) {
                boolean bb = true;
                try {
                    while(bb) {
                        fos.write(dis.readByte());
                    }
                } catch (EOFException eof) {}
            // cool, content-length is set, so read the stream in one chunk
            } else {
                byte[] buf = new byte[cl];
                dis.readFully(buf);
                fos.write(buf);
            }

            fos.flush();
            fos.close();
            dis.close();

            huc.disconnect();
    }

     public static void main(String args[]) throws IOException {
         new Download().download("[URL unfurl="true"]http://www.google.com",[/URL] "http_example.html");
     }

}

And then display the downloaded html in a JTextPane or JEditorPane or whatever ....

--------------------------------------------------
Free Database Connection Pooling Software
 
// java HtmlPanel to run the application
// flash and css not supported
import java.io.*;
import java.awt.*;
import java.net.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
public class HtmlPanel extends JPanel implements HyperlinkListener {
protected JEditorPane html;

public HtmlPanel(String startURL) {
setLayout(new BorderLayout());

try {
URL url = new URL(startURL);
html = new JEditorPane(url);
html.setEditable(false);
html.addHyperlinkListener(this);
JScrollPane scroller = new JScrollPane();
JViewport vp = scroller.getViewport();
vp.add(html);
vp.setBackingStoreEnabled(true);
add(scroller, BorderLayout.CENTER);

} catch (MalformedURLException e) {
System.out.println("Malformed URL: " + e);
} catch (IOException e) {
System.out.println("IOException: " + e);
}
}

public void hyperlinkUpdate(HyperlinkEvent e) {
if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
System.out.println("myMess"+e.getURL());
linkActivated(e.getURL());
}
}

protected void linkActivated(URL u) {
Cursor c = html.getCursor();
Cursor waitCursor = Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR);
html.setCursor(waitCursor);
SwingUtilities.invokeLater(new PageLoader(u, c));
}

class PageLoader implements Runnable {
URL url;
Cursor cursor;

PageLoader(URL u, Cursor c) {
url = u;
cursor = c;
}
public void run() {
if (url == null) {
html.setCursor(cursor);
Container parent = html.getParent();
parent.repaint();
} else {
Document doc = html.getDocument();
try {
html.setPage(url);
} catch (IOException ioe) {
html.setDocument(doc);
getToolkit().beep();
} finally {
url = null;
SwingUtilities.invokeLater(this);
}
}
}
}

public static void main(String args[]) {
JFrame frame = new JFrame("Roadster");
HtmlPanel panel = new HtmlPanel(args[0]);

frame.getContentPane().add(panel);
frame.setSize(500, 300);
frame.setVisible(true);
}
}
 
Hi,

thanks sedj, prosper for your reply. I've tried out both methods and below are the results.

sedj, i tried your code with a link what needs another asp to work, so even if the program saved a local copy of a.html, it still needs b.asp to show. I'm trying to link to where the postcode value will be decided by the program when it runs.

prosper, when i compiled your codes, there was a warning of setBackingStoreEnabled is deprecated and when i use "java HtmlPanel after a while a error msg pops out "IOException: java.net.ConnectException: Connection timed out: connect". I've tried several times with the same result. There's a comment that says flash and css not supported, does this mean that if the html page needs css, it won't show the html properly?
 
... and javascript and the like are not supported as well.

Man - think of what you're expecting!
Why are browsers 20MB in size? Only for themes and animated buttons/ logos, bookmark-management?

I think there was a java-browser: jigsaw.
Isn't it opensource?

Include it in your app...

seeking a job as java-programmer in Berlin:
 
stefanwagner, by java-browser: jigsaw, do you mean the web server by ? I don't think that is what I want, it being a web server and all.

Basically, I'm trying to convert a VB program to Java (company request). That VB program uses a WebBrowser control which allows the setting of the URL and displaying the page within the VB application which is easy to do. I just need to a)know whether this can be achieve in Java, b) how to do it, c)if cannot do it, what other options do I have?

I also have to find some way of showing Flash files using Java. Any thoughts on this and the above?

Thanks.
 
Well - bad memory: Jigsaw is a webSERVER? Ok - then I don't know, whether there is a openSource Browser.

I guess VB uses lots of IE-dlls, which isn't an option for java-progs.

a) nearly everything might be done, but will it be possible to get a budget?
b) why do they want to port it to java?
c) only write a plugin for a platform-independent browser? - No. Write a portable plugin for a browser, which is working on all needed platforms?

We know nearly nothing from your project - how shall we answer c) ?
Do you know which plattforms your programm shall support?
Which kind of content needs to be supported? Javascript, Frames, css, shtml, ...


seeking a job as java-programmer in Berlin:
 
Hi,

Management decision doesn't always pass to the the 'worker ants' below. I guess the whole company has switched to Java, they wanted to convert the only VB program to Java too. It would be even more wonderful if it can be cross platform. (The more the merrier, right?) The method of distribution to public is through CDs.

Currently, in VB, platforms supported are only Win95,98,2k. They want to expand to Win XP and if possible to Mac, etc.

The program performs searches on the local database and shows the results on the screen. If the users wants to see the location of the search results, they just have to click a button which will show them the map in the application. The asp pages are written by other dept and from what i can tell, needs JavaScript and CSS, definitely no Frames, SHTML. The program also launches Flash files as an intro to the CD (I've not seen any posting regarding showing Flash in Java Applications).

Basically, it's a directory search program with the map as an added feature. I don't really understand the part about portable plugin for browsers, I'm not really good in Java.
 
I think you should look at SWT (the eclipse plugin) - should allow utilisation of IE dll functions into your Swing app. Never used it personally though. This will really only be the way to handle flash and css in a Swing app, unless you buy a plugin to do it.


--------------------------------------------------
Free Database Connection Pooling Software
 
a.) Re-write your app as JSP, load the links from the DB,
and access the whole thing from your browser.

b.) Write it in Swing, and tell that to get it done for time
and budget, you'll have to open a separate window, and do roughly:
Code:
Runtime.getRuntime()exec(new String[] {
"explorer", url.getFile()});//yuck.

c.)
Code:
Runtime.getRuntime().exec("yourvbapp.exe");
put this in main. (Just kidding).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top