Results 1 to 5 of 5
  1. #1
    tmotse is offline Member
    Join Date
    Mar 2010
    Posts
    17
    Rep Power
    0

    Default Notepad Application (Swing)

    Hi Guys, i developed a notepad application with the the basic fearures: no i need it to only open .txt , .dat , .csv and .sh. i dont know how to do that: Please help guys

    Thanks

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

  3. #3
    tmotse is offline Member
    Join Date
    Mar 2010
    Posts
    17
    Rep Power
    0

    Default

    Here is my code: it opens all extensions i need to limit it to .txt , .dat , .csv and .sh.

    package com.notepad;

    Java Code:
    import java.awt.FileDialog;
    /**
     *  * @author tmotseothata
     *
     */
    public class Notepad extends Frame {
    
    	/**
    	 * @author tmotseothata
    	 * 
    	 */
    	private static final long serialVersionUID = 8462657633177163105L;
    	
    	// declaring variables
    	String filename;
    	
    	TextArea textarea;
    	
    	//enabling the data transfer
    	//the clipboard class implements the mechanism to transfer data using cut, copy and paste
    	Clipboard clip = getToolkit().getSystemClipboard();
    	
    //constructor Notepad
    	/**
    	 * 
    	 */
    	public Notepad() {
    
    		setLayout(new GridLayout(1, 1));
    		textarea = new TextArea();
    
    		add(textarea);
            
    		
    		//creating the MenuBar item
    		MenuBar menubar = new MenuBar();
    		//file menu
    		Menu fl = new Menu("file");
    		//other menu items
    		MenuItem nw = new MenuItem("New");
    		MenuItem op = new MenuItem("Open");
    		MenuItem sv = new MenuItem("Save");
    		MenuItem ex = new MenuItem("Exit");
    
    		//adding basic menu items
    		nw.addActionListener(new New());
    		fl.add(nw);
    
    		op.addActionListener(new Open());
    		fl.add(op);
    
    		sv.addActionListener(new Save());
    		fl.add(sv);
    
    		ex.addActionListener(new Exit());
    		fl.add(ex);
    
    		menubar.add(fl);
    		
    		
            
    		//edit menu
    		Menu E = new Menu("Edit");
    		
    		//menu items
    		MenuItem cut = new MenuItem("Cut");
    		MenuItem copy = new MenuItem("Copy");
    		MenuItem paste = new MenuItem("Paste");
    		
    		//adding basic menu items
    		cut.addActionListener(new Cut());
    		E.add(cut);
    		copy.addActionListener(new Copy());
    		E.add(copy);
    		paste.addActionListener(new Paste());
    		E.add(paste);
    		menubar.add(E);
    		setMenuBar(menubar);
    	
    		//creating 
    		mylistener mylist = new mylistener();
    		addWindowListener(mylist);
    
    	}
    
    	// recieving window events
    	//exit window on exit
    	public class mylistener extends WindowAdapter {
    		public void windowClosing(WindowEvent e) {
    			System.exit(0);
    		}
    	}
    	
    	// implementation of event listners
    	//defines what should be done when a user performs a certain action 
    
    	public class New implements ActionListener {
    		public void actionPerformed(ActionEvent e) {
    			textarea.setText(" ");
    			setTitle(filename);
    		}
    	}
    
    	public class Open implements ActionListener {
    		public void actionPerformed(ActionEvent e) {
    			FileDialog fd = new FileDialog(Notepad.this, "select File",
    					FileDialog.LOAD);
    			fd.setVisible(true);
    			fd.show();
    			if (fd.getFile() != null) {
    				filename = fd.getDirectory() + fd.getFile();
    				setTitle(filename);
    				ReadFile();
    			}
    			textarea.requestFocus();
    		}
    	}
    
    	public class Save implements ActionListener {
    	
    		//
    		public void actionPerformed(ActionEvent e) {
    			//display a dialog window from which the user can select a file
    			FileDialog fd = new FileDialog(Notepad.this, "Save File",
    					FileDialog.SAVE);
    			//fd.show(); 
    			fd.setVisible(true);
    			if (fd.getFile() != null) {
    				filename = fd.getDirectory() + fd.getFile();
    				setTitle(filename);
    				try {
    					DataOutputStream d = new DataOutputStream(
    							new FileOutputStream(filename));
    					String line = textarea.getText();
    					BufferedReader br = new BufferedReader(new StringReader(
    							line));
    					while ((line = br.readLine()) != null) {
    						d.writeBytes(line + "\r\n");
    						d.close();
    					}
    				} catch (Exception ex) {
    					System.out.println("File not found");
    				}
    				textarea.requestFocus();
    			}
    		}
    	}
    
    	//implementation of the exit listener: the app terminates on Exit
    	public class Exit implements ActionListener {
    		public void actionPerformed(ActionEvent e) {
    			System.exit(0);
    		}
    	}
    	
    //
    	public void ReadFile() {
    		BufferedReader d;
    		StringBuffer sb = new StringBuffer();
    		try {
    			d = new BufferedReader(new FileReader(filename));
    			String line;
    			while ((line = d.readLine()) != null)
    				sb.append(line + "\n");
    			textarea.setText(sb.toString());
    			d.close();
    		} catch (FileNotFoundException fe) {
    			System.out.println("File not Found");
    		} catch (IOException ioe) {
    		}
    	}
    	
    	 public class Cut implements ActionListener
         {
         public void actionPerformed(ActionEvent e)
             {
             String sel = textarea.getSelectedText();
             StringSelection ss = new StringSelection(sel);
             clip.setContents(ss,ss);
             textarea.replaceRange(" ",textarea.getSelectionStart(),textarea.getSelectionEnd());
         }
     }
     
     public class Copy implements ActionListener
         {
         public void actionPerformed(ActionEvent e)
             {
             String sel = textarea.getSelectedText();
             StringSelection clipString = new StringSelection(sel);
             clip.setContents(clipString,clipString);
         }
     }
     
     public class Paste implements ActionListener
         {
         public void actionPerformed(ActionEvent e)
             {
             Transferable cliptran = clip.getContents(Notepad.this);
             try
                 {
                 String sel = (String) cliptran.getTransferData(DataFlavor.stringFlavor);
                 textarea.replaceRange(sel,textarea.getSelectionStart(),textarea.getSelectionEnd());
             }
             catch(Exception exc)
                 {
                 System.out.println("not string flavour");
             }
         }
     }
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		Frame f = new Notepad();
    		f.setSize(500, 400);
    		f.setVisible(true);
    		//f.show();
    		
    
    	}
    
    }
    Last edited by Eranga; 03-30-2010 at 12:52 PM. Reason: added code tags

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default

    You know I have posted a link to the tutorial. If you're not bothered to read it, there's no way helping you.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  5. #5
    tmotse is offline Member
    Join Date
    Mar 2010
    Posts
    17
    Rep Power
    0

Similar Threads

  1. Existing swing application
    By macason in forum AWT / Swing
    Replies: 3
    Last Post: 11-30-2009, 11:51 PM
  2. Better Swing Application Framework
    By etf in forum Java Software
    Replies: 1
    Last Post: 09-07-2009, 03:25 PM
  3. deployment of swing application
    By makpandian in forum AWT / Swing
    Replies: 1
    Last Post: 12-26-2008, 05:08 PM
  4. Swing Application Framework
    By jurka in forum AWT / Swing
    Replies: 0
    Last Post: 10-09-2008, 07:01 PM
  5. Creating a notepad application with java
    By Daniel in forum AWT / Swing
    Replies: 2
    Last Post: 07-02-2007, 06:18 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •