来自动态SWT按钮的ActionEvent

时间:2014-02-21 18:47:22

标签: java button swt

我正在开发一个可以从数据库返回结果的UI。我想在返回的每一行旁边放一个按钮。我想基于它的状态(真/假)来操纵按钮,并根据状态触发其他几个事件。任何帮助将不胜感激。我的每个按钮都有一个id分配给它,但是按下它时我无法引用它。

private ArrayList<ExtButton>   btnList        = new ArrayList<ExtButton>();

/*
 * =======================================================
 * Inner Classes
 * =======================================================
 */
class ExtButton extends Composite
{
        private Button button;

        private int m_id;
        public ExtButton(Composite parent, int id) { 
            super(parent, SWT.NULL);
            setLayout(new FillLayout());
            button = new Button(this, SWT.TOGGLE | SWT.FLAT);
            button.setText("MyButton");
            m_id = id;
        }

        public int getId()
        {
            return m_id;
        }

        public Button getButton(){

            return button;
        }
}

...

   private void search(int resultSize)
{
    TableEditor[] rowEditor = new TableEditor[resultSize];

    for (int i = 0; i < resultSize; i++) {
        final TableItem item = new TableItem(resultTable, SWT.NONE);
        rowEditor[i] = new TableEditor(resultTable);
        ExtButton btnSelect = new ExtButton(resultTable, i);

        btnSelect.getButton().setText("Select");
        btnSelect.computeSize(SWT.DEFAULT, resultTable.getItemHeight());

        rowEditor[i].grabHorizontal = true;
        rowEditor[i].minimumHeight = btnSelect.getSize().y;
        rowEditor[i].minimumWidth = btnSelect.getSize().x;

        rowEditor[i].setEditor(btnSelect, item, 0);

        btnList.add(btnSelect);

        btnSelect.getButton().addSelectionListener(new SelectionAdapter() {
                public void widgetSelected(SelectionEvent event) {
                    Integer btnId = ((ExtButton) event.getSource()).getParent().getId();
                    ExtButton button = btnList.get(btnId);
                    boolean btnState = ((ExtButton) event.getSource()).getButton().getSelection();
                    if (btnState == true)
                    {

                        button.getButton().setText("Select");
                    }

java:935:错误:找不到符号
[javac] Integer btnId =((ExtButton)event.getSource())。getParent()。getId();
[javac的] ^
    [javac] symbol:方法getId()
    [javac] location:class Composite
    [javac] 1错误

谢谢!

1 个答案:

答案 0 :(得分:1)

getId是一种ExtButton而非Composite的方法(由getParent返回)

Integer btnId = ((ExtButton)event.getSource()).getId();