سؤال

I'd like to do the following without making the variable gui final:


public class MainClass
{

        GUIClass gui;
        Runnable r = new Runnable()
        {
            public void run()
            {
                // this won't work!
                gui = new GUIClass();
            }
        };
        SwingUtilities.invokeLater(r);
        Controller c = new Controller(gui);     

}

How can I achieve that? I want to construct the gui via the EDT. At the same time I want to assign that new instance to the variable gui. But this won't work without making gui final. I don't want to use final because it's not possible in my context. Anyone any idea how this could be solved? The code above is of course executed within the main method. But for some reasons I couldn't post it here as an error occured.

هل كانت مفيدة؟

المحلول

For all those who should be interested in this issue someday, here's the solution: I made a mistake. Actually you should differentiate between constructing the GUI-Object and making it visible. So here is the way to do that:


public class MainClass
{

        final GUIClass gui = new GUIClass();
        Runnable r = new Runnable()
        {
            public void run()
            {           
                gui.pack();
                gui.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
        Controller c = new Controller(gui);     

}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top