wood burning stoves 2.0*
The moose likes Applets and the fly likes Button not working in applet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "Button not working in applet" Watch "Button not working in applet" New topic
Author

Button not working in applet

LaTeef Lusk
Greenhorn

Joined: Mar 28, 2009
Posts: 15
I have my event target code in place but when I run the applet the button does nothing. Can someone point me in the right direction?

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

public class Temperature extends java.applet.Applet
{
public static void main(String[] args) {

new Temperature().init();

}

private TextField fahrenheitInput;
private TextField celsiusInput;
private Button convert;

public void init()
{
setBackground(Color.blue); // Set background color to blue
setForeground(Color.white); // Set foreground color to white

setLayout(new FlowLayout()); // Set Layout Style


convert = new Button ("Convert My Temperature");
fahrenheitInput = new TextField();
celsiusInput = new TextField();

Panel first_panel;
Panel second_panel;
Panel third_panel;

first_panel = new Panel();
first_panel.setLayout(new FlowLayout());
second_panel = new Panel();
second_panel.setLayout(new FlowLayout());
third_panel = new Panel();
third_panel.setLayout(new FlowLayout());


first_panel.add(new Label("Enter the temperature in Fahrenheit:"));
first_panel.add(fahrenheitInput);

second_panel.add(convert);

third_panel.add(new Label("Here is the converted temperature in Celsius:"));
third_panel.add(celsiusInput);

// Add the panels to the applet.
add(first_panel);
add(second_panel);
add(third_panel);
}

public boolean action (Event evt, Object o)
{
// the event target once the button is clicked
if (evt.target == convert)
{
// Convert the temp and assign it to the celsius textfield
int f = Integer.parseInt(fahrenheitInput.getText());
int c = 5 * (f-32) / 9;
String newCelsius = Integer.toString(c);
celsiusInput.setText(newCelsius);

// Event handled
return true;
}
else
return false;
}

}

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41064
    
  43
You need to add "this" as an action listener to the button. The class also needs to "implement ActionListener".

I'm assuming that this is for testing the GUI only, and that you know that applets should have neither a main method nor a constructor.


Ping & DNS - my free Android networking tools app
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37926
    
  22
Probably best discussed in the applets forum. Moving.
LaTeef Lusk
Greenhorn

Joined: Mar 28, 2009
Posts: 15
Adding in the action listener I am getting an error i dont understand.

c:\Temperature.java:5: Temperature is not abstract and does not override abstract method actionPerformed(java.awt.event.ActionEvent) in java.awt.event.ActionLis
tener
public class Temperature extends java.applet.Applet implements ActionListener
^

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

public class Temperature extends java.applet.Applet implements ActionListener
{

private TextField fahrenheitInput;
private TextField celsiusInput;
private Button convert;

public void init()
{
setBackground(Color.blue); // Set background color to blue
setForeground(Color.white); // Set foreground color to white

setLayout(new FlowLayout()); // Set Layout Style


convert = new Button ("Convert My Temperature");
convert.addActionListener(this);
fahrenheitInput = new TextField();
celsiusInput = new TextField();

Panel first_panel;
Panel second_panel;
Panel third_panel;

first_panel = new Panel();
first_panel.setLayout(new FlowLayout());
second_panel = new Panel();
second_panel.setLayout(new FlowLayout());
third_panel = new Panel();
third_panel.setLayout(new FlowLayout());


first_panel.add(new Label("Enter the temperature in Fahrenheit:"));
first_panel.add(fahrenheitInput);

second_panel.add(convert);

third_panel.add(new Label("Here is the converted temperature in Celsius:"));
third_panel.add(celsiusInput);

// Add the panels to the applet.
add(first_panel);
add(second_panel);
add(third_panel);
}

public boolean action (Event evt, Object o)
{
// the event target once the button is clicked
if (evt.target == convert)
{
// Convert the temp and assign it to the celsius textfield
int f = Integer.parseInt(fahrenheitInput.getText());
int c = 5 * (f-32) / 9;
String newCelsius = Integer.toString(c);
celsiusInput.setText(newCelsius);

// Event handled
return true;
}
else
return false;
}

}

clara irma
Greenhorn

Joined: May 06, 2009
Posts: 5
Hi,

when you implement ActionListener interface, you need to write the implementation of actionPerformed(ActionEvent e) method (see java documentation).

So your applet class should have that method's implementation, and you can put your button-clicked handler there.


♪♪♪ イルマ ♪♪♪
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41064
    
  43
Yes, the "public boolean action (Event evt, Object o)" method has been deprecated for more than 10 years; it should not be used. That's what ActionListeners are for.

You may want to read http://java.sun.com/docs/books/tutorial/uiswing/events/index.html about that, and also http://java.sun.com/docs/books/tutorial/uiswing/index.html about Swing GUIs in general. AWT GUIs (like you're using) have been out of favor for a long time, and are less functional than Swing GUIs to begin with.
ethaniel landfough
Greenhorn

Joined: Aug 16, 2009
Posts: 21
hi, i'm trying to make a game with java applet. My problem is i need to make a "New Button". I already made the code but it isn't working (doesn't load the other class applet). Here is my source code :

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

public class coba extends Applet
{
Button button1;

public void init()
{
Font font = new Font("TimesRoman",Font.BOLD,16);
setFont(font);

button1 = new Button("Level Easy");
resize(250,250);
LevelEasy lvleasy = new LevelEasy();
button1.addActionListener(lvleasy);
//button1.addActionListener(this);
add("Left",button1);

}
private class LevelEasy implements ActionListener
{

public void actionPerformed(ActionEvent e)
{
coba4 c4 = new coba4(); --> the class name i'm trying to call/ load in the applet. (coba4.class)
}
}

}

PS : I already place the the "coba4.class" in the same folder. Help me.....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Button not working in applet
 
Similar Threads
Incompatiable variable types
Need HELP Temperature Conversion
Java Applet to Stand alone application
Getting "Type expected" when trying to compile applet
non-static method init() cannot be referenced from a static context