Chat LIVE With Programming Experts! There Are 23 Online Right Now...

 

Code Snippets

  

Java Source Code


Welcome to Dream.In.Code
Become a Java Expert!

Join 244,258 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,280 people online right now. Registration is fast and FREE... Join Now!





Basic browser

simple basic browser.

Submitted By: harshakirans
Actions:
Rating:
Views: 9,465

Language: Java

Last Modified: February 25, 2007

Snippet


  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.util.*;
  4. import java.net.*;
  5. import java.io.*;
  6. import javax.swing.*;
  7. import javax.swing.event.*;
  8.  
  9. public class WebBrowser
  10. {
  11.      public static void main(String [] args)
  12.      {
  13.           JFrame frame = new EditorPaneFrame();
  14.           frame.show();
  15.      }
  16. }
  17. class EditorPaneFrame extends JFrame
  18. {
  19.  
  20.      private JTextField url;
  21.      private JCheckBox editable;
  22.      private JButton loadButton;
  23.      private JButton backButton;
  24.      private JEditorPane editorPane;
  25.      private Stack urlStack = new Stack();
  26.  
  27.  
  28.      public EditorPaneFrame()
  29.      {
  30.           setTitle("Java Web Browser");
  31.           setSize(600,400);
  32.           addWindowListener(new WindowAdapter()
  33.           {
  34.                public void windowClosing(WindowEvent e)
  35.                {
  36.                     System.exit(0);
  37.                }
  38.           } );
  39.  
  40.           // set up text field and load button for typing in URL
  41.  
  42.           url = new JTextField(30);
  43.  
  44.           loadButton = new JButton("Load");
  45.           loadButton.addActionListener(new ActionListener()
  46.           {
  47.                public void actionPerformed(ActionEvent event)
  48.                {
  49.                     try
  50.                     {
  51.                          // remember URL for back button
  52.                          urlStack.push(url.getText());
  53.                          editorPane.setPage(url.getText());
  54.                     }
  55.                     catch(Exception e)
  56.                     {
  57.                          editorPane.setText("Error: " +e);
  58.                     }
  59.                }
  60.           });
  61.  
  62.           // set up back button and button action
  63.  
  64.           backButton = new JButton("Back");
  65.           backButton.addActionListener(new ActionListener()
  66.           {
  67.                public void actionPerformed(ActionEvent event)
  68.                {
  69.                     if(urlStack.size()<=1) return;
  70.                     try
  71.                     {
  72.                          urlStack.pop();
  73.                          String urlString = (String)urlStack.peek();
  74.                          url.setText(urlString);
  75.                          editorPane.setPage(urlString);
  76.                     }
  77.                     catch(IOException e)
  78.                     {
  79.                          editorPane.setText("Error : " +e);
  80.                     }
  81.                }
  82.           });
  83.  
  84.           editorPane = new JEditorPane();
  85.           editorPane.setEditable(false);
  86.           editorPane.addHyperlinkListener(new HyperlinkListener()
  87.           {
  88.                public void hyperlinkUpdate(HyperlinkEvent event)
  89.                {
  90.                     if(event.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
  91.                     {
  92.                          try
  93.                          {
  94.                               urlStack.push(event.getURL().toString());
  95.                               url.setText(event.getURL().toString());
  96.  
  97.                               editorPane.setPage(event.getURL());
  98.                          }
  99.                          catch(IOException e)
  100.                          {
  101.                               editorPane.setText("Error: " + e);
  102.                          }
  103.                     }
  104.                }
  105.           });
  106.  
  107.           editable = new JCheckBox();
  108.           editable.addActionListener(new ActionListener()
  109.           {
  110.                public void actionPerformed(ActionEvent event)
  111.                {
  112.                     editorPane.setEditable(editable.isSelected());
  113.                }
  114.           });
  115.  
  116.           Container contentPane = getContentPane();
  117.           contentPane.add(new JScrollPane(editorPane), "Center");
  118.  
  119.           JPanel panel = new JPanel();
  120.           panel.add(new JLabel("URL"));
  121.           panel.add(url);
  122.           panel.add(loadButton);
  123.           panel.add(backButton);
  124.           panel.add(new JLabel("Editable"));
  125.           panel.add(editable);
  126.  
  127.           contentPane.add(panel,"South");
  128.      }
  129.  
  130. }
  131.  

Copy & Paste


Comments


purandar 2008-08-26 10:40:30

hey... good post.. i compiled it n it showed no error .. but.how do i run it..?

purandar 2008-09-02 09:36:38

its not connecting da...jus rep me pls...wordlife4purandar@gmail.com

harshakirans 2008-09-04 00:18:44

Dude check if you have any automatic proxy configurations settings in your connection, And the URL encoding part is not included in the code..Well i think that should be the problem

jack_lorren 2009-03-06 08:41:46

i think it not automatic,you must write "http://"

nafees88 2009-06-06 00:48:44

www.chatpk.tk


Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month