Calling methods from other classes in FXML controller


Question

I'm currently doing a homework assignment, creating a web browser with multiple tabs in javaFX using the scene builder and coding in the FXML controller. I'm fairly new, but I can't seem to figure out how to access other methods from other classes within the controller class.

I have one file name FXMLDocumentController.java where all my coding is in, and another file just called Name.java which just loads the .fxml document that was created by the scene builder.

package name;



public class FXMLDocumentController implements Initializable {

@FXML
private Tab TabPanel1;

@FXML
private Button backButton;

@FXML
private Button forwardButton;

@FXML
private Button goButton;

@FXML
private Label label;

@FXML
private MenuBar menubar1;

@FXML
private Button stopButton;

@FXML
private AnchorPane textArea1;

@FXML
private ComboBox urlAddress;

@FXML
private WebView webview1;
private WebEngine webEngine;

@FXML
void backButtonClickAction(ActionEvent event) {
    WebHistory history = webEngine.getHistory();
    int back = history.getCurrentIndex();
    if (back <= 0) {
    } else {
        history.go(-1);
    }

}

@FXML
void forwardButtonClickAction(ActionEvent event) {
    WebHistory history = webEngine.getHistory();
    int fwd = history.getCurrentIndex();
    fwd++;
    ObservableList<WebHistory.Entry> entryList = history.getEntries();
    int stop = entryList.size();
    if (fwd == stop) {
    } else {
        history.go(1);
    }
}

@FXML
void stopButtonAction(ActionEvent event) {
    webEngine.getLoadWorker().cancel();
}

@FXML
void goButtonAction(ActionEvent event) {
    String url = urlAddress.getValue().toString();
    if (url.startsWith("http://www.")) {
        webEngine.load(url);
    } else if (url.startsWith("www.")) {
        webEngine.load("http://" + url);
    } else {
        webEngine.load("http://www." + url);
    }

    //TabPanel1.setText(webEngine.getLocation());
    //System.out.println(webEngine.getTitle());
}

@FXML
void urlAddressEnter(ActionEvent event) {
    String url = urlAddress.getValue().toString();
    if (url.startsWith("http://www.")) {
        webEngine.load(url);
    } else if (url.startsWith("www.")) {
        webEngine.load("http://" + url);
    } else {
        webEngine.load("http://www." + url);
    }
}

@Override
public void initialize(URL url, ResourceBundle rs) {
    assert TabPanel1 != null : "fx:id=\"TabPanel1\" was not injected: check your FXML file 'FXMLDocument.fxml'.";
    assert backButton != null : "fx:id=\"backButton\" was not injected: check your FXML file 'FXMLDocument.fxml'.";
    assert forwardButton != null : "fx:id=\"forwardButton\" was not injected: check your FXML file 'FXMLDocument.fxml'.";
    assert goButton != null : "fx:id=\"goButton\" was not injected: check your FXML file 'FXMLDocument.fxml'.";
    assert label != null : "fx:id=\"label\" was not injected: check your FXML file 'FXMLDocument.fxml'.";
    assert menubar1 != null : "fx:id=\"menubar1\" was not injected: check your FXML file 'FXMLDocument.fxml'.";
    assert stopButton != null : "fx:id=\"stopButton\" was not injected: check your FXML file 'FXMLDocument.fxml'.";
    assert textArea1 != null : "fx:id=\"textArea1\" was not injected: check your FXML file 'FXMLDocument.fxml'.";
    assert urlAddress != null : "fx:id=\"urlAddress\" was not injected: check your FXML file 'FXMLDocument.fxml'.";
    assert webview1 != null : "fx:id=\"webview1\" was not injected: check your FXML file 'FXMLDocument.fxml'.";
    webEngine = webview1.getEngine();
    getHistory();

}

private void getHistory() {
    final WebHistory history = webEngine.getHistory();
    history.getEntries().addListener(new ListChangeListener<WebHistory.Entry>() {
        @Override
        public void onChanged(ListChangeListener.Change<? extends WebHistory.Entry> c) {
            c.next();
            for (WebHistory.Entry e : c.getRemoved()) {
                urlAddress.getItems().remove(e.getUrl());
            }
            for (WebHistory.Entry e : c.getAddedSubList()) {
                urlAddress.getItems().add(e.getUrl());
            }
        }
    });
}

}

This creates a browser with back, forward, stop and a history drop down list. I now need to create multiple tabs so I need to create a new .java file that will create new tabs and have a method to return the current webengine, so I can implement all of these features.

The problem is, when I create a new class I can't access it from the controller, I keep getting errors. For instance, if I had a new file named BrowserTab with a method called newBrowserTab, how could I call on that method from the controller file with all my other code. I figured it would be something like this:

BrowserTab.newBrowserTab(tabPane, "New Tab")
1
0
4/8/2014 11:47:25 PM

See static methods for a quick solution.

Static methods are the most straight-forward, but are not very object-oriented. There are other methods of passing object instances around to FXML controllers which don't involve static methods and provide greater encapsulation, instancing, polymorphism support, etc. with less global state.

1
5/23/2017 12:28:50 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon