sciocosmist 0 Newbie Poster

I am having some trouble trying to get a JApplet to display in a Browser. 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)
//////////////////////////////////////////


Code:
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)
///////////////////////////////////////////////


Code:
<head>
</head>


<body>
<applet code="HelloWorld.class">
</applet>


</body>



<!--Thanks for your help! -->


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


End  HTML  file


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



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


Just  in Case the Java file doesn't load


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


Code:
/**
*
* @author Bob Smith
*/



import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.MouseEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.NoSuchElementException;
import java.util.StringTokenizer;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.filechooser.*;
import javax.swing.filechooser.FileFilter;
import javax.swing.JApplet;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;



public class HelloWorld extends javax.swing.JApplet
{
FileReader fr;//can be removed
BufferedReader br;//dido
StringTokenizer st;//dido
//ChangeLine
String temp="", line="";//line="Thanks TAs";
JLabel label;
JPanel panel, radio_Pan, combo_Pan, button_Pan, check_Pan;
JRadioButton rb1=new JRadioButton("Option1"), rb2=new JRadioButton("Option2");
JComboBox combo=new JComboBox();//I don't ever fill this, I just wanted it for display
JCheckBox ch1=new JCheckBox ("ch1"), ch2=new JCheckBox("ch2");
JButton yes=new JButton("Yes"), no=new JButton ("No");
JFrame frame = new JFrame("Hello World");



/*Creates a new instance of HelloWorld */
public HelloWorld()
{
stuff();
}
public void stuff()//sets up the JApplet
{
try
{



//From here to next comment, will need to be commented if the filereader is removed
fr=new FileReader("HelloWorld.txt");
br=new BufferedReader (fr);
temp=br.readLine();
st=new StringTokenizer(temp, " \t\r\n%@&(*");


while(st.hasMoreTokens())
{
line+=st.nextToken() + " ";
}
System.out.println(line);
//From here to previous comment, will need to be commented if the filereader is removed



label=new JLabel (line);
label.setForeground(Color.red);
panel=new JPanel();
button_Pan=new JPanel();
radio_Pan=new JPanel();
combo_Pan=new JPanel();
check_Pan=new JPanel();


button_Pan.setLayout(new BoxLayout( button_Pan, BoxLayout.X_AXIS));
button_Pan.add(yes);
button_Pan.add(no);


combo_Pan.add(combo);


radio_Pan.add(rb1);
radio_Pan.add(rb2);



check_Pan.add(ch1);
check_Pan.add(ch2);



panel.setLayout(new BorderLayout());
panel.add(label, BorderLayout.CENTER);


panel.add(button_Pan,BorderLayout.SOUTH);
panel.add(radio_Pan, BorderLayout.WEST);
panel.add(combo_Pan,BorderLayout.EAST);
panel.add(check_Pan, BorderLayout.NORTH);



setContentPane(panel);


frame.getContentPane().add(panel);
frame.setSize(400, 400);
frame.pack();
frame.setVisible(true);
frame.validate();
}
//  catch (Exception e)
//  {
//  }
////////////////////////////////////////////
//These catches will need to be commented out if the filereader is removed
catch(FileNotFoundException fnfe)
{
System.out.println(fnfe.toString());
}
catch (IOException ioe)
{
System.out.println(ioe.toString());
}
catch (NoSuchElementException nsee)
{
System.out.println(nsee.toString());
}


}
public void paint(Graphics g)
{
frame.paint(g);
panel.paint(g);
label.paint(g);



super.paint(g);
this.paint(g);
}



public void init()
{


this.setBackground(Color.white);
this.setSize(400, 400);
Container contentPane = this.getContentPane();
//contentPane.setLayout(new BorderLayout());
//contentPane.add(panel, BorderLayout.CENTER);
}


public static void main(String[] args) throws IOException
{
HelloWorld hw = new HelloWorld();
hw.init();


}



}


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


End  of Java  file


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

Thanks for your help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.