Action Event with menu Item

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;

public class ActionListenerDemoMenuItem extends Frame implements ActionListener {
	MenuBar mb;
	Menu mn;
	MenuItem it1, it2, it3, it4;
	Panel p1;
	Button b1;
	TextArea area;

	public ActionListenerDemoMenuItem() {
		super("window");
		p1 = new Panel();
		b1 = new Button("save");
		area = new TextArea(30, 80);
		mb = new MenuBar();
		mn = new Menu("File");
		it1 = new MenuItem("FILECHOOSER");
		it2 = new MenuItem("COLOR CHOOSER");
		it3 = new MenuItem("EXIT");
		it4 = new MenuItem("save");
		add(p1);
		p1.add(area);
		p1.add(b1);
		mb.add(mn);
		mn.add(it1);
		mn.add(it2);
		mn.add(it3);
		mn.add(it4);
		setMenuBar(mb);
		show();
		setSize(400, 500);
		it1.addActionListener(this);
		it2.addActionListener(this);
		it3.addActionListener(this);
		it4.addActionListener(this);

	}

	@Override
	public void actionPerformed(ActionEvent evt) {
		JFileChooser f = new JFileChooser();
		if (evt.getSource() == it4) {
			f.showSaveDialog(p1);
		}
		if (evt.getSource() == it1) {
			area.setText("");
			try {

				f.showOpenDialog(p1);
				File f1 = f.getSelectedFile();
				RandomAccessFile f2 = new RandomAccessFile(f1, "rw");
				while (f2.getFilePointer() < f2.length())
					area.append(f2.readLine() + "\n");
				f2.close();
			} catch (Exception e) {
				System.out.println("error" + e);
			}
		}
		if (evt.getSource() == it2) {
			JColorChooser c = new JColorChooser(new Color(10, 20, 40));
			Color c1 = JColorChooser.showDialog(p1, "select color", new Color(
					20, 40, 40));
			p1.setBackground(c1);
		}
		if (evt.getSource() == it3) {
			System.exit(0);
		}
	}

	public static void main(String arg[]) {
		ActionListenerDemoMenuItem as = new ActionListenerDemoMenuItem();
	}
}

Download

Event handling in Java

Action Event with Menu Item
Action Event in Applet
Action Event
Adjustment Event
Focus Event
Item Event
Key Event
MOuse Event
Event with Timer
Window Event


Write your Comment

Name :
Email Id : [ We will not publish your email id ]
Your Comment : [ Maximum 200 chars ]
Enter Security code