• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to make a JavaBrowser  RSS feed

 
Ranch Hand
Posts: 473
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Can anyone help me with making application that can display
Html pages just like a browser does.
I went through API and came across class HTMDocument in
javax.swing.text.html but I could not make any use of it.
If I use AppletContext.getAppletContext(); in an applet then
what is the use of using it when one can easily view html
pages in the browser one is using to view that applet.
Please be elaborate in your replies.
Thanx in advance
 
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
you should have a look to JEditorPane. This can display HTML, but only HTML. You have to set a content type for it. It doesn't support JavaScript or CSS.
There are more information about it in the spcification.
Bye
Rene
 
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
heres a simple example
i remember finding this source when i went looking for help posting , ended up downloading HTTPClient.jar tho, works great for what i needed to do, but this should do for what you want, message me or search for that jar if you need more

/*
* TestBrowser.java
* A test bed for the JEditorPane and a custom editor kit.
* This extremely simple browser has a text field for typing in
* new urls, a JEditorPane to display the HTML page, and a status
* bar to display the contents of hyperlinks the mouse passes over.
*/
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
import java.awt.event.*;
import java.awt.*;
public class TestBrowser extends JFrame
{
public TestBrowser(String startingUrl)
{
// Ok, first just get a screen up and visible, with an appropriate
// handler in place for the kill window command
super("Test Pane");
setSize(400,300);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
we.getWindow().setVisible(false);
System.exit(0);
}
});
// Now set up our basic screen components, the editor pane, the
// text field for URLs, and the label for status and link information
JPanel urlPanel = new JPanel();
urlPanel.setLayout(new BorderLayout());
JTextField urlField = new JTextField(startingUrl);
urlPanel.add(new JLabel("Site: "), BorderLayout.WEST);
urlPanel.add(urlField, BorderLayout.CENTER);
final JLabel statusBar = new JLabel(" ");
// urlPanel.add(statusBar, BorderLayout.NORTH)
// Here's the editor pane configuration. It's important to make
// the "setEditable(false)" call, otherwise our hyperlinks won't
// work. (If the text is editable, then clicking on a hyperlink
// simply means that you want to change the text...not follow the
// link.)
final JEditorPane jep = new JEditorPane();
jep.setEditable(false);
// Here's where we force the pane to use our new editor kit
jep.setEditorKitForContentType("image/jpeg", new HTMLEditorKit());
jep.setContentType("image/jpeg");
try
{
jep.setPage(startingUrl);
}
catch(Exception e)
{
statusBar.setText("Could not open starting page. Using a blank.");
}
JScrollPane jsp = new JScrollPane(jep);
// and get the GUI components onto our content pane
getContentPane().add(jsp, BorderLayout.CENTER);
getContentPane().add(urlPanel, BorderLayout.NORTH);
getContentPane().add(statusBar, BorderLayout.SOUTH);
// and last but not least, hook up our event handlers
urlField.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
try
{
jep.setPage(ae.getActionCommand());
}
catch(Exception e)
{
statusBar.setText("Could not open starting page. Using a blank.");
}
}
});
// jep.addHyperlinkListener(new SimpleLinkListener(jep, urlField, statusBar));
}
public static void main(String args[])
{
(new TestBrowser(args.length > 0 ? args[0] : "http://www.google.com")).setVisible(true);
}
}
 
Maki Jav
Ranch Hand
Posts: 473
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Thanks to you folks. Taariq can you tell me why would I need HTTPClient.jar? Sorry to bother you but I think I am missing something you know so why not share ? May be I need more?!
[ June 13, 2002: Message edited by: Maki Jav ]
[ June 13, 2002: Message edited by: Maki Jav ]
 
Ta Ri Ki Sun
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no bother at all
i used it for simulating browser behaviour, i needed to log on to a site and post user details to initiate voice verification and get some feedback once its done, but its lots more useful than that.
closer to home, from what you say you want, heres a quote from the jakarta site

Designed for extension while providing robust support for the base HTTP protocol, the HTTP Client component may be of interest to anyone building HTTP-aware client applications such as web browsers , web service clients, or systems that leverage or extend the HTTP protocol for distributed communication.



HTTPClient - Jakarta Project
 
Maki Jav
Ranch Hand
Posts: 473
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Taariq for your help. Can you clear me one thing...
Why is the buttons hyperlinks(that get displayed on the webpages) not working in the application the code of which you have posted above?
well thanx again
 
Ta Ri Ki Sun
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i get a StringIndexOutOfBoundsException: String index out of range: 0 and i have to run so i cant see why right now, but heres my take on it from looking at that code
delete the comments so you add the listener
jep.addHyperlinkListener(new SimpleLinkListener(jep, urlField, statusBar));

