views:

44

answers:

1

is this possible? i have made a simple web browser, it can view some pages fine, but most are messed up, i believe it can be because of this (javascript not enabled). For example this is how CNN.com shows up: http://www.glowfoto.com/static_image/07-181402L/5111/png/07/2010/img5/glowfoto

here is my code so far:

public class Browser extends JPanel {

private JEditorPane mainArea;
private JTextField adress;
private JPanel justtoseparatethings;

public Browser() {

    setLayout(new BorderLayout());
    setVisible(true);

    mainArea = new JEditorPane();
    adress = new JTextField();
    justtoseparatethings = new JPanel();

    adress.setEditable(true);
    adress.setSize(0, 0);
    mainArea.setEditable(false);
    mainArea.setContentType("text/html");



    justtoseparatethings.setLayout(new BorderLayout());

    justtoseparatethings.add(adress);
    add(justtoseparatethings, BorderLayout.NORTH);
    //mejor separar estos por paneles tb
    //add(mainArea, BorderLayout.CENTER);
    add(new JScrollPane(mainArea),BorderLayout.CENTER);

    adress.addKeyListener(new KeyListener() {

        public void keyReleased(KeyEvent ke) {
        }

        public void keyPressed(KeyEvent ke) {
            if (adress.hasFocus() && ke.getKeyCode() == KeyEvent.VK_ENTER) {
                try {
                    String unproc = adress.getText();
                    String start = "http://";

                    int index1 = unproc.indexOf(start);

                    if (index1 != -1) {
                        mainArea.setPage(adress.getText());
                    } else {
                        unproc = start + unproc;
                    }
                    adress.setText(unproc);
                    mainArea.setPage(unproc);
                } catch (Exception a) {
                    System.out.println("there was an error in your request");
                }
            }
        }

        public void keyTyped(KeyEvent ke) {
        }
    });
    mainArea.addHyperlinkListener(new HyperlinkListener() {

        public void hyperlinkUpdate(HyperlinkEvent hle) {
            if (hle.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
                try {
                    mainArea.setPage(hle.getURL());
                    adress.setText(mainArea.getPage().toString());
                } catch (Exception ex) {
                    mainArea.setText("Error ocurred" + ex.getMessage());
                }
            }
        }
    });

}

}

thanks in advance

+2  A: 

JEditorPane is not a basis for a modern web browser. It has support for rendering most HTML 3.2, and that's about it. There is no JavaScript support. You should look at the existing questions on Swing browser components, such as Java based Swing Browser should support JavaScript and Best Java/Swing browser component.

Matthew Flaschen