user1729184
user1729184

Reputation: 21

Capture String argument as variable Java

Total Java noob, I need to figure out how to capture string entered as an argument for one method in order to reuse it later. Basically, here I pass String content as a label when creating buttons:

public void start() {

    // set up the label for one button

    viewer.setButtonLabel(1, "");
    viewer.setButtonLabel(2, "");
    viewer.setButtonLabel(3, "");




    // start the viewer
    viewer.open();
}

Then I want to reuse the string I've given as the button label as the keywords for a search in the next method, taking into account which button was pressed:

public void buttonPressed(int n) {

    // fetch an image of the Forum from Flickr
    Photo photo = finder.find("", 5);
}

I feel I'm making this much harder than it has to be but I'm totally new to Java and can't seem to find what I'm looking for. Would really appreciate any tips you may have.

Upvotes: 0

Views: 187

Answers (1)

Zerkz
Zerkz

Reputation: 721

String buttonLabel1 = "Blah blah blah";

viewer.setButtonLabel(1, buttonLabel1);

Next Method

blahMethod(buttonLabel1);

This is one way of doing this, but probably the easiest to see. You can get a bit more complicated by storing all your button labels into a list instead (which will make your code look cleaner), but since you are new, I suggest you try it this way. Eventually, to make your code dynamic (ex. If I select the 4th button, get the 4th label), you will have to use some sort of data structure to avoid from getting extremely sloppy/messy.

You also can probably "get" the label of the button in order to retrieve the same string value.

Upvotes: 2

Related Questions