Advertisements
Home > Java > Java Program to write an Applet which handles user input and show output – Q54

Java Program to write an Applet which handles user input and show output – Q54


Q54: Java Program to write an applet, which shows one choice item and one list item. Add some items in both. Whenever user selects an item that message should be displayed on the screen.

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

public class Ap_ListDemo extends Applet implements ActionListener {
	
	List os, browser;
	String msg = "";
	
	public void init(){
		os = new List(4,true);
		
		browser = new List(4,false);
		
		os.add("Windows 98/Xp");
		os.add("Solaris");
		os.add("Macos");
		
		browser.add("netscape 3.x");
		browser.add("netscape 4.x");
		browser.add("netscape 5.x");
		browser.add("netscape 6.x");
		browser.add("internet explorer 4.0");
		browser.add("internet explorer 5.0");
		browser.add("internet explorer 6.0");
		browser.add("Lynx 2.4");
		
		browser.select(1);
		
		add(os);
		add(browser);
		
		os.addActionListener(this);
		browser.addActionListener(this);
	}
	
	public void actionPerformed(ActionEvent ae){
		repaint();
	}
	
	public void paint(Graphics g){
		int idx[];
		
		msg = "current OS";
		idx = os.getSelectedIndexes();
		for(int i = 0; i < idx.length; i++)
			msg += os.getItem(idx[i]) + " ";
		
		g.drawString(msg,6,120);
		msg = "current Browser: ";
		msg += browser.getSelectedItem();
		g.drawString(msg,6,140);
	}
}

… from College notes (BCA/MCA assignments):


Advertisements
Categories: Java Tags:
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: