Search code examples
javauser-interfaceswingjeditorpane

JEditorPane can't take Google search queries, why?


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


Solution

  • 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.