Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(nested?) anonymous inner classes for buttons

I've used an anon inner class to get a button obj:

Button modButton = new Button("Modify");
modButton.addClickHandler(new ClickHandler() {
    @Override
    public void onClick(ClickEvent event) {
        //TODO: link to a pop-up, and do a refresh on exit
    }
});

I want to use this in an arbitrarily sized GWT FlexTable (which is basically an auto re-sizing table).

if i do something like this:

currentTable.setText(3, 0, "elec3");
currentTable.setWidget(3, 2, modButton);

currentTable.setText(4, 0, "elec4");
currentTable.setWidget(4, 2, modButton);

The button only shows up for the latter one (since there is only one instance). Since the table above will be populated programatically, its not really practical to define a new button for each possible instance.

I tried this the following:

currentTable.setText(4, 0, "elec4");
currentTable.setWidget(4, 2, new Button("Modify");
modButton.addClickHandler(new ClickHandler() {
    @Override
    public void onClick(ClickEvent event) {
        //TODO: link to a pop-up, and do a refresh on exit
    }
});
);

However, this won't compile at all (the first; I guess), I'm a bit lost - how can I accomplish this effect?

Thanks

like image 427
malangi Avatar asked Nov 14 '22 12:11

malangi


1 Answers

Your syntax is incorrect in the third example, but in any case, using an anonymous class in that case is impossible. You are trying to call addClickHandler on the newly-instantiated object, which is not stored in any variable. Theoretically, you could put that code in a constructor for your anonymous class and call that function on "this". The problem is, because of the peculiarities of Java's (absolutely atrocious) anonymous class syntax, it is impossible to define a constructor (what would it be called?).

I'm not 100% sure I understand what you are trying to accomplish, but could you define a function that just returned a new, correctly-configured button instance each time you called it? For example,

private Button newModButton() {
    Button modButton = new Button("Modify");
    modButton.addClickHandler(new ClickHandler() {
        @Override
        public void onClick(ClickEvent event) {
            //TODO: link to a pop-up, and do a refresh on exit
        }
    });
    return modButton;
}

Then you would call

currentTable.setWidget(4, 2, newModButton());
like image 120
Tim Yates Avatar answered Dec 06 '22 06:12

Tim Yates