Hello,

I'm trying to create an application that when you click on a button, it just displays a string but it doesnt work. Here's the code:

import java.io.*;
import java.awt.*;
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.event.*;

public class main extends Applet {

  TextField input;
  Button convert;
  
 
  public void init () {
   
     // Construct the TextFields
     input = new TextField(10);
     convert = new Button ("Convert");
     
     //Button b = new Button("Submit");

     
     add(input);
     add(convert);
     convert.addActionListener(null);
 
     
  }
  
  public void actionPerformed(ActionEvent evt)  
  { 
	  if (evt.getSource() == convert)
	  {
		  paint();
	
	  }
	  
  }
  
  public void paint(Graphics g)
  {
  	g.drawString ("Hello", getWidth()/2, getHeight()/2);
  	
  	
  }
}

Any ideas? Thanks

You had convert.addActionListener(null); but there's no ActionListener because the ActionListener you asigned to convert is nothing, null.
Put 'implements ActionListener' after extends Applet and put 'this' in convert.addActionListener.

Member Avatar for ztini

Implementing ActionListener and doing addActionListener(this) is not very good form and limits you only to the one actionPerformed() method in the class, e.g. zero scalability. The best way to do this is either 1) create a separate class that extends ActionListener and add a new instantiation of that class or 2) use an inner class.

Here is a simple example of the later:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;


public class ButtonClickApplet extends JApplet {

	private JLabel label = new JLabel();
	
	public void init() { 
		setLayout(new BorderLayout());
		
		JButton button = new JButton("Click Me");
		button.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				label.setText("Clicked Button");
			}
		});
		
		add(button, BorderLayout.SOUTH);
		add(label, BorderLayout.CENTER);
	}	
	
	public void destroy() { }
	public void start() { }
	public void stop() { }
}

You can clearly see that if I wanted to add another button that changed the text to something other than "Clicked Button" how you could easily and rapidly implement this. If you do what Mango suggested the previous post, you would have to add some logic overhead to your actionPerformed method....if button = this, then text = xxx, else text = yyy. What happens when you add 10 buttons? Gets ugly quick. You should almost never implement a listener on a container for this reason.

@ Phil++

never, never use

public void paint(Graphics g)

but

public void paintComponent(Graphics g)

sure better is following with hided hints posted by ztini

mKorbel, note that Phil++ was using Applet, not JApplet. Your advice is correct for JApplet, but in Applet paint() is the appropriate method to override.

@ Ezzaral

you are right, sorry for that, but as I read last posts on two pages ...,

how and from where this code coming from, yes AWT still exist but for another reasons

are you somebody to explain me ***..., for why todays posters using code from last milenium, who's so very very stupid ... and allows to learning this ...,

well now I maybe understood posters which mixing AWT with Swing, eeerg

No, I couldn't really offer any reason why they would want to use the older AWT Applet - only that he was using it.

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.