KSJaay
KSJaay

Reputation: 59

How to make a String available in other classes

I've managed to make the input into a string which is available within the same class but I want to make it so the input string can be available in different classes. Current class is OpenDetails and I want the string selectedFile to be available in a different class called OpenFileInfo. How would I set it so the result from selectedFile can be stored in either selectedRequirement or make it available in other classes?

I'm new to Java so if someone could help thank you.

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;

public class OpenFile
{
    String selectedRequirement = "";

    public static void main(String a[])
    {

    JFrame parent = new JFrame();
    String selectedFile;
    selectedFile = JOptionPane.showInputDialog(parent, "Add a new module");


    if(selectedFile.equalsIgnoreCase(selectedFile)){
    //Makes the user input case insensitive
    }

        final JTextArea edit = new JTextArea(60,100);

        JButton read = new JButton("Open "+ selectedFile +".txt");
        read.addActionListener( new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                try
                {
                    FileReader reader = new FileReader(selectedFile + ".txt");
                    BufferedReader br = new BufferedReader(reader);
                    edit.read( br, null );
                    br.close();
                    edit.requestFocus();
                }
                catch(Exception e2) { System.out.println(e2); }
            }
        });

        JButton write = new JButton("Save "+ selectedFile + ".txt");
        write.addActionListener( new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                try
                {
                    FileWriter writer = new FileWriter(selectedFile + ".txt");
                    BufferedWriter bw = new BufferedWriter( writer );
                    edit.write( bw );
                    bw.close();
                    edit.setText("");
                    edit.requestFocus();
                }
                catch(Exception e2) {}
            }
        });
                System.out.println("Module: " + selectedFile);

        JFrame frame = new JFrame("Requirements");
        frame.getContentPane().add( new JScrollPane(edit), BorderLayout.NORTH );
        frame.getContentPane().add(read, BorderLayout.WEST);
        frame.getContentPane().add(write, BorderLayout.EAST);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible(true);
    } 
}

Upvotes: 0

Views: 54

Answers (1)

CA2C7B
CA2C7B

Reputation: 378

As you are running from a static context, you need to define selectedRequirement as static:

private static String selectedRequirement = "";

To make selectedRequirement equal to selectedFile, simply say selectedRequirement = selectedFile; towards the end of the main function (maybe where you print it already).

To make selectedRequirement available to other classes, you need to create a "getter function" in the OpenFIle class (outside of the main function) like:

public String getSelectedRequirement(){
    return selectedRequirement;
}

As pointed out in the comments, it would be a good idea for you (or anyone who finds this in the future) to look at some tutorials on getters, setters, and general encapsulation.

Upvotes: 1

Related Questions