Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2006

Tip: Looking for answers? Try searching our database.

revise my code

Thread view: 
matrix - 03 Nov 2006 16:07 GMT
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();
       
       
   
    }
    }
}
Andrew Thompson - 03 Nov 2006 16:21 GMT
...
>     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 - 04 Nov 2006 03:48 GMT
..and stop sending me email.

Andrew T.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.