0

i have a method that create a array with some path to files...

and in the program i will press a button "Next Page", to show tem next item in the array[1] array[2]...

but the event of the button Next Page Cant access the array created inside the metlhod

i know how to pass the array between methods, but how to make a button event to access it ?!

the code is so big but its something like this:

    public class gui {
        public void actionPerformed(ActionEvent arg0) {
         ->>>>>     i want the array accessible from here,

        }
public void geraListaArquivos(String subdir, String matricula) {
....
....

....
File[] listOfFiles = folder.listFiles();
...
....
}


}

i want the access of the values in ListOfFiles, at the ActionPerfomed, how can i do this ;x?

Mark Davidson
  • 5,503
  • 5
  • 35
  • 54
user2582318
  • 1,607
  • 5
  • 29
  • 47

5 Answers5

1

Any Specific reason for using Arrays[] ? Use an ArrayList.

If you need to use Arrays specifically, just replace the ArrayList by arrays.

public class gui {

       public static List<String> listOfFiles = new ArrayList<String>();


        public void actionPerformed(ActionEvent arg0) {
         ->>>>>     i want the array accessible from here,

            // Access the listOfFiles Here.
            for(int count=0; count<listOfFiles.size(); count++)
            { 
                // DO YOUR OPERATION HERE 
            }

        }
      public void geraListaArquivos(String subdir, String matricula) {
      ....
      ....

      ....
      listOfFiles.add("FileName");
      ...
      ....
      }


}

Let me know if it you have any questions.

JNL
  • 4,683
  • 18
  • 29
1

As an alternative to exposing the array, consider using Action to encapsulate the File and its ActionListener, as shown here. This will make it easier to localize the code shared by buttons and menu items. In this way, you can allow the user to operate on File instances individually, rather than sequentially.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
0

Move your listOffFiles declaration outside the method. Then you can still populate it from the geraListaArquivos method and access it from your event handler like so:

public class gui {

    private File[] listOfFiles = null;

    public void actionPerformed(ActionEvent arg0) {
        // do something with listOfFiles here

    }

    public void geraListaArquivos(String subdir, String matricula) {
    ....
    ....
    ....
        listOfFiles = folder.listFiles();
    ....
    ....
    }
}
Jason Rae
  • 2,583
  • 5
  • 25
  • 34
0

You must move listOfFiles outside the "geraListaArquivos". You have various alternatives:

1 - ) You can put in a class variable. 2 - ) You can put in a instance variable. 3 - ) You can extract to another class, accesible for both methods. 4 - ) You can do a inner class. and go on...

You must choose what is better for your architecture.

0

You can draw an Image that looks like a button. Then try asking in a Thread: If mouseX is bigger than buttonX and less than buttonX+buttonLength and mouseY is bigger than buttonY and less than buttonY+buttonLength, you can call your methode( perhaps buttonIsPressed();). I hope it helps.