revise my code

Discussion in 'Java' started by matrix, Nov 3, 2006.

  1. matrix

    matrix Guest

    hello!
    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");
    JLabel label2 = new JLabel("");
    JLabel label3 = new JLabel("");
    JLabel label4 = new JLabel("");
    JPanel pane = new JPanel();
    pane.setLayout(new GridLayout(5,1,10,10));
    pane.add(label);
    pane.add(label2);
    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();



    }
    }
    }
     
    matrix, Nov 3, 2006
    #1
    1. Advertising

  2. matrix wrote:
    ....
    > public void saveInFile(String s,String fileName)
    > {
    > try{

    .....
    > {
    > fw.write(lineread+"\n\r");
    >
    > }


    // untested solution to common I/O problem
    fw.flush();

    > fw.close();


    And, please change all tab's for ' ' before posting,
    as most news clients expand tabs to ridiculous
    sizes (I put 'spaces' in, to line up my change with
    your tab'd code).

    Andrew T.
     
    Andrew Thompson, Nov 3, 2006
    #2
    1. Advertising

  3. ...and stop sending me email.

    Andrew T.
     
    Andrew Thompson, Nov 4, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jcnews
    Replies:
    147
    Views:
    2,605
    stoma
    Oct 20, 2004
  2. Sean Berry

    Help a newbie revise my program

    Sean Berry, Apr 26, 2004, in forum: Python
    Replies:
    2
    Views:
    328
    Marc 'BlackJack' Rintsch
    Apr 26, 2004
  3. matrix

    revise this code

    matrix, Nov 4, 2006, in forum: Java
    Replies:
    0
    Views:
    374
    matrix
    Nov 4, 2006
  4. Ramon F Herrera

    Need to revise my JNLP file

    Ramon F Herrera, Jul 11, 2009, in forum: Java
    Replies:
    11
    Views:
    1,196
    Andrew Thompson
    Jul 13, 2009
  5. Donald Welker

    Code fragment to revise CustomSD SDDL for Event Log

    Donald Welker, Sep 27, 2005, in forum: ASP .Net Security
    Replies:
    0
    Views:
    167
    Donald Welker
    Sep 27, 2005
Loading...

Share This Page