Java Applet

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Mar 2008
Posts: 31
Reputation: kinger29 is an unknown quantity at this point 
Solved Threads: 1
kinger29 kinger29 is offline Offline
Light Poster

Java Applet

 
0
  #1
Nov 10th, 2009
I have a java aplpet in eclipse. How do i get my java applet to load in an html web page. I put the .class file from the bin director in the same directory as my web page. I tried applet tag but it gives me an empty white box. Here is the code for my web page.

  1. <html>
  2. <body >
  3. <applet code="Main2.class" width="800" height="600">
  4. </body>
  5. </html>
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,844
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster
 
0
  #2
Nov 10th, 2009
Looks fine to me. You need to get the error message. Does it work when you run it on a local machine? Try putting Main2.class and the html page in the same folder on your local hard drive and double-clicking on the html file to open a browser. See if it works. If not, run it through appletviewer at the command line and see what error mssage(s) you get.

http://java.sun.com/j2se/1.5.0/docs/...gs.html#applet

This link may be of some help. From it:

  1. <applet code=Applet1.class width="200" height="200">
  2. Your browser does not support the <code>applet</code> tag.
  3. </applet>

You might want to stick in the "Your browser does not support..." line in there. Again, everything looks fine to me, so you need to find the exact error (i.e. can't find Main2.class or something).
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 31
Reputation: kinger29 is an unknown quantity at this point 
Solved Threads: 1
kinger29 kinger29 is offline Offline
Light Poster
 
0
  #3
Nov 10th, 2009
I added the browser does not support line and it is still just giving me an empty white box. When I run the applet in the applet viewer it works just fine. Here is my applet code. Did I set it up incorrectly? I believe I have gotten applets to work this way before. Do I need to implement start(), stop(), or destroy() methods?

  1.  
  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.util.*;
  6. import java.applet.Applet;
  7. import javax.swing.*;
  8.  
  9.  
  10.  
  11. public class Main3 extends JApplet {
  12.  
  13. //globals for user interface
  14. private Container content;
  15. private JPanel centerPanel; //Bottom panel that changes
  16. private CardLayout card; //Layout for the applet
  17. //globals for Euclidean Algorithm
  18. private static JTextField eucl_ai;
  19. private static JTextField eucl_bi;
  20. private static JTextArea eucl_o;
  21.  
  22. //This will initialize all of the GUIs, even the ones not showing
  23. public void init()
  24. {
  25.  
  26. //Applet Settings
  27. content = getContentPane();
  28. setSize(800, 600);
  29. content.setBackground(Color.GREEN);
  30. content.setLayout(new BorderLayout());
  31.  
  32. //This is the panel that contains all the buttons
  33. JPanel northPanel = new JPanel();
  34.  
  35. //Button for the Vigenere cipher
  36. JButton vigenere_b = new JButton("Vigenere Cipher");
  37. vigenere_b.addActionListener(new ActionListener() {
  38.  
  39. public void actionPerformed(ActionEvent e) {
  40. vigenere_gui();
  41. }
  42. });
  43. northPanel.add(vigenere_b);
  44.  
  45. //Button for the Euclidean Algorithm
  46. JButton eucl_b = new JButton("Euclidean");
  47. eucl_b.addActionListener(new ActionListener() {
  48.  
  49. public void actionPerformed(ActionEvent e) {
  50. eucl_gui();
  51. }
  52. });
  53. northPanel.add(eucl_b);
  54.  
  55. //Button for Reset
  56. JButton reset_b = new JButton("Reset");
  57. reset_b.addActionListener(new ActionListener() {
  58.  
  59. public void actionPerformed(ActionEvent e) {
  60. reset_gui();
  61. }
  62. });
  63. northPanel.add(reset_b);
  64.  
  65. //Add the north panel to the applet
  66. content.add(northPanel, BorderLayout.NORTH);
  67.  
  68.  
  69. //Set the layout in the applet to cardlayout
  70. centerPanel = new JPanel();
  71. card = new CardLayout();
  72. centerPanel.setLayout(card);
  73.  
  74. //Set the gui for the Euclidean Algorithm
  75. JPanel euclPanel = new JPanel();
  76. euclPanel.setBackground(Color.WHITE);
  77. SpringLayout eucl_layout = new SpringLayout();
  78. euclPanel.setLayout(eucl_layout);
  79. //Text
  80. JTextField eucl_1 = new JTextField();
  81. eucl_1.setBorder(BorderFactory.createEmptyBorder());
  82. eucl_1.setText("Extended Euclidean Algorithm");
  83. eucl_1.setEditable(false);
  84. eucl_1.setBackground(Color.WHITE);
  85. eucl_layout.putConstraint(SpringLayout.WEST, eucl_1, 5, SpringLayout.NORTH, content);
  86. eucl_layout.putConstraint(SpringLayout.NORTH, eucl_1, 5, SpringLayout.NORTH, content);
  87. euclPanel.add(eucl_1);
  88. //Text
  89. JTextField eucl_2 = new JTextField();
  90. eucl_2.setBorder(BorderFactory.createEmptyBorder());
  91. eucl_2.setText("Enter a,b to find gcd(a,b)");
  92. eucl_2.setEditable(false);
  93. eucl_2.setBackground(Color.WHITE);
  94. eucl_layout.putConstraint(SpringLayout.WEST, eucl_2, 5, SpringLayout.NORTH, content);
  95. eucl_layout.putConstraint(SpringLayout.NORTH, eucl_2, 55, SpringLayout.NORTH, content);
  96. euclPanel.add(eucl_2);
  97. //Text
  98. JTextField eucl_3 = new JTextField();
  99. eucl_3.setBorder(BorderFactory.createEmptyBorder());
  100. eucl_3.setText("a:");
  101. eucl_3.setEditable(false);
  102. eucl_3.setBackground(Color.WHITE);
  103. eucl_layout.putConstraint(SpringLayout.WEST, eucl_3, 5, SpringLayout.NORTH, content);
  104. eucl_layout.putConstraint(SpringLayout.NORTH, eucl_3, 80, SpringLayout.NORTH, content);
  105. euclPanel.add(eucl_3);
  106. //User Input
  107. eucl_ai = new JTextField(3);
  108. eucl_ai.setBorder(BorderFactory.createLineBorder(Color.BLACK));
  109. eucl_layout.putConstraint(SpringLayout.WEST, eucl_ai, 25, SpringLayout.NORTH, content);
  110. eucl_layout.putConstraint(SpringLayout.NORTH, eucl_ai, 80, SpringLayout.NORTH, content);
  111. euclPanel.add(eucl_ai);
  112. //Text
  113. JTextField eucl_4 = new JTextField();
  114. eucl_4.setBorder(BorderFactory.createEmptyBorder());
  115. eucl_4.setText("b:");
  116. eucl_4.setEditable(false);
  117. eucl_4.setBackground(Color.WHITE);
  118. eucl_layout.putConstraint(SpringLayout.WEST, eucl_4, 75, SpringLayout.NORTH, content);
  119. eucl_layout.putConstraint(SpringLayout.NORTH, eucl_4, 80, SpringLayout.NORTH, content);
  120. euclPanel.add(eucl_4);
  121. //User Input
  122. eucl_bi = new JTextField(3);
  123. eucl_bi.setBorder(BorderFactory.createLineBorder(Color.BLACK));
  124. eucl_layout.putConstraint(SpringLayout.WEST, eucl_bi, 95, SpringLayout.NORTH, content);
  125. eucl_layout.putConstraint(SpringLayout.NORTH, eucl_bi, 80, SpringLayout.NORTH, content);
  126. euclPanel.add(eucl_bi);
  127. //Submit Button
  128. JButton eucl_submit = new JButton("Submit");
  129. eucl_submit.addActionListener(new ActionListener() {
  130.  
  131. public void actionPerformed(ActionEvent e) {
  132. process_eucl();
  133. }
  134. });
  135. eucl_layout.putConstraint(SpringLayout.WEST, eucl_submit, 5, SpringLayout.NORTH, content);
  136. eucl_layout.putConstraint(SpringLayout.NORTH, eucl_submit, 105, SpringLayout.NORTH, content);
  137. euclPanel.add(eucl_submit);
  138. //Text
  139. JTextField eucl_5 = new JTextField();
  140. eucl_5.setBorder(BorderFactory.createEmptyBorder());
  141. eucl_5.setText("Output:");
  142. eucl_5.setEditable(false);
  143. eucl_5.setBackground(Color.WHITE);
  144. eucl_layout.putConstraint(SpringLayout.WEST, eucl_5, 5, SpringLayout.NORTH, content);
  145. eucl_layout.putConstraint(SpringLayout.NORTH, eucl_5, 155, SpringLayout.NORTH, content);
  146. euclPanel.add(eucl_5);
  147. //Output
  148. eucl_o = new JTextArea(15,30);
  149. eucl_o.setBorder(BorderFactory.createLineBorder(Color.BLACK));
  150. JScrollPane eucl_o_scroll = new JScrollPane(eucl_o);
  151. eucl_o_scroll.setPreferredSize(new Dimension(490,330));
  152. eucl_layout.putConstraint(SpringLayout.WEST, eucl_o_scroll, 5, SpringLayout.NORTH, content);
  153. eucl_layout.putConstraint(SpringLayout.NORTH, eucl_o_scroll, 180, SpringLayout.NORTH, content);
  154. euclPanel.add(eucl_o_scroll);
  155. //
  156. centerPanel.add("eucl", euclPanel);
  157.  
  158.  
  159. //Set the gui for the vigener cipher
  160. JPanel vigenerePanel = new JPanel();
  161. vigenerePanel.setBackground(Color.BLUE);
  162. vigenerePanel.add(new JButton("button vigenere"));
  163. centerPanel.add("vigenere", vigenerePanel);
  164.  
  165.  
  166. //Set the gui for another panel
  167. JPanel emptyPanel = new JPanel();
  168. emptyPanel.setBackground(Color.WHITE);
  169. centerPanel.add("empty", emptyPanel);
  170.  
  171. //Center components
  172. content.add(centerPanel, BorderLayout.CENTER);
  173. }
  174.  
  175. //This method displays the vigenere cipher in the center panel
  176. public void vigenere_gui() {
  177. card.show(centerPanel, "vigenere");
  178. }
  179.  
  180. //This method displays the Euclidean in the center panel
  181. public void eucl_gui() {
  182. card.show(centerPanel, "eucl");
  183. }
  184.  
  185. //This method resets the gui
  186. public void reset_gui() {
  187. //...
  188. }
  189.  
  190.  
  191. //
  192. // Extended Euclidean Algorithm for integers a & b
  193. //
  194. public void process_eucl() {
  195.  
  196. //...
  197.  
  198. }
  199.  
  200.  
  201.  
  202. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,844
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster
 
0
  #4
Nov 10th, 2009
One, your class is named Main3, not Main1, so make sure your html file points to the actual class file. I renamed the class to Main1 and compiled it. When I did, it created Main1.class, but it also created these files too:
  • Main1$1.class
  • Main1$2.class
  • Main1$3.class
  • Main1$4.class

Make sure you copy them too. If you only copied Main1.class, you could have problems.

When I did that, it worked, both in Firefox and Internet Explorer, but it only worked in IE after I clicked the "Allow Add-On".

So try renaming Main3 to Main1 in order to match the html file, make sure you copied the $ files too, and try it again. If it still doesn't work, try clearing your browser's cache, exiting it, and opening it again.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 31
Reputation: kinger29 is an unknown quantity at this point 
Solved Threads: 1
kinger29 kinger29 is offline Offline
Light Poster
 
0
  #5
Nov 10th, 2009
The Main3 issue was not my problem that was something else I was trying. My problem was that I wasn't importing the files with the '$' in it. Thanks I really appreciate the help
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 304 | Replies: 4
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC