简体   繁体   中英

Passing data from one class to another in Java

I am trying to pass an output to a text area.

I have Class Search which handles all the searching and display the output using System.out.println() .

I have created Class GUI in order to make the console output (System.out.println()) appear in the JTextArea .

I am trying to pass that data into the text area using objects but I dont know why it is not working.

Class Search has this method that calculates the output:

 public static void searchIndex(String searchString) throws IOException, ParseException 

Class GUI has the text1

In Class GUI I have tried this:

text1.setText(Search.searchIndex(searchString));

but it is giving me an error searchString cannot be resolved to a variable

Any suggestions ?

Regards.

The method is not returning:

public static void searchIndex(String searchString) throws IOException, ParseException {

Need to change void to String and also return the result:

public static String searchIndex(String searchString) throws IOException, ParseException {
    //do search
    return resultString; 
}

For the following to work:

text1.setText(Search.searchIndex(searchString));

searchString cannot be resolved to a variable

The above error is caused by the fact that you are trying to use the variable "searchString" without declaring it.

String searchString = "Some string that is in the search index";
text1.setText(Search.searchIndex(searchString));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM