Welcome, Guest. Please login or register.
Did you miss your activation email?
Pages: [1]   Go Down
  Print  
Author Topic: Java WebBrowser  (Read 1847 times)
0 Members and 1 Guest are viewing this topic.
_ikram
Member
*

Reputation: 3
Offline Offline
Posts: 65
Referrals: 0

Awards
« on: July 31, 2009, 08:49:17 AM »

alright make a new class called "MiniBrowser.java"

Inside that put all this:
Code
GeSHi (java):
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.net.*;
  4. import java.util.*;
  5. import javax.swing.*;
  6. import javax.swing.event.*;
  7. import javax.swing.text.html.*;
  8.  
  9. // The Mini Web Browser.
  10. public class MiniBrowser extends JFrame
  11. {
  12. // These are the buttons for iterating through the page list.
  13. private JButton backButton, forwardButton;
  14.  
  15. // Page location text field.
  16. private JTextField locationTextField;
  17.  
  18. // Editor pane for displaying pages.
  19. private JEditorPane displayEditorPane;
  20.  
  21. // Browser's list of pages that have been visited.
  22. private ArrayList pageList = new ArrayList();
  23.  
  24. // Constructor for Mini Web Browser.
  25. public MiniBrowser()
  26. {
  27. // Set application title.
  28. super("WebBorwser - created by Ikram");
  29.  
  30. // Set window size.
  31. setSize(640, 480);
  32.  
  33. // Handle closing events.
  34. addWindowListener(new WindowAdapter() {
  35. public void windowClosing(WindowEvent e) {
  36. actionExit();
  37. }
  38. });
  39.  
  40. // Set up file menu.
  41. JMenuBar menuBar = new JMenuBar();
  42. JMenu fileMenu = new JMenu("File");
  43. fileMenu.setMnemonic(KeyEvent.VK_F);
  44. JMenuItem fileExitMenuItem = new JMenuItem("Exit",
  45. KeyEvent.VK_X);
  46. fileExitMenuItem.addActionListener(new ActionListener() {
  47. public void actionPerformed(ActionEvent e) {
  48. actionExit();
  49. }
  50. });
  51. fileMenu.add(fileExitMenuItem);
  52. menuBar.add(fileMenu);
  53. setJMenuBar(menuBar);
  54.  
  55. // Set up button panel.
  56. JPanel buttonPanel = new JPanel();
  57. backButton = new JButton("< Back");
  58. backButton.addActionListener(new ActionListener() {
  59. public void actionPerformed(ActionEvent e) {
  60. actionBack();
  61. }
  62. });
  63. backButton.setEnabled(false);
  64. buttonPanel.add(backButton);
  65. forwardButton = new JButton("Forward >");
  66. forwardButton.addActionListener(new ActionListener() {
  67. public void actionPerformed(ActionEvent e) {
  68. actionForward();
  69. }
  70. });
  71. forwardButton.setEnabled(false);
  72. buttonPanel.add(forwardButton);
  73. locationTextField = new JTextField(35);
  74. locationTextField.addKeyListener(new KeyAdapter() {
  75. public void keyReleased(KeyEvent e) {
  76. if (e.getKeyCode() == KeyEvent.VK_ENTER) {
  77. actionGo();
  78. }
  79. }
  80. });
  81. buttonPanel.add(locationTextField);
  82. JButton goButton = new JButton("GO");
  83. goButton.addActionListener(new ActionListener() {
  84. public void actionPerformed(ActionEvent e) {
  85. actionGo();
  86. }
  87. });
  88. buttonPanel.add(goButton);
  89.  
  90. // Set up page display.
  91. displayEditorPane = new JEditorPane();
  92. displayEditorPane.setContentType("text/html");
  93. displayEditorPane.setEditable(false);
  94. displayEditorPane.addHyperlinkListener(this);
  95.  
  96. getContentPane().setLayout(new BorderLayout());
  97. getContentPane().add(buttonPanel, BorderLayout.NORTH);
  98. getContentPane().add(new JScrollPane(displayEditorPane),
  99. BorderLayout.CENTER);
  100. }
  101.  
  102. // Exit this program.
  103. private void actionExit() {
  104. System.exit(0);
  105. }
  106.  
  107. // Go back to the page viewed before the current page.
  108. private void actionBack() {
  109. URL currentUrl = displayEditorPane.getPage();
  110. int pageIndex = pageList.indexOf(currentUrl.toString());
  111. try {
  112. showPage(
  113. new URL((String) pageList.get(pageIndex - 1)), false);
  114. }
  115. catch (Exception e) {}
  116. }
  117.  
  118. // Go forward to the page viewed after the current page.
  119. private void actionForward() {
  120. URL currentUrl = displayEditorPane.getPage();
  121. int pageIndex = pageList.indexOf(currentUrl.toString());
  122. try {
  123. showPage(
  124. new URL((String) pageList.get(pageIndex + 1)), false);
  125. }
  126. catch (Exception e) {}
  127. }
  128.  
  129. // Load and show the page specified in the location text field.
  130. private void actionGo() {
  131. URL verifiedUrl = verifyUrl(locationTextField.getText());
  132. if (verifiedUrl != null) {
  133. showPage(verifiedUrl, true);
  134. } else {
  135. showError("Invalid URL");
  136. }
  137. }
  138.  
  139. // Show dialog box with error message.
  140. private void showError(String errorMessage) {
  141. JOptionPane.showMessageDialog(this, errorMessage,
  142. "Error", JOptionPane.ERROR_MESSAGE);
  143. }
  144.  
  145. // Verify URL format.
  146. private URL verifyUrl(String url) {
  147. // Only allow HTTP URLs.
  148. if (!url.toLowerCase().startsWith("http://"))
  149. return null;
  150.  
  151. // Verify format of URL.
  152. URL verifiedUrl = null;
  153. try {
  154. verifiedUrl = new URL(url);
  155. } catch (Exception e) {
  156. return null;
  157. }
  158.  
  159. return verifiedUrl;
  160. }
  161.  
  162. /* Show the specified page and add it to
  163. the page list if specified. */
  164. private void showPage(URL pageUrl, boolean addToList)
  165. {
  166. // Show hour glass cursor while crawling is under way.
  167. setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
  168.  
  169. try {
  170. // Get URL of page currently being displayed.
  171. URL currentUrl = displayEditorPane.getPage();
  172.  
  173. // Load and display specified page.
  174. displayEditorPane.setPage(pageUrl);
  175.  
  176. // Get URL of new page being displayed.
  177. URL newUrl = displayEditorPane.getPage();
  178.  
  179. // Add page to list if specified.
  180. if (addToList) {
  181. int listSize = pageList.size();
  182. if (listSize > 0) {
  183. int pageIndex =
  184. pageList.indexOf(currentUrl.toString());
  185. if (pageIndex < listSize - 1) {
  186. for (int i = listSize - 1; i > pageIndex; i--) {
  187. pageList.remove(i);
  188. }
  189. }
  190. }
  191. pageList.add(newUrl.toString());
  192. }
  193.  
  194. // Update location text field with URL of current page.
  195. locationTextField.setText(newUrl.toString());
  196.  
  197. // Update buttons based on the page being displayed.
  198. updateButtons();
  199. }
  200. catch (Exception e)
  201. {
  202. // Show error messsage.
  203. showError("Unable to load page");
  204. }
  205. finally
  206. {
  207. // Return to default cursor.
  208. setCursor(Cursor.getDefaultCursor());
  209. }
  210. }
  211.  
  212. /* Update back and forward buttons based on
  213. the page being displayed. */
  214. private void updateButtons() {
  215. if (pageList.size() < 2) {
  216. backButton.setEnabled(false);
  217. forwardButton.setEnabled(false);
  218. } else {
  219. URL currentUrl = displayEditorPane.getPage();
  220. int pageIndex = pageList.indexOf(currentUrl.toString());
  221. backButton.setEnabled(pageIndex > 0);
  222. forwardButton.setEnabled(
  223. pageIndex < (pageList.size() - 1));
  224. }
  225. }
  226.  
  227. // Handle hyperlink's being clicked.
  228. public void hyperlinkUpdate(HyperlinkEvent event) {
  229. HyperlinkEvent.EventType eventType = event.getEventType();
  230. if (eventType == HyperlinkEvent.EventType.ACTIVATED) {
  231. if (event instanceof HTMLFrameHyperlinkEvent) {
  232. HTMLDocument document =
  233. (HTMLDocument) displayEditorPane.getDocument();
  234. document.processHTMLFrameHyperlinkEvent(linkEvent);
  235. } else {
  236. showPage(event.getURL(), true);
  237. }
  238. }
  239. }
  240.  
  241. // Run the Mini Browser.
  242. public static void main(String[] args) {
  243. MiniBrowser browser = new MiniBrowser();
  244. browser.show();
  245. }
  246. }
  247.  
