Results 1 to 4 of 4

Thread: File Doesn't work

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2006
    Posts
    24

    File Doesn't work

    Code:
    import java.io.FileOutputStream;
    import java.io.FileInputStream;
    import java.io.ObjectOutputStream;
    import java.io.ObjectInputStream;
    import java.io.*;
    import dat;
    import java.awt.*;
    import java.util.*;
    import java.awt.event.*;
    
    
    
    
    public class LinkedList1 extends Frame implements ActionListener, java.io.Serializable 
    {
    	
    
    	
    	
    	
    	Button bAdd;
    	Button bDel;
    	Button bFind;
    	TextField tName=new TextField(40);
    	TextField tId=new TextField(40);
    	TextField tNum=new TextField(40);
    	
    		FileOutputStream outFile;
    		ObjectOutputStream out;
    		FileInputStream inFile;
    		ObjectInputStream in;
    	
    	
    	public static void main(String s[])
    	{
    		LinkedList1 l1=new LinkedList1();
    		l1.setSize(300,400);
    		l1.setVisible(true);
    	}
    	
    	public void LinkedList1() 
    	{
    		
    		bAdd=new Button("Add");
    		bDel=new Button("Delete");
    		bFind=new Button("Find");
    		
    		setLayout(new BorderLayout());
    		
    		Panel p;
    		
    		p=new Panel();
    		p.add(bAdd);
    		p.add(bDel);
    		p.add(bFind);
    		add("North",p);
    		
    		p=new Panel();
    		p.setLayout(new GridLayout(3,2));
    		p.add(new Label("Name:"));
    		p.add(tName);
    		p.add(new Label("Email ID:"));
    		p.add(tId);
    		p.add(new Label("Phone Number:"));
    		p.add(tNum);
    		add(p);
    		
    		bAdd.addActionListener(this);
    		bDel.addActionListener(this);
    		bFind.addActionListener(this);
    		
    		try
    		{
    			
    			outFile= new FileOutputStream("file.dat");
    			out = new ObjectOutputStream(outFile);
    			inFile= new FileInputStream("file.dat");
    			in = new ObjectInputStream(inFile);
    		}
    		catch(FileNotFoundException f)
    		{
    			System.out.println("erro");
    		}
    		catch(IOException io)
    		{
    			System.out.println("erro");
    		}
    		
    		
    		
    		
    		
    	}
    	
    	public void actionPerformed(ActionEvent e) 
    	{
    		if(e.getSource()==bAdd)
    		{
    			System.out.println("Test1");
    			//dat d=new dat("abhi","adgf","aiegfiyaf");
    		//	out.writeObject(d);
    		}
    		else if(e.getSource()==bFind)
    		{
    		 	String sn;
    		 	sn=tName.getText();
    		 	dat dd;
    		 	try
    		 	{
    		 	
    		 	while(in.readObject()!=null)
    		 	{
    		 		dd= (dat) in.readObject();
    		 		if(dd.name==sn)
    		 		{
    		 			tId.setText(dd.Id);
    		 		}
    
    		 	}	
    		 	}
    		 	catch(OptionalDataException ode)
    			{
    			System.out.println("erro");
    			}
    			catch(ClassNotFoundException ode)
    			{
    			System.out.println("erro");
    			}
    			catch(IOException io)
    			{
    			System.out.println("erro");
    			}
    		}
    		
    	}
    }




    Please ignore the name "linkedlist1". This file compiles easily but when i try to run it the frame doesn't show the panels it is supposed to. Please help

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: File Doesn't work

    A constructor is not a 'void' method, it has no return type. Another thing, use JFrame if possible.

    And BTW Frame implements the Serializable interface, no need to redo that
    Code:
    package testing;
    
    import java.awt.BorderLayout;
    import java.awt.Button;
    import java.awt.Frame;
    import java.awt.GridLayout;
    import java.awt.Label;
    import java.awt.Panel;
    import java.awt.TextField;
    
    public class LinkedList extends Frame
    {
    	private static final long serialVersionUID = 6790822955604554899L;
    
    	public static void main(final String s[])
    	{
    		final LinkedList l1 = new LinkedList();
    		l1.setSize(300, 400);
    		l1.setVisible(true);
    	}
    
    	private Button bAdd;
    	private Button bDel;
    	private Button bFind;
    
    	 private TextField tName = new TextField(40);
    	 private TextField tId = new TextField(40);
    	 private TextField tNum = new TextField(40);
    
    	public LinkedList()
    	{
    		super();
    		bAdd = new Button("Add");
    		bDel = new Button("Delete");
    		bFind = new Button("Find");
    
    		setLayout(new BorderLayout());
    		Panel p;
    
    		p = new Panel();
    		p.add(bAdd);
    		p.add(bDel);
    		p.add(bFind);
    		add("North", p);
    		pack();
    
    		 p = new Panel();
    		 p.setLayout(new GridLayout(3, 2));
    		 p.add(new Label("Name:"));
    		 p.add(tName);
    		 p.add(new Label("Email ID:"));
    		 p.add(tId);
    		 p.add(new Label("Phone Number:"));
    		 p.add(tNum);
    		 add(p);
    
    	}
    }
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jun 2006
    Posts
    24

    Re: File Doesn't work

    Hi,
    I fixed the constructor but even without the JFrame, isn't my application supposed to run?

  4. #4
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: File Doesn't work

    It should run, but the JFrame is better than the Frame. More user-friendly, at least the X button works with the JFrame
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width