JApplet display problem

Reply

Join Date: Aug 2006
Posts: 19
Reputation: sciocosmist is an unknown quantity at this point 
Solved Threads: 0
sciocosmist sciocosmist is offline Offline
Newbie Poster

JApplet display problem

 
0
  #1
Aug 1st, 2006
I am having some trouble trying to get a JApplet to display in a Browser http://javafaq.nu/images/smiles/icon_sad.gif . The applet does compile and does come up (displays) in a Microsoft Windows window frame, but does not display in a browser. I have several components that I place in JPanels and successfully place those in JPanels and finally into a JFrame.

Additionally, I am using a FileReader to extract some information that is then displayed. The program compiles and runs, but does not display in a internet browser (neither Firefox 1.5.05, IE 7.05, nor Samba). I would still like to have the option to read in multiple files, so if anyone could show me how to do this, that would be great.

I am questioning both my init() method and my paint(Graphics g) method. I have included a very simplistic version of the file (modified) that has many of the same features. (With this simplified version, it has the same problem as the more complex program in that it doesn't show up in a browser.)

If anyone would be willing to help me out, I would appreciate it. I have included the .java file, the .html file, and the .txt file that displays the information. Please let me know what I can do to allow this program to display in a Browser.

///////////////////////////////////////////
The text file

Save the text as 'HelloWorld.txt' the
file should be one line of text.

(Below the forward slashes)
//////////////////////////////////////////




  1. Hello@@@@@@@%%%%%%%%%%%%%%%%%%%%%%% Fellow Java Programmers&&&&&&&&&&&&&&&&&&&&&&&&&, Thank**********You(((((((((((((((((((( *************For Your @@@@@@@@Help!***********




//////////////////////////////////////////////
End of Text file
//////////////////////////////////////////////




///////////////////////////////////////////////

Here is the coding for the HTML file

(Copy the file below the forward slashes)

Just for verification, would you please
include the directory information that I
would need to save this in (ie same
directory, an upper or sub directory)
///////////////////////////////////////////////

  1. <head>
  2. </head>
  3.  
  4. <body>
  5. <applet code="HelloWorld.class">
  6. </applet>
  7.  
  8. </body>
  9.  
  10.  
  11. <!--Thanks for your help! -->



/////////////////////////////////////////////////

End HTML file

/////////////////////////////////////////////////

/////////////////////////////////////////////////

Just in Case the Java file doesn't load

Save this as HelloWorld.java
/////////////////////////////////////////////////


  1. /**
  2.  *
  3.  * @author Bob Smith
  4.  */
  5.  
  6.  
  7. import java.applet.Applet;
  8. import java.awt.*;
  9. import java.awt.event.*;
  10. import java.awt.event.MouseEvent;
  11. import java.io.BufferedReader;
  12. import java.io.BufferedWriter;
  13. import java.io.File;
  14. import java.io.FileNotFoundException;
  15. import java.io.FileOutputStream;
  16. import java.io.FileReader;
  17. import java.io.FileWriter;
  18. import java.io.IOException;
  19. import java.io.ObjectOutputStream;
  20. import java.io.PrintWriter;
  21. import java.util.ArrayList;
  22. import java.util.HashMap;
  23. import java.util.NoSuchElementException;
  24. import java.util.StringTokenizer;
  25. import javax.swing.*;
  26. import javax.swing.border.*;
  27. import javax.swing.filechooser.*;
  28. import javax.swing.filechooser.FileFilter;
  29. import javax.swing.JApplet;
  30. import javax.swing.JInternalFrame;
  31. import javax.swing.JLabel;
  32. import javax.swing.JOptionPane;
  33.  
  34.  
  35. public class HelloWorld extends javax.swing.JApplet
  36. {
  37. FileReader fr;//can be removed
  38. BufferedReader br;//dido
  39. StringTokenizer st;//dido
  40. //ChangeLine
  41. String temp="", line="";//line="Thanks TAs";
  42. JLabel label;
  43. JPanel panel, radio_Pan, combo_Pan, button_Pan, check_Pan;
  44. JRadioButton rb1=new JRadioButton("Option1"), rb2=new JRadioButton("Option2");
  45. JComboBox combo=new JComboBox();//I don't ever fill this, I just wanted it for display
  46. JCheckBox ch1=new JCheckBox ("ch1"), ch2=new JCheckBox("ch2");
  47. JButton yes=new JButton("Yes"), no=new JButton ("No");
  48. JFrame frame = new JFrame("Hello World");
  49.  
  50.  
  51. /*Creates a new instance of HelloWorld */
  52. public HelloWorld()
  53. {
  54. stuff();
  55. }
  56. public void stuff()//sets up the JApplet
  57. {
  58. try
  59. {
  60.  
  61.  
  62. //From here to next comment, will need to be commented if the filereader is removed
  63. fr=new FileReader("HelloWorld.txt");
  64. br=new BufferedReader (fr);
  65. temp=br.readLine();
  66. st=new StringTokenizer(temp, " \t\r\n%@&(*");
  67.  
  68. while(st.hasMoreTokens())
  69. {
  70. line+=st.nextToken() + " ";
  71. }
  72. System.out.println(line);
  73. //From here to previous comment, will need to be commented if the filereader is removed
  74.  
  75.  
  76. label=new JLabel (line);
  77. label.setForeground(Color.red);
  78. panel=new JPanel();
  79. button_Pan=new JPanel();
  80. radio_Pan=new JPanel();
  81. combo_Pan=new JPanel();
  82. check_Pan=new JPanel();
  83.  
  84. button_Pan.setLayout(new BoxLayout( button_Pan, BoxLayout.X_AXIS));
  85. button_Pan.add(yes);
  86. button_Pan.add(no);
  87.  
  88. combo_Pan.add(combo);
  89.  
  90. radio_Pan.add(rb1);
  91. radio_Pan.add(rb2);
  92.  
  93.  
  94. check_Pan.add(ch1);
  95. check_Pan.add(ch2);
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102. panel.setLayout(new BorderLayout());
  103. panel.add(label, BorderLayout.CENTER);
  104.  
  105. panel.add(button_Pan,BorderLayout.SOUTH);
  106. panel.add(radio_Pan, BorderLayout.WEST);
  107. panel.add(combo_Pan,BorderLayout.EAST);
  108. panel.add(check_Pan, BorderLayout.NORTH);
  109.  
  110.  
  111. setContentPane(panel);
  112.  
  113. frame.getContentPane().add(panel);
  114. frame.setSize(400, 400);
  115. frame.pack();
  116. frame.setVisible(true);
  117. frame.validate();
  118. }
  119. // catch (Exception e)
  120. // {
  121. // }
  122. ////////////////////////////////////////////
  123. //These catches will need to be commented out if the filereader is removed
  124. catch(FileNotFoundException fnfe)
  125. {
  126. System.out.println(fnfe.toString());
  127. }
  128. catch (IOException ioe)
  129. {
  130. System.out.println(ioe.toString());
  131. }
  132. catch (NoSuchElementException nsee)
  133. {
  134. System.out.println(nsee.toString());
  135. }
  136.  
  137. }
  138. public void paint(Graphics g)
  139. {
  140. frame.paint(g);
  141. panel.paint(g);
  142. label.paint(g);
  143.  
  144.  
  145. super.paint(g);
  146. this.paint(g);
  147. }
  148.  
  149.  
  150. public void init()
  151. {
  152.  
  153. this.setBackground(Color.white);
  154. this.setSize(400, 400);
  155. Container contentPane = this.getContentPane();
  156. //contentPane.setLayout(new BorderLayout());
  157. //contentPane.add(panel, BorderLayout.CENTER);
  158. }
  159.  
  160. public static void main(String[] args) throws IOException
  161. {
  162. HelloWorld hw = new HelloWorld();
  163. hw.init();
  164.  
  165. }
  166.  
  167.  
  168. }


////////////////////////////////////////////////////////////////////

End of Java file

////////////////////////////////////////////////////////////////////




Thanks for your help!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: JApplet display problem

 
0
  #2
Aug 2nd, 2006
You need the .class file in the same directory as well!

I've tested it using internet explorer, something comes up, but it goes mad.
Firefox refuses to work. The code probably has a few bugs in there?
Last edited by iamthwee; Aug 2nd, 2006 at 5:15 am.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: JApplet display problem

 
0
  #3
Aug 2nd, 2006
You can't do that unless the Applet is signed.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 19
Reputation: sciocosmist is an unknown quantity at this point 
Solved Threads: 0
sciocosmist sciocosmist is offline Offline
Newbie Poster

Re: JApplet display problem

 
0
  #4
Aug 2nd, 2006
Originally Posted by server_crash
You can't do that unless the Applet is signed.

What do you mean by signed? How do I make something signed? Why does the file compile and display in a Windows window frame, but not in the browser? Your thoughts would be appreciated.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 216
Reputation: hooknc is an unknown quantity at this point 
Solved Threads: 8
hooknc hooknc is offline Offline
Posting Whiz in Training

Re: JApplet display problem

 
0
  #5
Aug 2nd, 2006
Signed means that the Applet has access to the browsers local machine.

Due to security risks associated with Applets, applets are NOT ALLOWED TO ACCESS THE LOCAL MACHINE. However, if the applet is Signed then it is allowed to access the local machine.

I do not know about how to get an Applet Signed, but i'm sure there are plenty of pages on the triple W that will tell you how to do it.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 19
Reputation: sciocosmist is an unknown quantity at this point 
Solved Threads: 0
sciocosmist sciocosmist is offline Offline
Newbie Poster

Re: JApplet display problem

 
0
  #6
Aug 2nd, 2006
Originally Posted by hooknc
Signed means that the Applet has access to the browsers local machine.

Due to security risks associated with Applets, applets are NOT ALLOWED TO ACCESS THE LOCAL MACHINE. However, if the applet is Signed then it is allowed to access the local machine.

I do not know about how to get an Applet Signed, but i'm sure there are plenty of pages on the triple W that will tell you how to do it.
What about if I don't use any IO, I still do not know how to display a JApplet using panels and other components . Try it if you would like; comment out my IO (I commented so that it could easily be done), and then see if the JApplet will display. This is what I meant in my original posting saying I thought that there was a problem with my init() and paint(Graphics g) methods.

If someone knows how to sign an Applet (see previous responses) and/or display a JApplet with panels, frames, and other components, please let me know. That would be really cool.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: JApplet display problem

 
0
  #7
Aug 2nd, 2006
What about if I don't use any IO, I still do not know how to display a JApplet using panels and other components
Try coding an extremely trivial applet. You should be able to get it to work with your browser.

All you have to do is provide the class file.

If someone knows how to sign an Applet (see previous responses) and/or display a JApplet with panels, frames, and other components, please let me know. That would be really cool.
To be honest, it looks more trouble than it's worth.

http://www.suitable.com/docs/signing.html

You'll have to consider various conditions depending on which browser you're using, if you intend to release the code into the public domain.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 19
Reputation: sciocosmist is an unknown quantity at this point 
Solved Threads: 0
sciocosmist sciocosmist is offline Offline
Newbie Poster

Re: JApplet display problem

 
0
  #8
Aug 2nd, 2006
Originally Posted by iamthwee
Try coding an extremely trivial applet. You should be able to get it to work with your browser.

All you have to do is provide the class file.


The coding for what I have posted seems to be a trivial applet, but for some reason unkown to me, I have not been able to get a JApplet to display with panels (or other components for that matter) in a browser. (I have had limited sucess with JApplets that draw various items, but not with the JComponents.) Would someone be willing to post an example of both the HTML and java coding needed to make and display in a browser a JApplet with 1 JPanel, 1 JLabel, and 1 JButton. I don't care if the button does anything:eek: , I just would like to see a working example of a java program that makes a JApplet display in a browser. I have tried repeated times with various versions of JApplets, and so far I have had no sucess getting the JApplet to display in a browser. Additionally, the internet postings of JApplets that I have seen are ones that use the draw() method. If someone could send me the coding for how to do this, I would appreciate it.:cheesy: (As you can tell by the coding in the first posting of this thread, I have tried with the class file from the program posted, but to no avail.)

As always, Thank you to everyone that has offered help!
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 9
Reputation: ede is an unknown quantity at this point 
Solved Threads: 2
ede's Avatar
ede ede is offline Offline
Newbie Poster

Re: JApplet display problem

 
0
  #9
Aug 4th, 2006
ı look at your applet code and noticed you added main(). in Applets no need a main method. applets start with method init(). same Threads start with run() method.

ı wrote a sample applet code. ı wish it will be useful.

and as other friends point out to be able to show applets in local machine some security operations must done. but ı have not enough information about this. however recently ı did a change in JDK where there is a file named java.policy. if you add like following permission you may succeed.

grant {
permission java.io.FilePermission "HelloWorld.txt", "read, write, delete, execute";
};

but this may be dangerous!

here's code ı wrote. and ı test it successfully.

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import javax.swing.*;

public class appletDemo extends JApplet implements ActionListener {
private demoPanel panel;

private JPanel panel2;

private JLabel label;

private JTextArea area;

private JButton button;

public void init() {
Container c = getContentPane();
c.setLayout(new BorderLayout());
panel = new demoPanel();
panel.setBackground(Color.decode("#98CCFF"));
panel.setBorder(BorderFactory.createTitledBorder("JPanel"));
panel2 = new JPanel();
panel2.setLayout(new BorderLayout());
panel2.setBackground(Color.decode("#D1E8FF"));
panel2.setBorder(BorderFactory.createTitledBorder("JTextArea Panel"));
area = new JTextArea();
panel2.add(new JScrollPane(area));
JPanel p = new JPanel();
label = new JLabel("Press button to show content");
p.add(label);
button = new JButton("FILE READ");
button.addActionListener(this);
p.add(button);
c.add(p, BorderLayout.NORTH);
c.add(panel, BorderLayout.SOUTH);
c.add(panel2, BorderLayout.CENTER);

}

public void fileRead() {
BufferedReader reader = null;
try {
reader = new BufferedReader(
new FileReader(
"C:\\Documents and Settings\\feytullah\\Desktop\\HelloWorld.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
String line = "";
while (true) {
try {
line = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (line == null)
break;
area.append(line + "\n");
}
}

public void actionPerformed(ActionEvent e) {
fileRead();
}

public class demoPanel extends JPanel {

public Dimension getPrefferedSize() {
return new Dimension(400, 100);
}

public void paint(Graphics g) {
// Call super class constuctors
super.paint(g);
g.drawString("JApplet demo!!!", 25, 25);
}
}

}
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 19
Reputation: sciocosmist is an unknown quantity at this point 
Solved Threads: 0
sciocosmist sciocosmist is offline Offline
Newbie Poster

Re: JApplet display problem

 
0
  #10
Aug 4th, 2006
Originally Posted by ede
ı look at your applet code and noticed you added main(). in Applets no need a main method. applets start with method init(). same Threads start with run() method.

ı wrote a sample applet code. ı wish it will be useful.

and as other friends point out to be able to show applets in local machine some security operations must done. but ı have not enough information about this. however recently ı did a change in JDK where there is a file named java.policy. if you add like following permission you may succeed.

grant {
permission java.io.FilePermission "HelloWorld.txt", "read, write, delete, execute";
};

but this may be dangerous!

Thank you for your help and for answering one question that I had by the example code that you sent me. First, I was able to see that the JApplet works in an Applet Viewer, and I thank you for the program. Second, I was able to get the panels to display in a browser (this was very helpful to see an example that did load in a browser).

There is a difference in performance between the Applet Viewer version and the Browser version, in that I couldn't get the information that was in the *.txt file to display when the applet was displayed in a Browser.

Does anyone know how to read in and also write a file from a JApplet while the applet is displayed in a browser? In a previous posting, someone mentioned that the applet would have to be signed. IF THIS IS TRUE could someone tell me how to/refer me to a site that is very easy to follow -- being as I have never done this before:surprised .

Additionally, if someone could tell me how to use the command mentioned above:

grant {
permission java.io.FilePermission "HelloWorld.txt", "read, write, delete, execute";
};
With the instructions on how to use this, additionally could someone mention the security risks involved?

Thanks for your help!

--sciocosmist
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC