revise this code
PLEASE compile this code on your pc and add some new items then try to
update it using update button and we will see mu problem clearly which
is:
I have a main window ,by clicking on (add new item) , another window
open to fill the form
on clicking on (save to file),my form will be saved in text file.
if I open this text file using notepad for example,then I found my data
stored but concatenated
although I add to the saved string "\n" [hint:you will find two ## to
direct u to this line in
my code text]
if I change it to "\n\n" then my data is stored in separate lines which
I need.
the Problem that when I read the saved file by clicking on "update data
item" ,the returned
data in the form is not as saved
MY CODE:::
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class DataDictionaryMain extends JFrame implements
ActionListener
{
JButton addItem,updateItem,searchItem,exit;
public DataDictionaryMain()
{
super("Main Window");
setSize(400,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addItem = new JButton("add new item");
updateItem = new JButton("update data item");
searchItem= new JButton("search for item");
exit= new JButton ("Exit Program");
addItem.addActionListener(this);
updateItem.addActionListener(this);
searchItem.addActionListener(this);
exit.addActionListener(this);
JLabel label = new JLabel("Data Dictionary Program");
JPanel pane = new JPanel();
pane.setLayout(new GridLayout(5,1,10,10));
pane.add(label);
pane.add(addItem);
pane.add(updateItem);
pane.add(searchItem);
pane.add(exit);
setContentPane(pane);
setVisible(true);
}
public static void main (String[] args)
{
DataDictionaryMain main = new DataDictionaryMain();
}
public void actionPerformed (ActionEvent ave )
{
Object ob = ave.getSource();
if(ob == addItem )
{
Form1 f= new Form1( "form");
}
if(ob == updateItem)
{
Form1 form= new Form1( "form");
File file = form.getAFileToOpen();
form.writeFileToEditor(file);
}
if(ob == searchItem)
{
}
if(ob == exit)
{
System.exit(0);
}
}
}
public class Form1 extends JFrame implements ActionListener
{
JFileChooser filechooser;
JTextField text1 ;
JTextField text2 ;
JTextField text3 ;
JTextArea area4 ;
JButton saveFile;
public Form1 (String name)
{
super(name);
setSize(350,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
text1 = new JTextField (30);
text2 = new JTextField (30);
text3 = new JTextField (30);
area4 = new JTextArea (10,30);
saveFile = new JButton("save to file");
JLabel label1 = new JLabel ("Name");
JLabel label2 = new JLabel ("Type");
JLabel label3 = new JLabel ("Date");
JLabel label4 = new JLabel ("Description");
saveFile.addActionListener(this);
JPanel pane =new JPanel();
pane.add(label1);
pane.add(text1);
pane.add(label2);
pane.add(text2);
pane.add(label3);
pane.add(text3);
pane.add(label4);
pane.add(area4);
pane.add(saveFile);
setContentPane(pane);
area4.setLineWrap(true);
area4.setWrapStyleWord(true);
setVisible(true);
}
public static void main (String [] arguments)
{
Form1 f= new Form1( "form");
}
public void actionPerformed(ActionEvent evt )
{
Object src=evt.getSource();
if(src==saveFile )
{
/*##*/ String a =
text1.getText()+"\n"+text2.getText()+"\n"+text3.getText()+"\n"+area4.getText();
String f =text1.getText();
saveInFile(a,f);
JOptionPane.showMessageDialog(null,"The New Data Item save
correctly");
}
}
public File getAFileToOpen()
{
File file = null;
File currentdirectory = new File(".");
JFileChooser filechooser = new JFileChooser(currentdirectory);
int replycode = filechooser.showOpenDialog(null);
if (replycode == JFileChooser.APPROVE_OPTION){
file = filechooser.getSelectedFile();
}
return file;
}
public void writeFileToEditor(File file)
{
try{
FileReader filereader = new FileReader(file);
BufferedReader bufferedreader = new BufferedReader(filereader);
String lineread = "";
text1.setText(bufferedreader.readLine());
text2.setText(bufferedreader.readLine());
text3.setText(bufferedreader.readLine());
while ((lineread = bufferedreader.readLine()) != null){
area4.setText(lineread + "\n");
}
filereader.close();
}catch (FileNotFoundException fnfe){
System.err.println("FileNotFoundException: " + fnfe.getMessage());
}catch (IOException ioe){
System.err.println("IOException: " + ioe.getMessage());
}
}
public void saveInFile(String s,String fileName)
{
try{
File f=new File(fileName+".txt");;
FileWriter fw = new FileWriter(f);
StringReader sr = new StringReader(s);
BufferedReader br = new BufferedReader(sr);
String lineread = "";
while((lineread=br.readLine()) != null )
{
fw.write(lineread+"\n\r");
}
fw.close();
}
catch(IOException io){
System.err.println();
}
}
}