├── README.md ├── SimpleMiniWebBrowser.java ├── fech.md └── fecha.txt /README.md: -------------------------------------------------------------------------------- 1 | A Simple Web Browser Written in Java 2 | ==================================== 3 | I wanted to code up a simple web browser perhaps to better understand how a browser 4 | makes http requests and renders pages to the browser. Naturally, Most would say C++ 5 | would be the best choice for creating a browser; however, since I was more familiar 6 | with Java at the time, I decided to start with Java. 7 | I will add more comments and features down 8 | the road. -------------------------------------------------------------------------------- /SimpleMiniWebBrowser.java: -------------------------------------------------------------------------------- 1 | 2 | 3 | /* Swing Imports*/ 4 | import javax.swing.JButton; 5 | import javax.swing.JEditorPane; 6 | import javax.swing.JFrame; 7 | import javax.swing.JMenu; 8 | import javax.swing.JMenuBar; 9 | import javax.swing.JMenuItem; 10 | import javax.swing.JOptionPane; 11 | import javax.swing.JPanel; 12 | import javax.swing.JScrollPane; 13 | import javax.swing.JTextField; 14 | import javax.swing.event.HyperlinkEvent; 15 | import javax.swing.event.HyperlinkListener; 16 | import javax.swing.text.html.HTMLDocument; 17 | import javax.swing.text.html.HTMLFrameHyperlinkEvent; 18 | 19 | /* Abstract Window ToolKit imports */ 20 | import java.awt.BorderLayout; 21 | import java.awt.Cursor; 22 | import java.awt.event.ActionEvent; 23 | import java.awt.event.ActionListener; 24 | import java.awt.event.KeyAdapter; 25 | import java.awt.event.KeyEvent; 26 | import java.awt.event.WindowAdapter; 27 | import java.awt.event.WindowEvent; 28 | import java.net.URL; 29 | import java.util.ArrayList; 30 | 31 | 32 | 33 | public class SimpleMiniWebBrowser extends JFrame implements HyperlinkListener { 34 | // Create instances for... 35 | private JButton buttonBack = new JButton("<"), buttonForward = new JButton(">"); //...the Back button 36 | 37 | private JTextField locationTextField = new JTextField(35); //...the text field (35 chars) 38 | 39 | private JEditorPane displayEditorPane = new JEditorPane(); //...the display panel 40 | 41 | private ArrayList pageList = new ArrayList(); //...and an ArrayList for pageList 42 | 43 | 44 | 45 | public SimpleMiniWebBrowser() { 46 | setSize(640, 480); // Window size to 640px by 480px 47 | setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // What to do on close (default) 48 | JPanel bttnPanel = new JPanel(); // Create a JPanel instance with refVar bttnPanel 49 | 50 | // Back btn's ActionListener 51 | buttonBack.addActionListener(new ActionListener() { 52 | public void actionPerformed(ActionEvent e) { 53 | backActn(); 54 | } 55 | }); 56 | 57 | // Forward btn's ActionListener 58 | buttonBack.setEnabled(false); 59 | bttnPanel.add(buttonBack); 60 | buttonForward.addActionListener(new ActionListener() { 61 | public void actionPerformed(ActionEvent e) { 62 | forwardActn(); 63 | } 64 | }); 65 | 66 | // Setup The text Field 67 | buttonForward.setEnabled(false); 68 | bttnPanel.add(buttonForward); 69 | locationTextField.addKeyListener(new KeyAdapter() { 70 | public void keyReleased(KeyEvent e) { 71 | if (e.getKeyCode() == KeyEvent.VK_ENTER) { 72 | actionGo(); 73 | } 74 | } 75 | }); 76 | 77 | // bttnGo 78 | bttnPanel.add(locationTextField); 79 | JButton bttnGo = new JButton("GO"); 80 | bttnGo.addActionListener(new ActionListener() { 81 | public void actionPerformed(ActionEvent e) { 82 | actionGo(); 83 | } 84 | }); 85 | 86 | // Set Display Panel 87 | bttnPanel.add(bttnGo); 88 | displayEditorPane.setContentType("text/html"); 89 | displayEditorPane.setEditable(false); 90 | displayEditorPane.addHyperlinkListener(this); 91 | 92 | getContentPane().setLayout(new BorderLayout()); 93 | getContentPane().add(bttnPanel, BorderLayout.NORTH); 94 | getContentPane().add(new JScrollPane(displayEditorPane), BorderLayout.CENTER); 95 | }// Close SimpleMiniWebBrowser() 96 | 97 | 98 | 99 | //Navigate back action 100 | private void backActn() { 101 | URL currentUrl = displayEditorPane.getPage(); 102 | int pageIndex = pageList.indexOf(currentUrl.toString()); 103 | try { 104 | showPage(new URL((String) pageList.get(pageIndex - 1)), false); 105 | } catch (Exception e) { 106 | } 107 | } 108 | 109 | // Navigate forward action 110 | private void forwardActn() { 111 | URL currentUrl = displayEditorPane.getPage(); 112 | int pageIndex = pageList.indexOf(currentUrl.toString()); 113 | try { 114 | showPage(new URL((String) pageList.get(pageIndex + 1)), false); 115 | } catch (Exception e) { 116 | } 117 | } 118 | 119 | // Go action 120 | private void actionGo() { 121 | URL verifiedUrl = verifyUrl(locationTextField.getText()); 122 | if (verifiedUrl != null) { 123 | showPage(verifiedUrl, true); 124 | } else { 125 | System.out.println("Invalid URL"); 126 | } 127 | } 128 | 129 | 130 | // Verify the URL 131 | private URL verifyUrl(String url) { 132 | if (!url.toLowerCase().startsWith("http://")) 133 | return null; 134 | 135 | URL verifiedUrl = null; 136 | try { 137 | verifiedUrl = new URL(url); 138 | } catch (Exception e) { 139 | return null; 140 | } 141 | 142 | return verifiedUrl; 143 | } 144 | 145 | // Display the page 146 | private void showPage(URL pageUrl, boolean addToList) { 147 | try { 148 | URL currentUrl = displayEditorPane.getPage(); 149 | displayEditorPane.setPage(pageUrl); 150 | URL newUrl = displayEditorPane.getPage(); 151 | if (addToList) { 152 | int listSize = pageList.size(); 153 | if (listSize <= 0) { 154 | return; 155 | } 156 | int pageIndex = pageList.indexOf(currentUrl.toString()); 157 | if (pageIndex >= listSize - 1) { 158 | return; 159 | } 160 | for (int i = listSize - 1; i > pageIndex; i--) { 161 | pageList.remove(i); 162 | } 163 | pageList.add(newUrl.toString()); 164 | } 165 | locationTextField.setText(newUrl.toString()); 166 | updateBttns(); 167 | } catch (Exception e) { 168 | System.out.println("Unable to load page"); 169 | } 170 | } // Close showPage() 171 | 172 | 173 | 174 | 175 | // Update the Buttons 176 | private void updateBttns() { 177 | if (pageList.size() < 2) { 178 | buttonBack.setEnabled(false); 179 | buttonForward.setEnabled(false); 180 | } else { 181 | URL currentUrl = displayEditorPane.getPage(); 182 | int pageIndex = pageList.indexOf(currentUrl.toString()); 183 | buttonBack.setEnabled(pageIndex > 0); 184 | buttonForward.setEnabled(pageIndex < (pageList.size() - 1)); 185 | } 186 | } 187 | 188 | 189 | // Update the links 190 | public void hyperlinkUpdate(HyperlinkEvent event) { 191 | HyperlinkEvent.EventType eventType = event.getEventType(); 192 | if (eventType == HyperlinkEvent.EventType.ACTIVATED) { 193 | if (event instanceof HTMLFrameHyperlinkEvent) { 194 | HTMLFrameHyperlinkEvent linkEvent = (HTMLFrameHyperlinkEvent) event; 195 | HTMLDocument document = (HTMLDocument) displayEditorPane.getDocument(); 196 | document.processHTMLFrameHyperlinkEvent(linkEvent); 197 | } else { 198 | showPage(event.getURL(), true); 199 | } 200 | } 201 | } 202 | 203 | 204 | 205 | /* MAIN METHOD*/ 206 | public static void main(String[] args) { 207 | SimpleMiniWebBrowser browser = new SimpleMiniWebBrowser(); 208 | browser.setVisible(true); 209 | }//main() 210 | 211 | }// Close Class--SimpleMiniWebBrowser 212 | -------------------------------------------------------------------------------- /fech.md: -------------------------------------------------------------------------------- 1 | * -- Tue Feb 5 12:59:58 MST 2019 -------------------------------------------------------------------------------- /fecha.txt: -------------------------------------------------------------------------------- 1 | * today's date is Tue Feb 5 12:52:20 MST 2019 2 | --------------------------------------------------------------------------------