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
Printable View
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
Here is my code: it opens all extensions i need to limit it to .txt , .dat , .csv and .sh.
package com.notepad;
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();
}
}
You know I have posted a link to the tutorial. If you're not bothered to read it, there's no way helping you.
got it Thanks