Created by GeSHI 1.0.7.20


RESULT:

Logged

RuneScape Developers community: www.rspsnetwork.com
Hextion Private Server: www.hextion.com
Arkie
Javaforums.net Admin
Senior Member
*

Reputation: 16
Developer @ Javaforums.net
Offline Offline
Posts: 2593
Referrals: 13

WWW Awards
« Reply #1 on: July 31, 2009, 09:12:15 AM »

Looks good.

I've done a similiar application some time ago but found out that it doesn't (fully?) support css. As far as i know the issue still exists in the sdk 1.6 version although i'm not sure if its fixed with the latest update (1.6_13?) I've been planning to write my own browser render using java but haven't got the time for it yet.
Logged

Java and .NET developer

To students: It doesn't matter how hard you've studied; the material won't be on the exam anyway.

Fan of http://www.retardedweblogger.com
Oh man, too much stuff to do in so little time.

http://img222.imageshack....707/arkietomatoesmall.jpg
Blizzcon 2k9 Grubby and Cassandra Ng engaged ! <3
Triple D, eerste Denken Dan Doen
_ikram
Member
*

Reputation: 3
Offline Offline
Posts: 65
Referrals: 0

Awards
« Reply #2 on: July 31, 2009, 09:46:01 AM »

Looks good.

I've done a similiar application some time ago but found out that it doesn't (fully?) support css. As far as i know the issue still exists in the sdk 1.6 version although i'm not sure if its fixed with the latest update (1.6_13?) I've been planning to write my own browser render using java but haven't got the time for it yet.

Thanks, and no this doesnt fully support CSS :(
Logged

RuneScape Developers community: www.rspsnetwork.com
Hextion Private Server: www.hextion.com
Arkie
Javaforums.net Admin
Senior Member
*

Reputation: 16
Developer @ Javaforums.net
Offline Offline
Posts: 2593
Referrals: 13

WWW Awards
« Reply #3 on: August 03, 2009, 06:29:57 AM »

It came to my notice that you made a topic about it on your site as well: http://hextion.com/index.php?topic=221.0 and claiming i wrote the code. Whats up with that? (i've pmed you about it)
Logged

Java and .NET developer

To students: It doesn't matter how hard you've studied; the material won't be on the exam anyway.

Fan of http://www.retardedweblogger.com
Oh man, too much stuff to do in so little time.

http://img222.imageshack....707/arkietomatoesmall.jpg
Blizzcon 2k9 Grubby and Cassandra Ng engaged ! <3
Triple D, eerste Denken Dan Doen
_ikram
Member
*

Reputation: 3
Offline Offline
Posts: 65
Referrals: 0

Awards
« Reply #4 on: August 03, 2009, 11:14:53 AM »

It came to my notice that you made a topic about it on your site as well: http://hextion.com/index.php?topic=221.0 and claiming i wrote the code. Whats up with that? (i've pmed you about it)

sorry about the missunderstanding let me explain, i said i already posted this topic on "happyFaces fourm" so i think there was a bit of confusion.
Logged

RuneScape Developers community: www.rspsnetwork.com
Hextion Private Server: www.hextion.com
Arkie
Javaforums.net Admin
Senior Member
*

Reputation: 16
Developer @ Javaforums.net
Offline Offline
Posts: 2593
Referrals: 13

WWW Awards
« Reply #5 on: August 03, 2009, 11:16:47 AM »

sorry about the missunderstanding let me explain, i said i already posted this topic on "happyFaces fourm" so i think there was a bit of confusion.

Hmm ok, problem solved then.  dance
Logged

Java and .NET developer

To students: It doesn't matter how hard you've studied; the material won't be on the exam anyway.

Fan of http://www.retardedweblogger.com
Oh man, too much stuff to do in so little time.

http://img222.imageshack....707/arkietomatoesmall.jpg
Blizzcon 2k9 Grubby and Cassandra Ng engaged ! <3
Triple D, eerste Denken Dan Doen
_ikram
Member
*

Reputation: 3
Offline Offline
Posts: 65
Referrals: 0

Awards
« Reply #6 on: August 03, 2009, 11:25:11 AM »

Hmm ok, problem solved then.  dance
btw i gave u java guru rank on Hextion.com, btw who pmed you?
Logged

RuneScape Developers community: www.rspsnetwork.com
Hextion Private Server: www.hextion.com
Arkie
Javaforums.net Admin
Senior Member
*

Reputation: 16
Developer @ Javaforums.net
Offline Offline
Posts: 2593
Referrals: 13

WWW Awards
« Reply #7 on: August 03, 2009, 12:38:25 PM »

btw i gave u java guru rank on Hextion.com, btw who pmed you?

I don't think it's appropriate to say any names but the problem is solved right  dance
Logged

Java and .NET developer

To students: It doesn't matter how hard you've studied; the material won't be on the exam anyway.

Fan of http://www.retardedweblogger.com
Oh man, too much stuff to do in so little time.

http://img222.imageshack....707/arkietomatoesmall.jpg
Blizzcon 2k9 Grubby and Cassandra Ng engaged ! <3
Triple D, eerste Denken Dan Doen
Pages: [1]   Go Up
  Print  
 
Jump to:  

Your Ad Here