Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

jdialog class

Status
Not open for further replies.

mgl70

Programmer
Sep 10, 2003
105
US
Hi ,

I have a question regarding JDialog classes.
I have 2 classes that extend JDialog class.
I am creating a empty randomaccessfile when I use Dialog1 class.I close the dialog1 class . Next I need to call the randomaccessfile from dialog2 class.
for this I wrote a getFile() method in dialog1 class.
when I call that method from dialog2 class, I am getting null instead of randomaccessfile.

dialog1 class
{
public RandomAccessFile getFile()
{
return file;
}
}
---------------------------------
dialog2 class
{

Dialog1 d1 = new Dialog1();
RandomAccessFile file = d1.getFile();
//here I need to get the file since I need to write data in the file. but I am getting null here. what should I do?

}
Canot I get any info once I closed the dialog.
I appreciate your help.

thanks,
 
// create a file for testing, for opening and writing output
import java.io.RandomAccessFile;
import java.io.IOException;
import javax.swing.*;
import java.awt.*;
class DialogDemo extends JFrame
{
public DialogDemo()
{
super("Frame1");
}
public static void main(String args[])
{
DialogDemo ddObj = new DialogDemo();
ddObj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ddObj.setSize(400,400);
ddObj.setVisible(true);
FileDialog d1 = new FileDialog(ddObj);
d1.show();
if (d1.getFile()!=null)
{
System.out.println( d1.getDirectory()+d1.getFile() );
}
RandomAccessFile rf;
try
{
rf = new RandomAccessFile(d1.getDirectory()+d1.getFile(), "rw");
rf.seek(rf.length());
rf.writeUTF("last line"+"\r");
rf.close();
}
catch (IOException ie)
{System.out.println("error in opening file or writing file!");}
//RandomAccessFile file = d1.getFile();
//here I need to get the file since I need to write data in the file. but I am getting null here. what should I do?
}

}
 
// if case for checking correctness in selecting input file
RandomAccessFile rf;
if (d1.getFile()!=null)
{
try
{
rf = new RandomAccessFile(d1.getDirectory()+d1.getFile(), "rw");
rf.seek(rf.length());
rf.writeUTF("last line"+"\r");
rf.close();
}
catch (IOException ie)
{System.out.println("error in opening file or writing file!");}
//RandomAccessFile file = d1.getFile();
//here I need to get the file since I need to write data in the file. but I am getting null here. what should I do?
}// endif
 
Thank you very much.but my problem is I am not getting file. if I get file I can able to write.


But my question was . I have 2 java classes dialog1,dialog2.
Somebody wrote these class.I did not write those.

dialog1 extends JDialog.
constructor: d1 = new dialog1(panel1);


dialog2 extends JDialog.
constructor: d2 = new dialog1(panel1);

In the dialog1, I am creating a randomaccessfile.close the dialog.
Next I open dialog2. Here I need to get the randomaccessfile
from dialog1 class.
In dialog1 class ,I wrote a method getFile().

In dialog2 class, I want to get the file because I need to write some info to that file.
In dialog2 . I gave like this.
file = d1.getfile();

here ,I should get file. Then I can write into any thing to that file.
But "file" variable(dialog2 class) is giving "null" because
It is not getting file from dialog1 class.
what is my problem?

I appreciate your help.





 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top