1

I am trying to create a simple Java browser using eclipse Luna 4.4.1 I manage to create the browser but I have 2 issues.

  1. The display of the pages. When my browser shows the pages it shows them broken, for example "google.com", the background is blue, messed up letters, and i even show contented that in a normal browser doesn't show
  2. The Google search engine doesn't work, even though the Yahoo engine works.

This is my code

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.util.Stack;

import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;

class EditorPaneFrame extends JFrame
{

private JTextField url;
 private JButton backButton;
 private JEditorPane editorPane;
 private Stack urlStack = new Stack();

 public EditorPaneFrame()
 {
 setTitle("Java Web Browser");
 setSize(1000,800);
 addWindowListener(new WindowAdapter()
 {
 public void windowClosing(WindowEvent e)
 {
 System.exit(0);
 }
 } );
 // set up text field and load button for typing in URL
 url = new JTextField(30);

 url.addActionListener(
            //action listener for when the user press enter
            new ActionListener(){
                public void actionPerformed(ActionEvent event)
                    {
                         try
                         {
                             // remember URL for back button
                             urlStack.push(url.getText());
                             editorPane.setPage("http://"+url.getText());
                             }
                             catch(Exception e)
                             {
                             editorPane.setText("Error: " +e);
                             }
                             }

                }

            );

         // set up back button and button action
     backButton = new JButton("Back");
     backButton.addActionListener(new ActionListener()
     {
     public void actionPerformed(ActionEvent event)
     {
     if(urlStack.size()<=1) return;
     try
     {
     urlStack.pop();
     String urlString = (String)urlStack.peek();
     url.setText(urlString);
     editorPane.setPage("http://"+urlString);
     }
     catch(IOException e)
     {
     editorPane.setText("Error : " +e);
     }
     }
     });
     editorPane = new JEditorPane();
     editorPane.setEditable(false);
     editorPane.addHyperlinkListener(new HyperlinkListener()
     {
         public void hyperlinkUpdate(HyperlinkEvent event)
         {
         if(event.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
         {
         try
         {
         urlStack.push(event.getURL().toString());
         url.setText(event.getURL().toString());
         editorPane.setPage(event.getURL());
         }
         catch(IOException e)
         {
         editorPane.setText("Error: " + e);
         }
         }
         }
         });

         Container contentPane = getContentPane();
         contentPane.add(new JScrollPane(editorPane), "Center");
         JPanel panel = new JPanel();
         panel.add(new JLabel("URL  http://"));
         panel.add(url);
         panel.add(backButton);
         contentPane.add(panel,"North");
         }
}


import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.net.*;
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;

public class WebBrowser {
@SuppressWarnings("deprecation")
public static void main(String [] args)
 {
 JFrame frame = new EditorPaneFrame();
 frame.show();
 }
}

Any help will be appreciated

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • *"I manage to create the browser but I have 2 issues."* Do you also have 1 question? Also note that SO is a Q&A Site, not a help desk. Different questions should be asked in different threads. – Andrew Thompson Dec 02 '14 at 23:31

1 Answers1

0

Last time I checked, JEditorPane was way behind the current version of HTML. Also, you've probably got to plug in a Javascript Engine somehow (When I tried this a decade ago, this was true). You can look at Apache Rhino for a Java Javascript Engine. There are also things like cascading style sheets / flash plugins that again I think you'd have to bootstrap.

Good luck!

ControlAltDel
  • 33,923
  • 10
  • 53
  • 80