Results 1 to 5 of 5
Thread: Notepad Application (Swing)
- 03-30-2010, 08:27 AM #1
Member
- Join Date
- Mar 2010
- Posts
- 17
- Rep Power
- 0
- 03-30-2010, 08:30 AM #2
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 03-30-2010, 08:53 AM #3
Member
- Join Date
- Mar 2010
- Posts
- 17
- Rep Power
- 0
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 11:52 AM. Reason: added code tags
- 03-30-2010, 09:19 AM #4
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
- 03-30-2010, 10:24 AM #5
Member
- Join Date
- Mar 2010
- Posts
- 17
- Rep Power
- 0
Similar Threads
-
Existing swing application
By macason in forum AWT / SwingReplies: 3Last Post: 11-30-2009, 10:51 PM -
Better Swing Application Framework
By etf in forum Java SoftwareReplies: 1Last Post: 09-07-2009, 02:25 PM -
deployment of swing application
By makpandian in forum AWT / SwingReplies: 1Last Post: 12-26-2008, 04:08 PM -
Swing Application Framework
By jurka in forum AWT / SwingReplies: 0Last Post: 10-09-2008, 06:01 PM -
Creating a notepad application with java
By Daniel in forum AWT / SwingReplies: 2Last Post: 07-02-2007, 05:18 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks