Magazine
 
Tips & Tricks
 

tr.getTransferData(DataFlavor.stringFlavor);
int start=field.getSelectionStart();
int end=field.getSelectionEnd();
field.replaceRange(s,start,end);
}
catch (Exception e) {
return;
}
}
public boolean save() {
JFileChooser fileChooser = new
JFileChooser();
fileChooser.setFileFilter (javaFileFilter);
int returnVal =
fileChooser.showSaveDialog(this);
if(returnVal ==
JFileChooser.APPROVE_OPTION){
file = fileChooser.getSelectedFile ();
if (file.exists()) {
int response =
JOptionPane.showConfirmDialog (null,”File
already exists. Do you want to
continue?”,”Overwrite
Confirmation”,JOptionPane.OK_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE);
if (response ==
JOptionPane.CANCEL_OPTION) return false;
}
String fileName = file.getName();
if(fileName != null) {
int extensionIndex =
fileName.lastIndexOf(‘.’);
setTitle(fileName.substring(0,extensionIndex));
}
return writeFile (file, field.getText());
}
return false;
}
public static boolean writeFile (File file, String
content) {
try {
PrintWriter pw = new PrintWriter (new
BufferedWriter (new FileWriter (file)));
pw.print (content);
pw.flush ();
pw.close ();
}
catch (IOException e) {
return false;
}
return true;
}
public void quit() {
System.exit(0);
}
}

 

The result of the above class can be Shown Below:






Clicking the open menu item results in opening the selected file and file name appears at the top showing the current file opened.

Oct 2007 | Java Jazz Up | 70
previous
index
next
 
Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,

30
, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53 , 54, 55, 56, 57,

58
, 59, 60, 61, 62, 63 , 64, 65 , 66 , 67 , 68 , 69 , 70, 71, 72, 73, 74, 75, 76, 77, 78,   Download PDF