heres the code for SimpleLinkListener

/*
* SimpleLinkListener.java
* A hyperlink listener for use with JEditorPane. This
* listener will change the cursor over hotspots based on enter/exit
* events and also load a new page when a valid hyperlink is clicked.
*/
package effortestimator.dialogs;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
public class SimpleLinkListener implements HyperlinkListener {
private JEditorPane pane; // The pane we're using to display html
private JTextField urlField; // An optional textfield for showing
// the current URL being displayed
private JLabel statusBar; // An option label for showing where
// a link would take you
public SimpleLinkListener(JEditorPane jep, JTextField jtf, JLabel jl)
{
pane = jep;
urlField = jtf;
statusBar = jl;
}
public SimpleLinkListener(JEditorPane jep) {
this(jep, null, null);
}
public void hyperlinkUpdate(HyperlinkEvent he) {
HyperlinkEvent.EventType type = he.getEventType();
// Ok. Decide which event we got...
if (type == HyperlinkEvent.EventType.ENTERED) {
// Enter event. Go the the "hand" cursor and fill in the status bar
pane.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
if(statusBar!= null)
statusBar.setText(he.getURL().toString());
}
else if (type == HyperlinkEvent.EventType.EXITED) {
// Exit event. Go back to the default cursor and clear the status bar
pane.setCursor(Cursor.getDefaultCursor());
if(statusBar!= null)
statusBar.setText(" ");
}
else {
// Jump event. Get the url, and if it's not null, switch to that
// page in the main editor pane and update the "site url" label.
try {
pane.setPage(he.getURL());
if (urlField != null) {
urlField.setText(he.getURL().toString());
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
}

remember to change the package , lemme know if it works or not
 
Maki Jav
Ranch Hand
Posts: 473
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
The browser is work fine, it is not giving any error. Could it be that your need some over hauling (he he)
I say the browser is not up to the mark! It does not show some sites and also it display other tags
sometimes. Don't you agree?
Could it be that we make a better browser???
 
Ta Ri Ki Sun
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it did indeed, and yes its not "up to the mark", but then its not really a browser.
download mozilla, hehe
but if you absolutely must have your own java browser then start at the HTTPClient.jar
 
Maki Jav
Ranch Hand
Posts: 473
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
I have downloaded the commons-httpClient.jar and commons-httpclient-2.0-alpha1.zip files from the same website that you mentioned. Is it different from the HTTPClient.jar ?
And maybe some day my lucky stars will shine and tell me how to do it(to make java browser)!!!
Taariq thanks for your consistent help
And of course I can always download the mozilla stuff or better still I can use the IE ? right!?
[ June 21, 2002: Message edited by: Maki Jav ]
 
Ta Ri Ki Sun
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey no prob at all
about the jar difference, the one i downloaded is 716KB, the one at jakarta is 81KB, not sure what the difference is tho, the site i got mine from is down alot lately, haven't seen it up in months.
maybe its just that in mine all docs and source files are included, dunno whats in jakarta's jar, maybe just the .class files
if you want i'll mail my jar to you, cos a link to that site will be useless
 
Ta Ri Ki Sun
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and i'm not convinced IE is better than mozilla, but then i also dont want to start a browser war, especially since i use a few browsers, mozilla, IE, netscape and opera, dont ask, hehe
 
Maki Jav
Ranch Hand
Posts: 473
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well that's a very few names that you put out there (hehe)I just use one browser!
Nobody going to start war as the companies are unaware of our conversation (I can leak it to them!!)
I need that HTTPClient.jar so please mail me
Thanx in advance!
my email is adilsec@yahoo.com
Thanks again
 
Ta Ri Ki Sun
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you're welcome
and i'll send the jar right away
 
Maki Jav
Ranch Hand
Posts: 473
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Taariq,
Gee Thanx I got it. I will get to you as soon as I have something!?
Good news We have crossed the 13 replies barrier
Congrates
Bye
 
Ta Ri Ki Sun
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
theres a 13 reply barrier ?
hehe, good stuff
and yes please keep me posted, and if you run into a problem i'll try help out
 
Maki Jav
Ranch Hand
Posts: 473
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi again
I has been ill so I am sorry . The other thing that kept me back was my assignment.
I have seen the HotJava Browser and it is not up to the mark of IE or Netscape display-wise, putting an effort into making our own will not give us that much satisfaction...right?! what you say?
I think the simple browser that you gave me is more than enough. ..plz advice !
 
There's a way to do it better - find it. -Edison. A better tiny ad:
Why should you try IntelliJ IDEA ?
https://coderanch.com/wiki/696337/IntelliJ-IDEA
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!