LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Java: Getting the contents of a search dialog box in text editor i am making (https://www.linuxquestions.org/questions/programming-9/java-getting-the-contents-of-a-search-dialog-box-in-text-editor-i-am-making-173655/)

ludeKing 04-23-2004 07:26 AM

Java: Getting the contents of a search dialog box in text editor i am making
 
Hi all,
I am makinga text editor in Java :)

I am trying to implement the search function but am having problems:
I have a class called SearchDialog, which contains (amongst others) the following method:
Code:

protected String getText() {
return target.getText();
}

So therefore when one enters text in a search dialog box, the string that they enter is retrieved by this method.

Now I need to actually get this info. I am writing another method to do this. I need to get the string, and then I can use the indexOf() method to find the search string in a certain string.

So far, my method looks like this:
Code:

public void Search(){
        String searchString = searchDialog.getText();
        System.out.println("The search term is: " + searchString);
        //other command to come here, I am just testing at the moment.
    }

But I am getting this error:
Code:

non-static method getText() cannot be referenced from a static context
        String searchString = SearchDialog.getText();
                                          ^

Whats going wrong, why can't I access it? Please help!!!!

ludeKing 04-23-2004 08:11 AM

I just made everything associated with the getText() method static as wel,, and it is working.

Is this bad design? Should I be doing this a different way?

Looking_Lost 04-23-2004 12:56 PM

Are you using an InputDialog to get the input? If so it'll return the string entered once a button in the dialog is clicked, so you could have a method in your class



private String getSearchTerm(){

String input =null;

input=JOptionPane.showInputDialog(blah...blah...blah);

return input;
}


and you'll still be staying with the event driven notion of doing things

Komakino 04-23-2004 01:00 PM

Are you calling the Search function from main()? If so that's not a good way of doing it and would explain the static context.

ludeKing 04-23-2004 08:46 PM

No I'm calling it from another method outside of main.

The problem is, we are'nt allowed to modify the code the lecturer gave us for the SearchDialog class too much.
I just added the static flags and now it works fine. Can't see whats wrong with doing that!


All times are GMT -5. The time now is 07:33 AM.