views:

244

answers:

4

Hi,

I am creating a very basic web browser using JEditorPane just to teach myself Swing and GUIs in Java but am having trouble implementing a Firefox-like Google Search bar.

I'm not if it's due to a limitation of JEditorPane or my lack of understanding but if I try and take the string typed into the "Google Search" bar and use the setPage() method of JEditorPane, it doesn't work.

Here is my code for the ActionListener of the "Google Search" button:

public void actionPerformed(ActionEvent arg0) 
{
try
{
 content.setPage("http://www.google.com/search?q=" + searchBar.getText());
}
catch (IOException e) 
{
 JOptionPane.showMessageDialog(frame, "Error searching for: " + searchBar.getText());
}
}

Even when I try and just do content.setPage("http://www.google.com/search?p=test"); it doesnt work, so is it something to do with setPage()'s way of taking the URL string? As in it doesn't like the "?" or "=" characters or is there another way of doing it all together?

Thanks for your time,

InfinitiFizz

A: 

JEditorPane is a poor choice to implement even the simplest browser. It works to display simple HTML pages but it stops there.

Try The Flying Saucer Project, it works pretty well(it's not a full browser, but close enough).They have an example which simulates actually a web browser.

adrian.tarau
Please look at the comment I gave to jonescb's answer.
Infiniti Fizz
A: 

Like adrian.tarau said, JEditorPane is very poor at displaying modern web pages.
It doesn't even support HTML 4 or Javascript. I believe Google uses Javascript to make the Search button work.

Another suggestion would be to use the Lobo Browser/Cobra engine.

jonescb
I am having to use JEditorPane because I'm not making a full-blown web browser, this is just to teach me Swing and GUIs. So no I can't use other Java Browser engines.Also, as I show above in my code, I am not trying to implement a listener for the "Google Search" button, I have created a JTextField "search box" and a JButton "Search" button, and I'm just wondering if it doesn't like the ? or = in the String used for Google searching (because if I remove the ? and = it tries to go to "http://www.google.com/searchq" just fine).Thankyou for your answer but it doesn't answer my question.
Infiniti Fizz
A: 

If you need a full browser in Java check out Lobo: http://lobobrowser.org/java-browser.jsp

voidlogic
Please look at the comment I gave to jonescb's answer.
Infiniti Fizz
A: 

Add something to print the exception you are catching and you'll see that you're receiving a 403 Forbidden from Google.

There are a lot of Java bots out there and sites have started blocking requests with "java" in the User-agent field. Google will let you get their home page, but won't let you search unless you override the User-agent field.

Start your jvm with -Dhttp.agent=myappname/1.0 where myappname is the name of your application.

Devon_C_Miller