1

I've been messing with windowbuilder pro lately. I've lately run into the problem of figuring out how to add items to a list when a button is pressed. The error is that it can't seem to access list. Here is the code I have:

package wb;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import swing2swt.layout.BorderLayout;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.List;
import swing2swt.layout.BoxLayout;
import swing2swt.layout.FlowLayout;

public class WindowBuilderTest {

/**
 * Launch the application.
 * @param args
 */
public static void main(String[] args) {
    try {
        WindowBuilderTest window = new WindowBuilderTest();
        window.open();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

/**
 * Open the window.
 */
public void open() {
    Display display = Display.getDefault();
    Shell shell = new Shell();
    shell.setSize(450, 300);
    shell.setText("SWT Application");
    shell.setLayout(new BorderLayout(0, 0));

    TabFolder tabFolder = new TabFolder(shell, SWT.NONE);

    TabItem tbtmListTest = new TabItem(tabFolder, SWT.NONE);
    tbtmListTest.setText("List Test");

    Composite composite = new Composite(tabFolder, SWT.NONE);
    tbtmListTest.setControl(composite);
    composite.setLayout(new BorderLayout(0, 0));

    Composite composite_1 = new Composite(composite, SWT.NONE);
    composite_1.setLayoutData(BorderLayout.SOUTH);
    composite_1.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

    Button button = new Button(composite_1, SWT.NONE);
    button.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseUp(MouseEvent arg0) {
            list.add("Herpity Derpity"); //It screws up here.
        }
    });
    button.setText("+");

    Button button_1 = new Button(composite_1, SWT.NONE);
    button_1.setText("-");

    List list = new List(composite, SWT.BORDER);
    list.setLayoutData(BorderLayout.CENTER);

    TabItem tabItem = new TabItem(tabFolder, SWT.NONE);
    tabItem.setText("New Item");

    Button btnNewButton = new Button(shell, SWT.NONE);
    btnNewButton.setLayoutData(BorderLayout.SOUTH);
    btnNewButton.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseUp(MouseEvent arg0) {
            System.exit(0);
        }
    });
    btnNewButton.setText("Quit");

    shell.open();
    shell.layout();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }
    }
}

This is my first time asking a question on stackoverflow, so I have high hopes. I put my limited patience in the hands of you guys.

sarnold
  • 102,305
  • 22
  • 181
  • 238
Astrognome
  • 303
  • 3
  • 9

3 Answers3

2

Declare list above your call to addMouseListener, and declare it as final, e.g. final List list = ....

ziesemer
  • 27,712
  • 8
  • 86
  • 94
1

The list variable has function scope (i.e. local variable) and hence it is not visible in anonymous inner class. You have to declare list as a final (SO Thread - Why do we use final keyword with anonymous inner classes? ) or declare as as field of WindowBuilderTest class.

public class WindowBuilderTest
{
  List list;
  .....
}
Community
  • 1
  • 1
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
0

you should declare the list before using it.

final List list = new List(composite, SWT.BORDER);
Button button = new Button(composite_1, SWT.NONE);
button.addMouseListener(new MouseAdapter() {
    @Override
    public void mouseUp(MouseEvent arg0) {
        list.add("Herpity Derpity"); //It screws up here.
    }
});
dku.rajkumar
  • 18,414
  • 7
  • 41
  • 58
  • why -1, whats wrong in this?? list is to after "Button button = new Button(composite_1, SWT.NONE);" and i asked to declare before it. anyway i have edited to make it more clear – dku.rajkumar Dec 22 '11 at 14:49
  • It won't work unless the `list` is also declared as `final`, as the anonymous inner class won't be able to access it. You'll get an error like: "Cannot refer to a non-final variable list inside an inner class defined in a different method" – ziesemer Dec 23 '11 at 00:34