PDA

Click to See Complete Forum and Search --> : File Doesn't work


abhs_94
Oct 27th, 2008, 09:04 AM
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

ComputerJy
Oct 27th, 2008, 04:57 PM
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
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);

}
}

abhs_94
Oct 28th, 2008, 12:36 AM
Hi,
I fixed the constructor but even without the JFrame, isn't my application supposed to run?

ComputerJy
Oct 28th, 2008, 02:07 AM
It should run, but the JFrame is better than the Frame. More user-friendly, at least the X button works with the JFrame :)