VB icon

Editor in Java

Email
Submitted on: 1/4/2015 12:04:00 PM
By: SHRIDHAR VISHWAS PURANDARE (from psc cd)  
Level: Beginner
User Rating: By 5 Users
Compatibility: Java (JDK 1.5)
Views: 1911
 
     Learn basics of file handling,JFileChooser swing utility and now u have simple!!!!! editor Please rate me as it is my first attempt An assignment in my college
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Editor in Java
// Description:Learn basics of file handling,JFileChooser swing utility and now u have simple!!!!! editor
Please rate me as it is my first attempt
An assignment in my college
// By: SHRIDHAR VISHWAS PURANDARE (from psc cd)
//
// Assumes:it is basic code for editor with UI and u can go through and learn.
then u can add anything to make it a mini project in second year
//**************************************

/*
*
*@AUTHOR -- SHRIDHAR PURANDARE
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.filechooser.*;
import java.io.*;
public class win extends Frame implements WindowListener
{			
			public Frame ob1;
			public MenuBar ob2;
			public Menu file;
			public MenuItem New;
			public MenuItem Open;
			public MenuItem Save;
			public TextArea text;
			BufferedReader inputStream;
			
			public File filename;
			public FileWriter outputStream;
			
		public win()
		{
			ob1=new Frame("Darshan");
			ob1.setLayout(new BorderLayout(10,20));
			ob1.resize(200,200);
			ob2=new MenuBar();
			ob1.setMenuBar(ob2);
			file=new Menu("File");
			New=new MenuItem("New");
			Open=new MenuItem("Open");
			Save=new MenuItem("Save");
			file.add(New);
			New.addActionListener(new ActionListener()
			{
				public void actionPerformed(ActionEvent event)
				{
					int ask = JOptionPane.showConfirmDialog(win.this,"Are you sure? This will lose any unsaved documents!");
					if (ask ==JOptionPane.YES_OPTION) 
						{
 							text.setText("");
						}
				setTitle("dar");
				}
			});// end actionlistener of menuitem New
			file.insertSeparator(1);
			file.add(Open);
			Open.addActionListener(new ActionListener()
			{
				public void actionPerformed(ActionEvent event)
					{
						openfile();
					}
			});
			file.insertSeparator(3);
			file.add(Save);
			Save.addActionListener(new ActionListener()
			{
				public void actionPerformed(ActionEvent event)
				{
					saveFile();
				}
			});
			text=new TextArea();
			ob1.add(text);
			ob2.add(file);
			ob1.addWindowListener(this);
			ob1.show();
		}
	public static void main(String[] args) 
	{
		//System.out.println("Hello World!");
		win ob3=new win();
		 
		
		
	}
	 public void windowClosing(WindowEvent e) {
		System.exit(0);
	}
	public void windowActivated(WindowEvent e) {}
	public void windowDeactivated(WindowEvent e) {}
	public void windowDeiconified(WindowEvent e) {}
	public void windowIconified(WindowEvent e) {}
	public void windowOpened(WindowEvent e) {}
	public void windowClosed(WindowEvent e) {}
	private void openfile()
{
	
	JFileChooser filechooser=new JFileChooser();
	
	filechooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
	
	int result=filechooser.showOpenDialog(this);
	
	if(result==JFileChooser.CANCEL_OPTION)
	return;
	
	
	filename=filechooser.getSelectedFile();
	
	if(filename==null||filename.getName().equals(""))
JOptionPane.showMessageDialog(this,"invalid filename" ,"invalid filename",JOptionPane.ERROR_MESSAGE);
	
try {
 inputStream = new BufferedReader(new FileReader(filename.getPath()));
 String inputLine;
 text.setText("");
 setTitle("Darshan Notepad-"+filename.getName());
 while((inputLine = inputStream.readLine()) != null) {
 text.append(inputLine+"\n");
 }
}
catch(FileNotFoundException ioe) {
 JOptionPane.showMessageDialog(null, "Sorry,File Not Found", "", JOptionPane.WARNING_MESSAGE );
}
catch(IOException ioe) {
 System.out.println(filename.getAbsoluteFile());
}
	
}
		private void saveFile()
		{
			JFileChooser ob=new JFileChooser();
			ob.setFileSelectionMode(JFileChooser.FILES_ONLY);
			int flag=ob.showSaveDialog(this);
			if(flag==JFileChooser.CANCEL_OPTION)
				return;
			filename=ob.getSelectedFile();
			if(filename==null|filename.getName().equals(""))
				JOptionPane.showMessageDialog(null,"Invalid filename","Invalid filename",JOptionPane.ERROR_MESSAGE);
			try
			{
				 outputStream=new FileWriter(filename.getPath());				
 setTitle("darshan Notepad-"+filename.getName());
 outputStream.write(text.getText());
 outputStream.close();
}
catch(IOException ioe) {
System.out.println("IOException");
}
 
	
}
}


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.