0

It should be simple, but i can't figure it out.. I have this code:

Form<?> form2 = new Form<Void>("form2") {
    @Override
    protected void onSubmit() {
        ...
    dataView = new DataView("listview", new ListDataProvider(list));
        ...
    }
}
add(dataView);

How to define dataView and where? If i try to make final DataView dataView = null; occur an error: The final local variable dataView cannot be assigned, since it is defined in an enclosing type

fen1ksss
  • 1,100
  • 5
  • 21
  • 44
  • See http://stackoverflow.com/questions/1299837/cannot-refer-to-a-non-final-variable-inside-an-inner-class-defined-in-a-differen – halex Oct 04 '12 at 15:47

1 Answers1

0

You cannot assign to a final variable and only final variables can be referenced from inside the enclosed type. You can however mutate final variables from inside the enclosed type.

If the DataView is not immediately available you may need a blocking queue which will block until it becomes available:

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue; 

final BlockingQueue<DataView> dataViews = new ArrayBlockingQueue<DataView>();
Form<?> form2 = new Form<Void>("form2") {
   @Override
   protected void onSubmit() {
      ...
      dataViews.offer(new DataView("listview", new ListDataProvider(list)));
      ...
   }
};
//form is submitted in a different thread somewhere between here and add
...
add(dataViews.take());


Now it is impossible to tell from the context you gave us but the simplest solution, which doesn't require any final variables, would be to just call add(dataView) inside submit like so:

Form<?> form2 = new Form<Void>("form2") {
   @Override
   protected void onSubmit() {
      ...
      DataView dataView = new DataView("listview", new ListDataProvider(list));
      add(dataView); 
      ...
   }
};
cyon
  • 9,340
  • 4
  • 22
  • 26
  • then on line **add(dataViews);** occur an error: **The method add(Component...) in the type MarkupContainer is not applicable for the arguments (List)** – fen1ksss Oct 04 '12 at 16:01
  • That is because the `add()` method expects a `DataView` not a `List`. But you also need to make sure that the `submit` method executes and `dataView` is added to the list before you call `add()` at the end. You may even need `java.util.concurrent.Future`. – cyon Oct 04 '12 at 16:18