0

I am trying to make a browser using javafx in which i want to add the bookmark functionality.I want to bookmark the webpage and display it in the menuitem.Bookmarking is working but the problem is that it is not displaying the bookmark field in the menuitem.I try to print the value in the console it is printing the value in the console but it is not displaying in the menuitem.

Now ,FirstpageController is the main controller whose fxml file is "Firstpage.fxml".This fxml only contain a tabpane.It contain an included file that is TabpageController which contains following code: TabpageController contains a button "bookmark_btn" which is used to add bookmark in the browser using addbookmark() method.It's fxml file is "Tabpage.fxml" which is included in "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;

    }

Now by clicking on "bookmark_btn" it open new scene that is "Bookmarkpage.fxml" which a simple bookmarking window which contain 2 textfield for url,title and "add" button.Now by clicking on "Add" button it should bookmark the current page.There is the menu "bookmenu" in the "tabpage.fxml" which contain menuitem "bkmenuitem".All the bookmark field should be display in the menuitem "bkmenuitem" which is in the "tabpage.fxml" file. Also the controller of this "Bookmarkpage.fxml" is "Bookmarkcontroller.java" which contain following code:

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

This the model class of the "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;
}

}

Output from Bookmarkcontroller.java
google
add to menuitems
Output from TabpageController.java
setbookinfo enetring for add btn
google
google

setbookinfo

As it is printing the output of these 2 lines as"google"
System.out.println(bookinfo.getTitile());
System.out.println(bkmenuitem.getText());

but not showing anything in the menuitem field for following line...
bkmenuitem.setText(bookinfo.getTitile());

I try a lot and also check it on stackoverflow on how to pass parameter from one controller to another but nothing is working.Also I am not getting any error.But the menuitem "bkmenuitem " is not printing the value.Any help would be appreciated as i am struggling a lot sorry for the long code .If i don't provide this much code then i won't understand anything.

user123
  • 11
  • 2

1 Answers1

0

I was able to get this to work by changing the TabpageController addbookmark method to

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

showing the stage after scene is added and passing thru the menuitem then in Bookmarkcontroller creating a setter for the menu item and updating it

 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());
    }
Nigel Savage
  • 991
  • 13
  • 26
  • thanks for answer but still not working there is not any problem in addbookmark(). Still I am not getting value in "bkmenuitem" Menuitem. I want "bkmenuitem.setText(bookinfo.getTitile());" this thing to work.But it is printing the value only in the console but not in the menuitem.Any help would be appreciated. – user123 May 30 '16 at 16:48
  • Thanks for the answer.But I don't want to implement in this way as it set bkmenuitem value before clicking on"Add" btn.I want the title to be display on the menuitem after clickng on "Add" btn whose method is "BookmarkToModelActionEvent()" which is in "Bookmarkcontroller.java" and whose "setBookinfo()" is in "TabpageController.java" .In "setBookinfo()" when i write line "System.out.println(bkmenuitem.getText());" it's returning null value on console and not printing anything in application.I also try to generate getter and setter method of "bkmenuitem" but still the same output. – user123 May 31 '16 at 09:52