Click to See Complete Forum and Search --> : open an url from an application
rupeshmg
September 27th, 1999, 10:30 AM
Dear friend,
How can I link to a given url site like hotmail or any other site from an application.
For example I should view the HTML pages from that site, or download files from that site, etc.
ie, on clicking a button, I should get a connection to that site.
Rupesh.
poochi
September 27th, 1999, 12:27 PM
> I should view the HTML pages from that site
To view a HTML page , use JEditorPane which will display a simple HTML page.
JEditorPane pane = new JEditorPane();
try{
pane.setPage( "http://www.hotmail.com" );
}catch( Exception e ){}
rupeshmg
September 29th, 1999, 01:56 AM
Dear friend,
Thanks a lot for responding. I got the Hotmail front page, when I used your code, but it did not looked similar to the actual hotmail front page. Is there any way to navigate through the links?
Rupesh.
poochi
September 29th, 1999, 08:27 PM
From SwingSet example...
import com.sun.java.swing.*;
import com.sun.java.swing.event.*;
import com.sun.java.swing.text.*;
import com.sun.java.accessibility.*;
import java.awt.*;
import java.net.URL;
import java.net.MalformedURLException;
import java.io.IOException;
/*
* @version 1.7 02/02/98
* @author Jeff Dinkins
* @author Tim Prinzing
* @author Peter Korn (accessibility support)
*/
public class HtmlPanel extends JPanel implements HyperlinkListener {
SwingSet swing;
JEditorPane html;
public HtmlPanel(SwingSet swing) {
this.swing = swing;
// setBackground(Color.white);
setBorder(swing.emptyBorder10);
setLayout(new BorderLayout());
getAccessibleContext().setAccessibleName("HTML panel");
getAccessibleContext().setAccessibleDescription("A panel for viewing HTML documents, and following their links");
try {
URL url = new URL("http://www.hotmail.com");
html = new JEditorPane(url);
html.setEditable(false);
html.addHyperlinkListener(this);
JScrollPane scroller = new JScrollPane();
scroller.setBorder(swing.loweredBorder);
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);
}
}
/**
* Notification of a change relative to a
* hyperlink.
*/
public void hyperlinkUpdate(HyperlinkEvent e) {
if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
System.out.println("Link" );
linkActivated(e.getURL());
}
}
/**
* Follows the reference in an
* link. The given url is the requested reference.
* By default this calls <a href="#setPage">setPage</a>,
* and if an exception is thrown the original previous
* document is restored and a beep sounded. If an
* attempt was made to follow a link, but it represented
* a malformed url, this method will be called with a
* null argument.
*
* @param u the URL to follow
*/
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));
}
/**
* temporary class that loads synchronously (although
* later than the request so that a cursor change
* can be done).
*/
class PageLoader implements Runnable {
PageLoader(URL u, Cursor c) {
url = u;
cursor = c;
}
public void run() {
if (url == null) {
// restore the original cursor
html.setCursor(cursor);
// PENDING(prinz) remove this hack when
// automatic validation is activated.
Container parent = html.getParent();
parent.repaint();
} else {
Document doc = html.getDocument();
try {
html.setPage(url);
} catch (IOException ioe) {
html.setDocument(doc);
getToolkit().beep();
} finally {
// schedule the cursor to revert after
// the paint has happended.
url = null;
SwingUtilities.invokeLater(this);
}
}
}
URL url;
Cursor cursor;
}
}
This code wont compile and will give some compilation error. Get the idea of how to use this JEditorPane.
This class is being used mainly to display the simple html help file , not for complicated
pages.
There are lots of commertial html renderers in the net ..
1. HotJava from Sun.
2. ICEBrowser from icesoft ( www.icesoft.com , you can download the trial version and try it.)
Poochi...
hheister
November 5th, 1999, 06:06 PM
If you want to display html pages from a Java application you could use the HTMLWindow
component i have created. It is much better at rendering html than the Jeditorpane
and it is free. You can download a copy at http://home.earthlink.net/~hheister
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.