Search code examples
javagwtflowlayoutpanel

How to call outer method in GWT?


This is my code:

final DialogBox menuWrapper = new DialogBox(true);
MenuBar options = new MenuBar(true);
menuWrapper.add(options);
options.addItem("First", new ScheduledCommand()
        {
            FlowPanel flowpanel;
            ..
            flowpanel = new FlowPanel();
            flowpanel.add(txtFirst);
            ..
            ChartLoader chartLoader = new ChartLoader(ChartPackage.CORECHART);
            chartLoader.loadApi(new Runnable() {

                        @Override
                        public void run() { 
                        ..
                        LineChart lineChart = new LineChart();
                        Datatable ..
                        lineChart.draw(dataTable);
                        panelChart.add(lineChart);
                        addPanel(panelChart);
                        }
                    private void addPanel(LayoutPanel panelChart) {
                        flowpanel.add(panelChart);
                    }
            }
        }

For this line

   flowpanel.add(panelChart);

I have this error:

Cannot refer to a non-final variable flowpanel inside an inner class defined in a different method

I'd like to add all my widget to flowPanel which is on PopupPanle, but I can't to define final method, any solution to solve this problem? Thanks.


Solution

  • You need to add a modifier final before a FlowPanel declaration:

    final FlowPanel flowpanel = new FlowPanel();
    

    Or, you can make this FlowPanel a class variable:

    private FlowPanel flowpanel;
    ...
    // your class methods
    

    When you refer to an object in a deferred or asynchronous method, the compiler wants to make sure that this object will not be reassigned by the time it is needed.