Pass a file array to other class

Hello,

I'm new in Java.

I'm trying to pass a file array to other class where I create the array. But I don't see how?

I'm using netbeans 5.0 to create a JfileChooser and get the files selected.

Thanks

privatevoid jMenuItemImportarActionPerformed(java.awt.event.ActionEvent evt){

// Here I'm calling the JFlieChooser:

new ImportaArchivoJDialog(this,true).addWindowFocusListener(new java.awt.event.WindowFocusListener(){

publicvoid windowGainedFocus(WindowEvent windowEvent){

}

publicvoid windowLostFocus(WindowEvent windowEvent){

CargaArchivo();// In this method I need the file array

}

});

}

privatevoid ImportaArchivojFileChooserActionPerformed(java.awt.event.ActionEvent evt){

boolean bLeido;

fArchivo=ImportaArchivojFileChooser.getSelectedFiles();

this.setVisible(false);

}

[1676 byte] By [kaminante11a] at [2007-11-27 3:22:58]
# 1
My guess:CargaArchivo(fArchivo);
DrClapa at 2007-7-12 8:25:43 > top of Java-index,Java Essentials,Java Programming...
# 2

I don't understand because 2 methods are in different class:

public class A{

//............................

private void jMenuItemImportarActionPerformed(java.awt.event.ActionEvent evt) {

// Here I'm calling the JFileChooser:

new ImportaArchivoJDialog(this,true).addWindowFocusListener(new java.awt.event.WindowFocusListener() {

public void windowGainedFocus(WindowEvent windowEvent) {

}

public void windowLostFocus(WindowEvent windowEvent) {

CargaArchivo(fArchivo); // In this method I need the file array, but

// fArchivo is not in this class

}

});

}

//.................

}

public class ImportaArchivoJDialog extends javax.swing.JDialog {

//...........................

private void ImportaArchivojFileChooserActionPerformed(java.awt.event.ActionEvent evt)

fArchivo=ImportaArchivojFileChooser.getSelectedFiles();

this.setVisible(false);

}

//..........

}

so, how pass

kaminante11a at 2007-7-12 8:25:43 > top of Java-index,Java Essentials,Java Programming...
# 3
Is there an instance of the class, that has the CargaArchivo(fArchivo) method, in the class A ?could you post the signature of this method ?
java_2006a at 2007-7-12 8:25:43 > top of Java-index,Java Essentials,Java Programming...
# 4

public class A{

/* ... */

private void jMenuItemImportarActionPerformed(java.awt.event.ActionEvent evt) {

// Here I'm calling the JFileChooser:

// Okay fine ... is this where you build the File array?

new ImportaArchivoJDialog(this,true).addWindowFocusListener(new java.awt.event.WindowFocusListener() {

public void windowGainedFocus(WindowEvent windowEvent) {

/* ... */

}

public void windowLostFocus(WindowEvent windowEvent) {

CargaArchivo(fArchivo); // In this method I need the file array, but

// fArchivo is not in this class

// So I ask you ... what is this File array called?

}

});

}

}

abillconsla at 2007-7-12 8:25:43 > top of Java-index,Java Essentials,Java Programming...
# 5
I create the file array in the class ImportaArchivoJDialog (an instance of this class) when user select the files in a Jfilechooser. But I need read the files (to use StringTokenizer) in other class named A.
kaminante11a at 2007-7-12 8:25:43 > top of Java-index,Java Essentials,Java Programming...
# 6

the method CargaArchivo (in class A) will use StringTokenizer to read the files of this array, but the array was created in the class ImportaArchivoJDialog (an instance of this).

