Javafx:Menuitem没有从控制器

时间:2016-05-30 14:11:47

标签: javafx bookmarks

我正在尝试使用javafx制作浏览器,其中我想添加书签功能。我想将网页加入书签并在menuitem中显示。书签正在运行,但问题是它没有显示书签字段在menuitem中。我尝试在控制台中打印它在控制台中打印的值,但它没有显示在menuitem中。

现在,FirstpageController是主控制器,其fxml文件是" Firstpage.fxml"。这个fxml只包含一个tabpane.It包含一个包含文件的TabpageController,其中包含以下代码: TabpageController包含一个按钮" bookmark_btn"用于在浏览器中使用addbookmark()方法添加书签。它的fxml文件是" Tabpage.fxml"它包含在" Firstpage.fxml"。

TabpageController.java

public class TabpageController implements Initializable
{
   @FXML public TextField addressField;
   @FXML private Button bookmark_btn;
   @FXML private Menu bookmenu;
   @FXML public MenuItem bkmenuitem;

  @FXML
    private void addbookmark() throws IOException
    {
        Bookmodel bm = new Bookmodel(addressField.getText(), first.tab1.getText()); //Take the url and tile
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(Main.class.getResource("/tabcheck/Bookmarkpage.fxml"));
        AnchorPane bookmarkpane = loader.load();

        Stage bookstage = new Stage();
        bookstage.setResizable(false);
        bookstage.show();           
        Scene bookscene = new Scene(bookmarkpane);
        bookstage.setScene(bookscene);
        Bookmarkcontroller bc = loader.getController();
        bc.setBookinfo(bm);         
        //bkmenuitem.setText(DEFAULT_HOME.toString()); 
    }

    public void setBookinfo(Bookmodel bookinfo)
    {
        System.out.println("setbookinfo enetring for add btn");
        bkmenuitem.setText(bookinfo.getTitile());
        System.out.println(bookinfo.getTitile());
        System.out.println(bkmenuitem.getText());
        System.out.println("setbookinfo");
        this.bookinfo = bookinfo;

    }

现在点击" bookmark_btn"它开启了新的场景,即#34; Bookmarkpage.fxml"这是一个简单的书签窗口,其中包含2个文本字段,用于网址,标题和"添加"按钮。现在点击"添加"按钮它应该为当前页面添加书签。有菜单" bookmenu"在" tabpage.fxml"其中包含menuitem" bkmenuitem"。所有书签字段都应显示在菜单项目中#b; bkmenuitem"它位于" tabpage.fxml"文件。 也是这个" Bookmarkpage.fxml"的控制器。是" Bookmarkcontroller.java"其中包含以下代码:

Bookmarkcontroller.java

public class Bookmarkcontroller {
@FXML
 TextField bookmarkTitleTextField;
@FXML 
 TextField bookmarkURLTextField;
@FXML private Button addbook;

TabpageController tabpageController;
private Bookmodel bookinfo;

@FXML private void BookmarkToModelActionEvent(ActionEvent event) throws IOException
{

    ((Node) event.getSource()).getScene().getWindow().hide();
    Bookmodel addbm = new Bookmodel(bookmarkTitleTextField.getText());
    System.out.println(bookmarkTitleTextField.getText());
    System.out.println("add to menuitems");
    FXMLLoader loader = new FXMLLoader();
    loader.setLocation(Main.class.getResource("/tabcheck/Tabpage.fxml"));
    AnchorPane bookmarkpane = loader.load();
    TabpageController addtc = loader.getController();
    addtc.setBookinfo(addbm);
    //bkmenuitem.setText(bookmarkTitleTextField.getText());
}

  public void setBookinfo(Bookmodel bookinfo) {
    bookmarkURLTextField.setText(bookinfo.getUrl());
    bookmarkTitleTextField.setText(bookinfo.getTitile());
    this.bookinfo = bookinfo;
}

这是" Bookmarkcontroller"

的模型类

Bookmodel.java

public class Bookmodel {

private String url;
private String titile;
public Bookmodel() {

}
public Bookmodel(String url, String titile) {
    this.url = url;
    this.titile = titile;
}
public Bookmodel(String titile) {
    this.titile = titile;
}
public String getUrl() {
    return url;
}
public void setUrl(String url) {
    this.url = url;
}
public String getTitile() {
    return titile;
}
public void setTitile(String titile) {
    this.titile = titile;
}

}

Bookmarkcontroller.java的输出
谷歌
添加到menuitems
TabpageController.java的输出
setbookinfo enetring for add btn
谷歌
谷歌

setbookinfo

正在打印这两行的输出为" google"
        的的System.out.println(bookinfo.getTitile());
        的的System.out.println(bkmenuitem.getText());

但没有在menuitem字段中显示以下行的任何内容...
      的 bkmenuitem.setText(bookinfo.getTitile());

我尝试了很多,并在stackoverflow上检查它如何将参数从一个控制器传递到另一个控制器,但没有任何工作。我也没有收到任何错误。但是菜单项" bkmenuitem"没有打印这个值。任何帮助都会受到赞赏,因为我很长时间的代码都很难过。如果我没有提供这么多代码,那么我就不会理解任何事情。

1 个答案:

答案 0 :(得分:0)

我可以通过将TabpageController addbookmark方法更改为

来实现此功能
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation( ....);
        loader.load();
        AnchorPane bookmarkpane = loader.getRoot();
        Bookmarkcontroller bc = loader.getController();
        Stage bookstage = new Stage();
        bookstage.setResizable(false);

        Scene bookscene = new Scene(bookmarkpane);
        bookstage.setScene(bookscene);
        bookstage.show();
        bc.setMenuItem(bkmenuitem);
        bc.setBookinfo(bm);

显示场景添加后的阶段,然后通过menuitem 在Bookmarkcontroller中为菜单项创建一个setter并更新它

 MenuItem menuitem;
    public void setMenuItem(MenuItem bkmenuitem)
    {
       this.menuitem = bkmenuitem  ;


    }


public void setBookinfo(Bookmodel bookinfo) {
        bookmarkURLTextField.setText(bookinfo.getUrl());
        bookmarkTitleTextField.setText(bookinfo.getTitile());
        this.bookinfo = bookinfo;

        menuitem.setText(bookinfo.getTitile());
    }