Cookie's Shitty Webbrowser

Posts 1–10 of 10 · Page 1 of 1
Cookie's Shitty Webbrowser
I couldn't post this in the webbrowser thread.
So I'll post it here.

It's not good, it doesn't support a lot.

But here you go

Code:
import javax.swing.*;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLFrameHyperlinkEvent;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User: Nathaniel
 * Date: 8/31/11
 * Time: 5:57 PM
 * To change this template use File | Settings | File Templates.
 */

public class Browser extends JFrame implements HyperlinkListener {
    private ArrayList pageList = new ArrayList();

    private JTextField address;
    private JButton go;
    private JButton back;
    private JButton next;
    private JScrollPane scrollPane1;
    private JEditorPane body;


    public static void main(String[] args) {
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (Exception e) {
                    }
                    Browser g = new Browser();
                    g.setVisible(true);
                }
            });
        } catch (Exception ex) {
        }
    }

    public Browser() {
        address = new JTextField();
        go = new JButton();
        back = new JButton();
        next = new JButton();
        scrollPane1 = new JScrollPane();
        body = new JEditorPane();
        Container contentPane = getContentPane();
        go.setText("Go");
        back.setText("<");
        next.setText(">");
        {
            body.setContentType("text/html");
            scrollPane1.setViewportView(body);
        }

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
                contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(contentPaneLayout.createParallelGroup()
                                        .addComponent(scrollPane1, GroupLayout.DEFAULT_SIZE, 597, Short.MAX_VALUE)
                                        .addGroup(contentPaneLayout.createSequentialGroup()
                                                .addComponent(back, GroupLayout.PREFERRED_SIZE, 43, GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(next)
                                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(address, GroupLayout.DEFAULT_SIZE, 446, Short.MAX_VALUE)
                                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(go)))
                                .addContainerGap())
        );
        contentPaneLayout.setVerticalGroup(
                contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                        .addComponent(back)
                                        .addComponent(next)
                                        .addComponent(address, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(go))
                                .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(scrollPane1, GroupLayout.DEFAULT_SIZE, 357, Short.MAX_VALUE)
                                .addContainerGap())
        );
        pack();
        setLocationRelativeTo(getOwner());
        setTitle("Cookie's Java Browser");
        back.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (pageList.size() > 1) {
                    URL currentUrl = body.getPage();
                    int pageIndex = pageList.indexOf(currentUrl.toString());
                    try {
                        showPage(new URL((String) pageList.get(pageIndex - 1)), false);
                    } catch (Exception ex) {
                    }
                }
            }
        });
        next.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                URL currentUrl = body.getPage();
                int pageIndex = pageList.indexOf(currentUrl.toString());
                if (pageList.get(pageIndex + 1) != null) {
                    try {
                        showPage(new URL((String) pageList.get(pageIndex + 1)), false);
                    } catch (Exception ex) {
                    }
                }
            }
        });
        go.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                URL url = verifyUrl(address.getText());
                if (url != null) {
                    showPage(url, true);
                } else {
                    showError("Invalid URL");
                }
            }
        });
    }

    private void showPage(URL pageUrl, boolean addToList) {
        setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
        try {
            URL currentUrl = body.getPage();
            body.setPage(pageUrl);
            URL newUrl = body.getPage();
            if (addToList) {
                int listSize = pageList.size();
                if (listSize > 0) {
                    int pageIndex = pageList.indexOf(currentUrl.toString());
                    if (pageIndex < listSize - 1) {
                        for (int i = listSize - 1; i > pageIndex; i--) {
                            pageList.remove(i);
                        }
                    }
                }
                pageList.add(newUrl.toString());
            }
            address.setText(newUrl.toString());
        } catch (Exception e) {
        } finally {
            setCursor(Cursor.getDefaultCursor());
        }
    }

    public void hyperlinkUpdate(HyperlinkEvent event) {
        HyperlinkEvent.EventType eventType = event.getEventType();
        if (eventType == HyperlinkEvent.EventType.ACTIVATED) {
            if (event instanceof HTMLFrameHyperlinkEvent) {
                HTMLFrameHyperlinkEvent linkEvent = (HTMLFrameHyperlinkEvent) event;
                HTMLDocument document = (HTMLDocument) body.getDocument();
                document.processHTMLFrameHyperlinkEvent(linkEvent);
            } else {
                showPage(event.getURL(), true);
            }
        }
    }

    private void showError(String errorMessage) {
        JOptionPane.showMessageDialog(this, errorMessage, "Error", JOptionPane.ERROR_MESSAGE);
    }

    private URL verifyUrl(String url) {
        if (!url.toLowerCase().startsWith("http://"))
            return null;
        URL verifiedUrl = null;
        try {
            verifiedUrl = new URL(url.toLowerCase());
        } catch (Exception e) {
            return null;
        }
        return verifiedUrl;
    }
}
Screenshot pl0x ? I don't haz java but I'd like to see what it looks like.
Quote Originally Posted by Nathan View Post
Wow, nice bro!
java musnt be very good for browser development....
this can be done in visual basic in 10 minutes.

no offence.
Quote Originally Posted by Velocity View Post
java musnt be very good for browser development....
this can be done in visual basic in 10 minutes.

no offence.
It's a high level language tard. Java is lower and you have to write the GUI yourself (which accounts for most of his code). VB.NET = drag and drop.
@Hassan, you wanted a screenshot, so now you're gonna look at it and say what you think





This is so awesome.
That browser's fucking beast!
Quote Originally Posted by Lehsyrus View Post
That browser's fucking beast!
I know right
Posts 1–10 of 10 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us