Results 1 to 3 of 3
  1. #1
    MarkWilson is offline Member
    Join Date
    Jun 2008
    Location
    us
    Posts
    47
    Rep Power
    0

    Default java Web Browser

    hi friends,
    can u help me in, Designing a new web browser
    i have some sample code to desing a new browser

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.net.*;
    import java.util.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.text.html.*;
    
    // The Mini Web Browser.
    public class MiniBrowser extends JFrame
    implements HyperlinkListener
    {
    // These are the buttons for iterating through the page list.
    private JButton backButton, forwardButton;
    
    // Page location text field.
    private JTextField locationTextField;
    
    // Editor pane for displaying pages.
    private JEditorPane displayEditorPane;
    
    // Browser's list of pages that have been visited.
    private ArrayList pageList = new ArrayList();
    
    // Constructor for Mini Web Browser.
    public MiniBrowser()
    {
    // Set application title.
    super("Mini Browser");
    
    // Set window size.
    setSize(640, 480);
    
    // Handle closing events.
    addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
    actionExit();
    }
    });
    
    // Set up file menu.
    JMenuBar menuBar = new JMenuBar();
    JMenu fileMenu = new JMenu("File");
    fileMenu.setMnemonic(KeyEvent.VK_F);
    JMenuItem fileExitMenuItem = new JMenuItem("Exit",
    KeyEvent.VK_X);
    fileExitMenuItem.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    actionExit();
    }
    });
    fileMenu.add(fileExitMenuItem);
    menuBar.add(fileMenu);
    setJMenuBar(menuBar);
    
    // Set up button panel.
    JPanel buttonPanel = new JPanel();
    backButton = new JButton("< Back");
    backButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    actionBack();
    }
    });
    backButton.setEnabled(false);
    buttonPanel.add(backButton);
    forwardButton = new JButton("Forward >");
    forwardButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    actionForward();
    }
    });
    forwardButton.setEnabled(false);
    buttonPanel.add(forwardButton);
    locationTextField = new JTextField(35);
    locationTextField.addKeyListener(new KeyAdapter() {
    public void keyReleased(KeyEvent e) {
    if (e.getKeyCode() == KeyEvent.VK_ENTER) {
    actionGo();
    }
    }
    });
    buttonPanel.add(locationTextField);
    JButton goButton = new JButton("GO");
    goButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    actionGo();
    }
    });
    buttonPanel.add(goButton);
    
    // Set up page display.
    displayEditorPane = new JEditorPane();
    displayEditorPane.setContentType("text/html");
    displayEditorPane.setEditable(false);
    displayEditorPane.addHyperlinkListener(this);
    
    getContentPane().setLayout(new BorderLayout());
    getContentPane().add(buttonPanel, BorderLayout.NORTH);
    getContentPane().add(new JScrollPane(displayEditorPane),
    BorderLayout.CENTER);
    }
    
    // Exit this program.
    private void actionExit() {
    System.exit(0);
    }
    
    // Go back to the page viewed before the current page.
    private void actionBack() {
    URL currentUrl = displayEditorPane.getPage();
    int pageIndex = pageList.indexOf(currentUrl.toString());
    try {
    showPage(
    new URL((String) pageList.get(pageIndex - 1)), false);
    }
    catch (Exception e) {}
    }
    
    // Go forward to the page viewed after the current page.
    private void actionForward() {
    URL currentUrl = displayEditorPane.getPage();
    int pageIndex = pageList.indexOf(currentUrl.toString());
    try {
    showPage(
    new URL((String) pageList.get(pageIndex + 1)), false);
    }
    catch (Exception e) {}
    }
    
    // Load and show the page specified in the location text field.
    private void actionGo() {
    URL verifiedUrl = verifyUrl(locationTextField.getText());
    if (verifiedUrl != null) {
    showPage(verifiedUrl, true);
    } else {
    showError("Invalid URL");
    }
    }
    
    // Show dialog box with error message.
    private void showError(String errorMessage) {
    JOptionPane.showMessageDialog(this, errorMessage,
    "Error", JOptionPane.ERROR_MESSAGE);
    }
    
    // Verify URL format.
    private URL verifyUrl(String url) {
    // Only allow HTTP URLs.
    if (!url.toLowerCase().startsWith("http://"))
    return null;
    
    // Verify format of URL.
    URL verifiedUrl = null;
    try {
    verifiedUrl = new URL(url);
    } catch (Exception e) {
    return null;
    }
    
    return verifiedUrl;
    }
    
    /* Show the specified page and add it to
    the page list if specified. */
    private void showPage(URL pageUrl, boolean addToList)
    {
    // Show hour glass cursor while crawling is under way.
    setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
    
    try {
    // Get URL of page currently being displayed.
    URL currentUrl = displayEditorPane.getPage();
    
    // Load and display specified page.
    displayEditorPane.setPage(pageUrl);
    
    // Get URL of new page being displayed.
    URL newUrl = displayEditorPane.getPage();
    
    // Add page to list if specified.
    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());
    }
    
    // Update location text field with URL of current page.
    locationTextField.setText(newUrl.toString());
    
    // Update buttons based on the page being displayed.
    updateButtons();
    }
    catch (Exception e)
    {
    // Show error messsage.
    showError("Unable to load page");
    }
    finally
    {
    // Return to default cursor.
    setCursor(Cursor.getDefaultCursor());
    }
    }
    
    /* Update back and forward buttons based on
    the page being displayed. */
    private void updateButtons() {
    if (pageList.size() < 2) {
    backButton.setEnabled(false);
    forwardButton.setEnabled(false);
    } else {
    URL currentUrl = displayEditorPane.getPage();
    int pageIndex = pageList.indexOf(currentUrl.toString());
    backButton.setEnabled(pageIndex > 0);
    forwardButton.setEnabled(
    pageIndex < (pageList.size() - 1));
    }
    }
    
    // Handle hyperlink's being clicked.
    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) displayEditorPane.getDocument();
    document.processHTMLFrameHyperlinkEvent(linkEvent);
    } else {
    showPage(event.getURL(), true);
    }
    }
    }
    
    // Run the Mini Browser.
    public static void main(String[] args) {
    MiniBrowser browser = new MiniBrowser();
    browser.show();
    }
    }
    but its not sufficient , it only show's URL bar and Go button
    i want some advance level
    like
    1. Next ,Prev ,Stop , Refresh Buttons
    2. Status Bar
    and some good Grapics look
    hope u will help me ,
    Thank you,

  2. #2
    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

    Default Re: java Web Browser

    I am making a web browser too. Use netbeans for the GUI and the refresh button would be along the lines of this (considering you put an action listener and the actionPerformed lines before it.) (Also put this code at the beginning of your code: private Stack urlStack = new Stack(); ) Then at the actionPerformed part of the reload button:
    try{
    urlStack.push(locationTextField.getText());
    displayEditorPane.setPage(locationTextField.getTex t());
    }
    catch(Exception e){
    JOptionPane error;
    error = new JOptionPane("Oh darn, we couldn't reload your page! Try again!", JOptionPane.ERROR_MESSAGE);
    JDialog error2 = error.createDialog("Oh no!");
    error2.setVisible(true);
    }

    What this does is it takes the URL from the textfield (the URL box) and it puts it in again, thus reloading it. If it encounters an error, it will have a dialog box pop up and tell the

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,404
    Rep Power
    20

    Default Re: java Web Browser

    Quote Originally Posted by cannibalturtle87 View Post
    (Also put this code at the beginning of your code: private Stack urlStack = new Stack(); )
    Did you really think that MarkWilson is still waiting for a solution, 4 years on?

    Please don't post to old dead threads.

    db

    THREAD CLOSED
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. How to print a HTML file in browser look using JAVA
    By ammameiya in forum Advanced Java
    Replies: 5
    Last Post: 06-25-2010, 02:36 AM
  2. Browser
    By Jessie Madman in forum AWT / Swing
    Replies: 20
    Last Post: 02-17-2009, 01:56 PM
  3. Need Help! swt/Browser
    By sealyu in forum SWT / JFace
    Replies: 4
    Last Post: 07-09-2008, 08:16 PM
  4. java networking via browser
    By pratik.ac in forum Networking
    Replies: 2
    Last Post: 04-21-2008, 05:25 PM
  5. how to get url from browser
    By srinivas reddy in forum New To Java
    Replies: 0
    Last Post: 02-08-2008, 07:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •