Create a JAVA applet program that accepts and displays object‟s details on respective GUI components. The Applet consists of seven GUI components such as two labels Name and Price followed by two textfields of size 10 respectively. There are two buttons labeled as ADD and DISPLAY followed by a Textarea. The values entered by the user on the text fields are that of two instance variables of a class with name Fruit and the individual object details are to be stored on an array of type Fruit upon the pressing of ADD button. Users are allowed to add 5 object‟s details and while pressing the DISPLAY button, the values of all the five objects are displaying on the Text Area.
Note: Fruit class should be created with two instance variables and a constructor to initialize the values. Use comments to illustrate the various concepts applied / utilized in the solution.

Recommended Answers

All 6 Replies

That's your assignment. What's your question?

I do hope you weren't expecting someone to do your homework for you.

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

public class Fruit extends Applet implements ActionListener
{
	String Fname;
	int Fprice;
	public Fruit(String x,int y)
	{
		Fname=x;
		Fprice=y;
	}
	String m="";
	int i;
	Label name,price;
	TextField N,P;
	Button ADD,DISPLAY;
	TextArea Di;
	 Fruit A[];
	public void init()
	 {
	 	A=new Fruit[5];
	 	name=new Label("Name");
	 	N=new TextField(10);
	 	price=new Label("Price");
		P=new TextField(10);
	 	ADD=new Button("Add");
	    DISPLAY=new Button("Display");
	 	Di=new TextArea("",5,40);
	 	 add(name);
	 	 add(N);
	 	 add(price);
	 	 add(P);
	 	 ADD.addActionListener(this);
	 	 add(ADD);
	 	 DISPLAY.addActionListener(this);
	 	 add(DISPLAY);
	 	 add(Di);
	 	 
	 }
	 public void actionPerformed(ActionEvent e)
	 {
	
	 	for(i=0;i<5;i++)
	 	{
	 		if(e.getSource()==ADD)
	 		{
	 			N.setText("");
	 			P.setText("");
	 			String z=N.getText();
	 			int d=Integer.parseInt(P.getText());
	 			A[i]=new Fruit(z,d);
	 			m=(""+z+d);
	 		}
	 	if(e.getSource()==DISPLAY)
	 		{
	 		
	 			Di.setText(""+m);
	 		}
	 	}
      }
}


Is this true .. And what is missing

And what is your problem ?
One thing for sure

for(i=0;i<5;i++)	 	{
	 if(e.getSource()==ADD)

no need to check 5 times that ADD is the JComponent that generated the Event

And what is your problem ?
One thing for sure

for(i=0;i<5;i++)	 	{
	 if(e.getSource()==ADD)

no need to check 5 times that ADD is the JComponent that generated the Event

Well ..
The first thing .. I do not think that the program which I do it correctly

I don't know how to work with constructor in this program

No one has the answer to the problem???

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.