tags:

views:

66

answers:

2

Hello. I call addNotify() method in class that I posted here. The problem is, that when I call addNotify() as it is in the code, setKeys(objs) do nothing. Nothing appears in my explorer of running app.

But when I call addNotify()without loop(for int....), and add only one item to ArrayList, it shows that one item correctly.

Does anybody know where can be problem? See the cede

class ProjectsNode extends Children.Keys{
private ArrayList objs = new ArrayList();

public ProjectsNode() {


}

    @Override
protected Node[] createNodes(Object o) {
    MainProject obj = (MainProject) o;
    AbstractNode result = new AbstractNode (new DiagramsNode(), Lookups.singleton(obj));
    result.setDisplayName (obj.getName());
    return new Node[] { result };
}

@Override
protected void addNotify() {
    //this loop causes nothing appears in my explorer.
    //but when I replace this loop by single line "objs.add(new MainProject("project1000"));", it shows that one item in explorer
    for (int i=0;i==10;i++){
        objs.add(new MainProject("project1000"));
    }
    setKeys (objs);
}

}

+5  A: 

Look at this loop:

for (int i=0;i==10;i++)

That's going to start with i = 0, and keep going while i == 10. I think you meant:

for (int i = 0; i < 10; i++)

(Extra whitespace added just for clarity.)

Jon Skeet
Problem solved, thanks.
joseph
+1  A: 

Jon is right... your loop is very likely to be incorrect.

Here is a translation of your for-loop into a while loop, just to clarify his observation even more...

Your loop currently means this... (in while-loop-ness)

int i = 0;

while (i==10) {
    objs.add(new MainProject("project1000"));
    i++;
}
setKeys (objs);

The addNotify is never called because add is never called...

vkraemer
this is surprise for me. Alhough I have not so much experience in programming, I used for cycle a lot of times. And this never happend to me till now.
joseph
No, you haven’t used the `for` loop like this and have had it working because *it does not work*.
Bombe
@Bombe - I do not write "like this". So, yes, I used for cycle for a couple of years and I've never had this problem.
joseph
Of course, when using the `for` loop correctly it does indeed work. Unfortunately “using the `for` loop correctly” has nothing to do with what you have done. :)
Bombe