0

I am using a JFrame which accepts a value from the user and stores it in a variable (filePath). I want to use this value in another class. How can i hold the value from the JFrame and use it in another class?

JFrame code:

JFileChooser fileChooser = new JFileChooser();
        fileChooser.setDialogTitle("Specify a file to save");

        int userSelection = fileChooser.showSaveDialog(this);
        if (userSelection == JFileChooser.APPROVE_OPTION) {
            File fileToSave = fileChooser.getSelectedFile();
            String filePath = fileToSave.getAbsolutePath();
        }

Class code:

String filename="";

I want to get the filePath value into filename String.

Any help?

Maruthi Srinivas
  • 562
  • 2
  • 10
  • 22
  • Why not simply pass the value to the other class that needs it? – MadProgrammer Sep 30 '15 at 09:16
  • How to pass the value to the class? That is my question – Maruthi Srinivas Sep 30 '15 at 09:18
  • It sounds as though this frame should be an option pane or a modal dialog. See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Sep 30 '15 at 09:18
  • Thanks @AndrewThompson will look into it. – Maruthi Srinivas Sep 30 '15 at 09:19
  • *"How to pass the value to the class?"* This indicates a lack of knowledge in basic OOP. 'OOP 101' should be long behind you before attempts to make rich client desktop apps. .. – Andrew Thompson Sep 30 '15 at 09:19
  • My problem is not just passing a value to the class. Taking an user input from a JFrame, storing it in a variable and then passing it to the class. – Maruthi Srinivas Sep 30 '15 at 09:20
  • 1
    `fileChooser.showSaveDialog(this);` is passing a variable (`this`) to a class (`JFileChooser`). Perhaps you should go back to basics and have a look at [Passing Information to a Method or a Constructor](https://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html) before embarking on something as complex a GUI – MadProgrammer Sep 30 '15 at 09:24
  • *"Taking an user input from a JFrame, storing it in a variable and then passing it to the class"* ... How's that any different to passing a value to class? – MadProgrammer Sep 30 '15 at 09:26

2 Answers2

1

You can create a static method in that class where you want to hold the value. eg.

class Get {

    static String filename;

    public static void getValue(String value) {
        filename = value;
    }
}

Then once you get the file path from.

String filePath = fileToSave.getAbsolutePath();

Just after that call the static method of the other class. For instance in my case that class is Get.

Get.getValue(filePath);

OR Crete a constructor of the class that gets a string value.

class Get {

        String filename;

        Get(String value) {
            filename = value;
        }
    }
}

While creating the object of the class, Send that value to the constructor.

Get g = new Get(filePath);

And even simpler. Introduce a static variable in the holder class, from the jframe class set its value to filepath.

class Get {

    static String filename;

}

Then just set the value of the filename to the filePath as below.

Get.filename = filePath;
CodeRunner
  • 687
  • 5
  • 13
0

There are several ways to pass the parameter to another class. You haven't said much of the context of your problem, maybe You could write little more about it.

Nevertheless here are few ways:

Let say You get this string in class A and want to pass it to class B.

Make filename variable public property in class A, than class B can access that property if it has reference to class A. This is probably the worst solution because B has to have reference to class A and B must wait for it's turn to get this property.

Better solution is to implement Observer pattern where A is subject and B is observer. This is best solution if You need to pass string immediately after you get it from JFileChooser.

Another solution will be to store string in Singleton object that all classes have access to.

In my opinion, latter two solutions are preferred ones. But it all depends on context of the problem and complexity of your app (maybe there is no need to complicate code with patterns).

ldulcic
  • 61
  • 1
  • 5