public void CargaArchivo(File ArchivosACargar[]){

FileReader frLeeArchivo;

int indice;

try{

for (int i=0;i<=fArchivos.length;i++){

frLeeArchivo= new FileReader(fArchivos[i]);

String cadenaCompleta = new String();

do{

indice=frLeeArchivo.read();

if (indice!=-1) {

cadenaCompleta=cadenaCompleta+(char)indice;

}

} while (indice!=-1);

System.out.println("cadena completa es: "+cadenaCompleta);

StringTokenizer stCampo=new StringTokenizer(cadenaCompleta,"\t\r\n");

//........ other test

}

kaminante11a at 2007-7-12 8:25:43 > top of Java-index,Java Essentials,Java Programming...
# 7

Use the Singleton pattern like the following :

public class A{

private File [] fileArray;

public static A instance = null;

private A(){}

public static synchronized A getInstance(){

return (instance==null)?new A():instance;

}

public setFileArray(File[] fileArray){

this.fileArray=fileArray;

}

}

In the ImportaArchivoJDialog class, when you create the file array, call this:

A.getInstance().setFileArray(your_file_array);

java_2006a at 2007-7-12 8:25:43 > top of Java-index,Java Essentials,Java Programming...
# 8
thanks, i'll try it
kaminante11a at 2007-7-12 8:25:43 > top of Java-index,Java Essentials,Java Programming...
# 9

Nooo... I can't use singleton

There are many errors, and I want to know PLEASE...

?there are other form to give a variable to parent class? I don't use hierarchy, because one class is a frame (principal frame of the application) and the other class is a jDialog. The problem is this: JDialog create a file array and I need that Jframe use it.

Thanks a lot

kaminante11a at 2007-7-12 8:25:43 > top of Java-index,Java Essentials,Java Programming...
# 10

In this case, when you create a JDialog, pass it the JFrame as a parameter, then, when you create the file array in the dialog, set this array to the frame

variable of the JDialog. Here an example :

MyFrame class

public class MyFrame extends JFrame{

private File[] fileArray;

public void setFileArray(File[] fileArray){

this.fileArray=fileArray;

}

public File[] getFileArray(){

return this.fileArray;

}

}

MyDialog

public class MyDialog extends JDialog{

private MyFrame myFrame;

public MyDialog(MyFrame myFrame){

this.myFrame = myFrame;

}

void selectFileArray(){ //here the method to select files (jfilechooser)

//........open JFileChooser and get file array here

myFrame.setFileArray(the_file_array_you_selected);

}

}

if this will not work for you, so post the whole code plz

java_2006a at 2007-7-12 8:25:43 > top of Java-index,Java Essentials,Java Programming...
# 11
I wish you would have answered the questions I posted in post #4, above - they were very direct questions.~Bill
abillconsla at 2007-7-12 8:25:43 > top of Java-index,Java Essentials,Java Programming...
# 12

> [code]

> public class A{

> /* ... */

> private void

> jMenuItemImportarActionPerformed(java.awt.event.Actio

> Event evt) {

>

> ay fine ... is this where you build the File array?

// No, I build the file array at the class of JDialog , in this class there are a method where i need the file array

>

> ImportaArchivoJDialog(this,true).addWindowFocusListen

> r(new java.awt.event.WindowFocusListener() {

> public void windowGainedFocus(WindowEvent

> windowEvent) {

>/* ... */

> }

> public void windowLostFocus(WindowEvent

> windowEvent) {

> CargaArchivo(fArchivo); // In this method I

> need the file array, but

> //

> fArchivo is not in this class

> //

> So I ask you ... what is this File array called?

// In the method CargaArchivo, i need to use the file array, so

//in this code in the event i was trying to use it to call this method

>}

> ;

>}

> /code]

kaminante11a at 2007-7-12 8:25:43 > top of Java-index,Java Essentials,Java Programming...
# 13

Here is the code posted with my question in the form of a comment.

public void windowLostFocus(WindowEvent windowEvent) {

CargaArchivo(fArchivo); // In this method I need the file array, but

// fArchivo is not in this class

// So I ask you ... what is this File array called?

Another poster posited that you should pass to the CargaArchivo method the Object named fArchivo.

So all I asked was, if fArchivo is not the name for the Object, what is it? If you are going to pass an Object in this manner, than the Object reference must have a name.

~Bill

abillconsla at 2007-7-12 8:25:43 > top of Java-index,Java Essentials,Java Programming...
# 14

> Here is the code posted with my question in the form

> of a comment.

public void windowLostFocus(WindowEvent windowEvent)

{

CargaArchivo(fArchivo); // In this method I

need the file array, but

//

fArchivo is not in this class

//

So I ask you ... what is this File array called?

> Another poster posited that you should pass to the

> CargaArchivo method the Object named fArchivo.

>

> So all I asked was, if fArchivo is not the name for

> the Object, what is it? If you are going to pass an

> Object in this manner, than the Object reference must

> have a name.

>

> ~Bill

About your question, yes, the object is fArchivo. It is build in class where paint JFileChooser, and I need it at other class (named class A at the example)

I'm trying with the code that java2006 posted, it works but I don't know in waht cases it will works same.

Thanks a lot

kaminante11a at 2007-7-12 8:25:43 > top of Java-index,Java Essentials,Java Programming...