Need help with JFileChooser and methods
Hi how do I call this into an other class (bold)
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"JPG & GIF Images", "jpg", "gif");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(parent);
if(returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println("You chose to open this file: " +
chooser.getSelectedFile().getName());
}
I have been following the Swing tutorial on this site but I still can't really get it though, please help.
Thank you for reading
Sorry this is the code I want to call from an other class...
if (returnVal == JFileChooser.APPROVE_OPTION)
{
File file = fc.getSelectedFile();
System.out.println("You chose to open this file: " + file.getName());
}
You see that "file" I want to call that name to a string in an other class, once agian please help
> Suppose that other class has a method setFilename,
> and that the current class has a field named target
> that references an instance of that other class:
> > target.setFilename(file.getName());
>
Ok, so here's the deal I call the class with the FileChooser "RectPanel" and the other class "List". How do I get the file from RectPanel to List. Do i type:List.setFilename(file.getName();
Or where do I get that iformation to the List class. If it's possible send me an example of how you can do...
P.S. I'm very new to methods have never dealt with them before
There are only so many possibilities:
1. RectPanel creates an instance of List as a field:
public class RectPanel {
private List list = new List();
.... list.setFilename(file.getName());
}
2. Rect panel creates an instance of List in the method where it obtains the File object:
public class RectPanel {
public void method() {
List list = new List();
....
list.setFilename(file.getName());
}
}
3. List is created else where and passed to a RectPanel method that reatins a reference in a field:
public class RectPanel {
private List list = null;
public void setList(List x) {
list = x;
}
.... list.setFilename(file.getName());
}
4. List is passed to the method that obtains the File object:
public class RectPanel {
public void method(List list) {
....
list.setFilename(file.getName());
}
}
5. etc...
I must thank you for being very helpful.
I haven't figured out how to do yet but you have helped me and for that I'm greatful.
I'm a too much of a beginner to try to understand these thing as passing information as methods, I hope I'll understand some day.
//Many Thanks Considerate