从FXML控制器中的其他类调用方法

时间:2014-04-08 23:47:26

标签: java controller javafx fxml scenebuilder

我目前正在做家庭作业,使用场景构建器和FXML控制器中的编码在javaFX中创建一个带有多个标签的Web浏览器。我相当新,但我似乎无法弄清楚如何从控制器类中的其他类访问其他方法。

我有一个文件名FXMLDocumentController.java,其中包含我的所有编码,另一个文件名为Name.java,它只加载由场景构建器创建的.fxml文档。

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());
            }
        }
    });
}

}

这将创建一个包含后退,前进,停止和历史记录下拉列表的浏览器。我现在需要创建多个选项卡,所以我需要创建一个新的.java文件,它将创建新的选项卡并有一个方法来返回当前的webengine,所以我可以实现所有这些功能。

问题是,当我创建一个新类时,我无法从控制器访问它,我不断收到错误。例如,如果我有一个名为BrowserTab的新文件,其中包含一个名为newBrowserTab的方法,那么如何使用我的所有其他代码从控制器文件中调用该方法。我认为它会是这样的:

BrowserTab.newBrowserTab(tabPane, "New Tab")

1 个答案:

答案 0 :(得分:1)

有关快速解决方案,请参阅static methods

静态方法是最直接的,但不是非常面向对象的。 passing object instances around to FXML controllers还有其他方法,它们不涉及静态方法,并提供更大的封装,实例化,多态支持等,而且全局状态较少。