Results 1 to 7 of 7
  1. #1
    Remix919 is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default New to Java, Trying to Create a Simple Browser

    Ok, I'm entirely new to Java, but I know PHP and mySQL programming. I downloaded the Java JDK w/ Netbeans. All that I am trying to do is create a simple java browser for my BlackBerry phone. I got the following code from a java resource site for a simple web browser, but I'm not sure how to use it in Netbeans to create a java app. Could someone please help me? I'd really appreciate it!

    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 Simple 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();
        }
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default

    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    in netbeans its simple: Run > Clean & Build (or press SHIFT+F11) then go to your project folder and the executable JAR will be in the 'dist' folder

  4. #4
    Remix919 is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by KevinWorkman View Post
    Thanks, that tutorial helped me get a basic understanding of how Netbeans works and where to put code and how to build and run a basic app.

  5. #5
    Remix919 is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by ozzyman View Post
    in netbeans its simple: Run > Clean & Build (or press SHIFT+F11) then go to your project folder and the executable JAR will be in the 'dist' folder
    Thank you both for your help, I just built and ran the app and it seems to work, but talk about BASIC, when I try to load a website using the browser it doesn't seem to be able to read any code besides basic HTML, any ideas how to fix that slight problem or any links to a better java browser code?

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,528
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Remix919 View Post
    Thank you both for your help, I just built and ran the app and it seems to work, but talk about BASIC, when I try to load a website using the browser it doesn't seem to be able to read any code besides basic HTML, any ideas how to fix that slight problem or any links to a better java browser code?
    That is not a "slight problem"; you need a Javascript interpreter at least and that takes quite a bit of code to incorporate it in the html aware text panes. Lower your expectations or forget all about it.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Remix919 is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    That is not a "slight problem"; you need a Javascript interpreter at least and that takes quite a bit of code to incorporate it in the html aware text panes. Lower your expectations or forget all about it.

    kind regards,

    Jos
    Ok, my goal is to basically make an app for the BlackBerry which is based on Java. All I want is an app that displays a single webpage, no address bar or buttons. I don't want to have to learn all of Java just do make this one app, do you think you can point me in the right direction for how I could make something like that?

Similar Threads

  1. Trying to create a simple web app
    By xhunter in forum New To Java
    Replies: 1
    Last Post: 02-11-2011, 08:25 PM
  2. Replies: 3
    Last Post: 11-07-2010, 11:46 AM
  3. Create Web Browser in Java
    By rao2141 in forum Forum Guides
    Replies: 0
    Last Post: 10-15-2010, 02:48 PM
  4. Replies: 10
    Last Post: 08-14-2010, 09:16 PM
  5. how to create and install toolbar in a browser
    By barney in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 07:19 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
  •