1

I am making a map over the highest mountains in europe and want to create a for loop that makes the actions for the buttons for me.

I am currently stuck at this code:

String Text = "btn" + i;
        Text.addSelectionListener(new SelectionAdapter()

I want to add:

btn1.addSelectionListener(new SelectionAdapter()

btn2.addSelectionListener(new SelectionAdapter()

btn3.addSelectionListener(new SelectionAdapter()

btn4.....

when running the for loop. Does anyone know how to do this?

 for(final int i=1; i< country.length; i++){
        String Text = "btn" + i;
        Text.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {

                txtCountry= new Text(shell, SWT.BORDER);
                txtCountry.setText("Country: " + country[i]);
                txtCountry.setBounds(22, 112, 204, 30);
                formToolkit.adapt(txtCountry, true, true);

                txtHighestMountain = new Text(shell, SWT.BORDER);
                txtHighestMountain.setText("Highest Mountain: " + highestPoint[i]);
                txtHighestMountain.setBounds(22, 148, 204, 30);
                formToolkit.adapt(txtHighestMountain, true, true);

                txtMeters = new Text(shell, SWT.BORDER);
                txtMeters.setText("Meters above sea level: " + MAMSL[i]);
                txtMeters.setBounds(22, 184, 204, 30);
                formToolkit.adapt(txtMeters, true, true);
                }
}
2
  • 1
    and what is the problem exactly? any error? Commented Dec 18, 2015 at 11:25
  • I am getting the following error: The method addSelectionListener(new SelectionAdapter(){}) is undefined for the type String Commented Dec 18, 2015 at 11:28

3 Answers 3

1

Create an array of buttons:

Button[] btnArr = new Button[] {
    btn1, btn2, btn3,...
}

& loop through it:

for(int i = 0; i < btnArr.length; i++) {
    btnArr[i].addSelectionListener(new SelectionAdapter()....)
} 

Plus, to optimize your code, consider creating a custom SelectionListener.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks that looks like a good way to do it, however when trying to implement the array I get following error: btnArr cannot be resolved to a variable. Any suggestions here?
You have to declare it. Check my edit. Button[] btnArr = new ...
1

To achieve your first goal, just create a List with the buttons, and insert SelectionAdapter in each iteration.

List<Button> buttons = // fill the list

for (Button b : buttons) {
    b.addSelectionListener(new SelectionAdapter() {....});
}

Comments

0

In your code, you are adding a SelectionListener to your String. You will need to add the listener to your Button.

Furthermore, the code can be streamlined a bit, such as having the SelectionListener in a dedicated method.

An example:

private Button[] getButtons() {
    Button[] buttons = new Button[country.length];

    for (final int i = 1; i < country.length; i++) {
        String text = "btn" + i;
        Button button = new Button(text);
        button.addSelectionListener(getCountrySelectionListener(country[i]));
        buttons[i] = button;
    }
    return buttons;
}

private SelectionListener getCountrySelectionListener(final String country) {
    return new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {

            txtCountry = new Text(shell, SWT.BORDER);
            txtCountry.setText("Country: " + country);
            txtCountry.setBounds(22, 112, 204, 30);
            formToolkit.adapt(txtCountry, true, true);

            txtHighestMountain = new Text(shell, SWT.BORDER);
            txtHighestMountain.setText("Highest Mountain: " + highestPoint[i]);
            txtHighestMountain.setBounds(22, 148, 204, 30);
            formToolkit.adapt(txtHighestMountain, true, true);

            txtMeters = new Text(shell, SWT.BORDER);
            txtMeters.setText("Meters above sea level: " + MAMSL[i]);
            txtMeters.setBounds(22, 184, 204, 30);
            formToolkit.adapt(txtMeters, true, true);
        }
    }
}

This assumes the required variables and such is global. If not, you could always just pass them across as parameters, or create a Bean (Wrapper Object) for the related values, Such as a Country Bean.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.