Results 1 to 4 of 4
  1. #1
    Grizz is offline Member
    Join Date
    Sep 2011
    Posts
    2
    Rep Power
    0

    Default Average Counter Applet works in Eclipse but not in browser.

    Hi all first time poster new to java and applets.
    I am trying to write an Applet that reads user inputted file names with an button press. I can get the applet to run when executed from Eclipse. However when I load put it on my webpage it loads but the button clicks don't do anything, such as even tell me the file doesn't exist. Below is the code, any help would be GREATLY appreciated!
    PHP Code:
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    
    public class AverageCounter extends Applet implements ActionListener {
    	
    	private static final long serialVersionUID = -3070441705237174071L;
    	Panel mainPanel, buttonPanel, displayPanel;
    	Label mainLabel, mainLabel2, mainLabel3, resultLabel, resultLabel2;
    	Button runButton;
    	TextField nameField;
    	Thread counterThread = null;
    	float average;
    	float sum;
    	
    	public void init(){
    		//setLayout(new FlowLayout());
    		setLayout(null);
    		setBackground(Color.GREEN);
    		
    		mainPanel = new Panel();
    		mainPanel.setBackground(Color.GRAY);
    		mainPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
    		mainPanel.setFont(new Font("Helveteca", Font.BOLD, 12));	
    		mainLabel = new Label("This program calculates the Average of a list of integers");
    		mainLabel.setBackground(Color.GRAY);
    		mainLabel2 = new Label ("provided by an input file.");
    		mainLabel2.setBackground(Color.GRAY);
    		mainLabel3 = new Label("(Hint: Try infile.txt and infile1.txt)");
    		mainLabel3.setBackground(Color.GRAY);
    		
    		mainPanel.add(mainLabel);
    		mainPanel.add(mainLabel2);
    		mainPanel.add(mainLabel3);
    		mainPanel.setBounds(10, 24, 400, 90);
    		add(mainPanel);
    		
    		nameField = new TextField("Input file name.", 35);
    		nameField.setBounds(125, 150, 100, 20);
    		add(nameField);
    		nameField.addActionListener(this);
    		
    		buttonPanel = new Panel();
    		buttonPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
    		buttonPanel.setBackground(Color.green);
    		buttonPanel.setBounds(100,175,250,50);
    		add(buttonPanel);
    		runButton = new Button("Calculate based off typed filename.");
    		runButton.addActionListener(this);
    		runButton.setBackground(Color.GRAY);
    		runButton.setForeground(Color.BLACK);
    		runButton.setFont(new Font("Helveteca", Font.BOLD, 12));
    		buttonPanel.add(runButton);		
    		
    		displayPanel = new Panel();
    		displayPanel.setBounds(50, 250, 500,30);
    		displayPanel.setBackground(Color.cyan);
    		displayPanel.setFont(new Font("Helveteca", Font.BOLD, 12));
    		add(displayPanel);
    		
    		resultLabel = new Label("                                                                                                                           ");
    		resultLabel.setForeground(Color.black);
    		displayPanel.add(resultLabel);
    		start();
    		
    	}
    
    	public void start(){
    		nameField.requestFocus();
    		runButton.requestFocus();
    	}
    	public void actionPerformed(ActionEvent e) {
    			doRun();
    }	
    	
    	public void doRun(){
    	 int count = 0;
    	  	 int sum = 0;
    	  	 double avg = 0.0;
      		 String  line;
    	  	 String fileName = nameField.getText().toString();
    	  	 File file = new File(fileName);
        if(file.exists() )
        {
    	  	 try{
    	  		FileReader fReader = new FileReader(file);
    	  		BufferedReader in = new BufferedReader(fReader);
    	      while ((line = in.readLine()) != null) { 	 
    	    	  if (line.length() >= 2)
    	    	  		{ 			    	
    	    		  resultLabel.setText("There is a character larger than one integer value. Please review input file.");	    	  
    	    	  			}
    	    	  else {			    		    
    	    	  sum +=  Integer.parseInt(line);
    	    	  count ++;
    	    	  avg = sum / count;	
    		  	  	resultLabel.setText("There were " + count + " integers and their average value is " + avg +".");
    	    	  }
    	    	}  
    	  	}catch (IOException d)  {}	
        	}
        else{
          	 resultLabel.setText("File name does not exist. Please check your file spelling: " + fileName);
        }
      }	
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Average Counter Applet works in Eclipse but not in browser.

    Have you looked at the browser's Java console for any exceptions or error messages? I expect that you would need a signed applet for this to work via a web page. Is your applet signed?

  3. #3
    Grizz is offline Member
    Join Date
    Sep 2011
    Posts
    2
    Rep Power
    0

    Default Re: Average Counter Applet works in Eclipse but not in browser.

    Thanks for the quick reply. I am not familiar with having Applets signed I will look into that. If you have a good resource for that it would be great. There are ton's of errors in the console of the browser is there anything specific I should be looking for in there?

    Thanks again!

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Average Counter Applet works in Eclipse but not in browser.

    Quote Originally Posted by Grizz View Post
    Thanks for the quick reply. I am not familiar with having Applets signed I will look into that. If you have a good resource for that it would be great.
    You'll have to Google for this.

    There are ton's of errors in the console of the browser is there anything specific I should be looking for in there?
    I'm not sure how to answer this question as I have no idea what you mean by "tons of errors". You need to look for anything that is preventing your code from running. You might want to show us the "tons".

Similar Threads

  1. Works within Netbeans, not in browser though
    By Ambergahill in forum New To Java
    Replies: 0
    Last Post: 11-27-2010, 05:46 PM
  2. Replies: 1
    Last Post: 08-19-2010, 12:47 PM
  3. Applet working in Eclipse but not in browser
    By leeloo in forum Java Applets
    Replies: 7
    Last Post: 07-05-2010, 01:24 AM
  4. Works in Eclipse but not in browser
    By panini in forum Java Applets
    Replies: 3
    Last Post: 12-14-2009, 03:34 AM
  5. Replies: 10
    Last Post: 10-11-2009, 07:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •