0

I'm trying to make a reference of WebEngine

public class ClientArea implements Initializable {

public WebEngine engine;

    @Override
    public void initialize(URL url, ResourceBundle rb) {

        engine = browser.getEngine();
        engine.load("about:blank");
    }
}

for a class in the same file (ClientArea.java)

private static class SearchBox extends Region {

        private WebEngine engine; // private or public

        private TextField insertU;
        private Button refreshButton;
        private Button stopButton;
        private Button backButton;
        private Button forwardButton;
        public Button voltarButton;

        public SearchBox() {
        }
}

but that does not work, what would it be?

Perco
  • 160
  • 1
  • 18
  • can you elaborate "does not work", please? – Sergey Grinev Mar 20 '13 at 21:00
  • @Sergey has a reference example of WebEngine? – Perco Mar 21 '13 at 10:59
  • have you tried http://docs.oracle.com/javafx/2/webview/WebViewSample.java.htm ? – zhujik Mar 21 '13 at 11:04
  • @SergeyGrinev is not what I need, so I have two files. java has the 1st stage and the second has the webengine, I want to show what ta webengine happening on stage as the title, so I need the reference. – Perco Mar 21 '13 at 11:19
  • I'm not sure I understand your question. But if you need to transfer webengine instance from one class to another then just make a setter: `SearchBox#setEngine(WebEngine engine)`. – Sergey Grinev Mar 21 '13 at 12:56
  • @SergeyGrinev where to insert this code in my project? – Perco Mar 21 '13 at 13:18

1 Answers1

1

May be i don't understand your question. You want to access from a nested class (SearchBox) a field (engine) of the top level class (ClientArea).

It is easy if the nested class is not static. In this case, the inner class keeps a reference (ClientArea.this) to the outer class and could access to its fields and methods.

public class ClientArea implements Initializable {
    public WebEngine engine;

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        engine = browser.getEngine();
        engine.load("about:blank");
    }

    private class SearchBox extends Region {
        public SearchBox() {
            System.err.println(engine);
        }

        private ClientArea getClientArea() {
            return ClientArea.this;
        }

        private WebEngine getWebEngine() {
            return engine;
        }
     }
}

Look at this answer on the difference between the static nested class ad the inner class.

The java tutorial on the nested classes.

Community
  • 1
  • 1
gontard
  • 28,720
  • 11
  • 94
  • 117
  • Read my answer, the nested class must not be static. – gontard Mar 22 '13 at 13:40
  • HI @gontard, and if I want to use in another file. java? look what happened! http://i.stack.imgur.com/1yMFx.png – Perco Mar 22 '13 at 14:31
  • My solution works for inner class. An inner class keeps a reference on its outer class. Unrelated classes have not references between thems (fortunaly) – gontard Mar 22 '13 at 14:35