How do I use a directory path variable from an activity listener from one class in another?
I am new to programming and I decided like an idiot that my first project would be one way to exceed my level. I haven't had much success trying to find a solution to this problem on the site, so I ask.
So, I have a button that creates a file named from user input and then creates directories containing that file. It's in the "NewProject" class.
JButton btnNewButton = new JButton("Create");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String projectName = textPane.getText();
File projectFolder = new File("C:\\Jibberish\\" + projectName);
File projectStart = new File("C:\\Jibberish\\" + projectName +
"\\" + "Project" + "\\" + "text.rtf");
Now, in another class, "workspace", I have a JTree and a JEditorPane. I want to know how I can get a variable of type "projectStart" in the class "workspace" so I could use the directory as the model for the JTree and the file "text.rtf" as the default text in the JEditor.
If you need more information, I will try to provide it. Please answer as if I don't know anything, because I don't. Thanks in advance.
source to share
Not sure if I quoted you correctly, but to transfer the project to Workpace.class you could create a private class variable and create a getter method for it.
private File projectStart = null;
private void buttonAction(){ //this is where your ActionListener stuff happens
JButton btnNewButton = new JButton("Create");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String projectName = textPane.getText();
File projectFolder = new File("C:\\Jibberish\\" + projectName);
projectStart = new File("C:\\Jibberish\\" + projectName +
"\\" + "Project" + "\\" + "text.rtf");
}
public File getProjectStart(){ //this method makes your projectStart-variable accessible for other classes
return projectStart;
}
In your workspace class, you can use this variable by calling it something like this:
private void foo(){
NewProject np = new NewProject();
File copyOfProjectStart = np.getProjectStart();
}
Hope this helps you.
source to share