Discussion Locked

beginner's java applet question

0 helpful
7 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
I'm trying out some simple Java applets from a couple of books. I found that whereas I can always get them to run from Textpad or applet viewer, they don't work in the browser, from an html doc. With applets that use the JApplet class, the browser doesn't find the applet at all. I read somewhere that you sometimes have to use an older applet class (Applet rather than JApplet). I tried this too and now the applet displays but only some of the functionality works. Again, these are applets that run successfully from Textpad. Can someone point me to some solutions? I can post code if you like.

7 replies

by pmdvs

Yes. As I explained I already figured that out. The trouble is also happening with the Applet class. Specifically here, the Applet loads (this is directly from a tutorial) but the "Add" functionality does not work in my browser. It works fine in theapplet viewer, so I know the code logic is ok.

// applet

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class OlderAdderApplet extends Applet implements ActionListener
{
private TextField inputOutputField; private double sum = 0;

public void init()
{
setLayout(new BorderLayout());

Panel buttonPanel = new Panel();
buttonPanel.setBackground(Color.gray);
buttonPanel.setLayout(new FlowLayout()); Button addButton = new Button("Add");
addButton.addActionListener(this);
buttonPanel.add(addButton);
Button resetButton = new Button("Reset");
resetButton.addActionListener(this);
buttonPanel.add(resetButton);
add(buttonPanel, BorderLayout.SOUTH);

Panel textPanel = new Panel();
textPanel.setBackground(Color.blue);
textPanel.setLayout(new FlowLayout());
inputOutputField = new TextField("Numbers go here.", 30);
inputOutputField.setBackground(Color.white);
textPanel.add(inputOutputField);
add(textPanel, BorderLayout.CENTER);
}

public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand().equals("Add"))
{
sum = sum +
stringToDouble(inputOutputField.getText());
inputOutputField.setText(Double.toString(sum));
}
else if (e.getActionCommand().equals("Reset"))
{ sum = 0;
inputOutputField.setText("0.0");
}
else
inputOutputField.setText("Error in adder code.");
}

private stat

by pmdvs

Here's the full code (didn't fit in my other comment)

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class OlderAdderApplet extends Applet implements ActionListener
{
private TextField inputOutputField;
private double sum = 0;

public void init()
{
setLayout(new BorderLayout());

Panel buttonPanel = new Panel();
buttonPanel.setBackground(Color.gray);
buttonPanel.setLayout(new FlowLayout());
Button addButton = new Button("Add");
addButton.addActionListener(this);
buttonPanel.add(addButton);
Button resetButton = new Button("Reset");
resetButton.addActionListener(this);
buttonPanel.add(resetButton);
add(buttonPanel, BorderLayout.SOUTH);

Panel textPanel = new Panel();
textPanel.setBackground(Color.blue);
textPanel.setLayout(new FlowLayout());
inputOutputField = new TextField("Numbers go here.", 30);
inputOutputField.setBackground(Color.white);
textPanel.add(inputOutputField);
add(textPanel, BorderLayout.CENTER);
}

public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand().equals("Add"))
{
sum = sum +
stringToDouble(inputOutputField.getText());
inputOutputField.setText(Double.toString(sum));
}
else if (e.getActionCommand().equals("Reset"))
{ sum = 0;
inputOutputField.setText("0.0");
}
else
inputOutputField.setText("Error in adder code.");
}

private static double stringToDouble(String stringObject)
{
return Double.parseDouble(stringObject.trim());
}
}

by okujava

the problem is the parseDouble method call in

private static double stringToDouble(String stringObject)
{
return Double.parseDouble(stringObject.trim());
}
}

the method doesn't exist in jdk version 1.1.X
change it to:
return new Double(stringObject.trim()).doubleValue();

hope it helps.

P.S. if u move over the applet with your mouse, u should see a message in the status bar. what does it say?

This discussion is closed to new replies.