1

I want to make a button on an AnchorPane without drag it from the library in the FXML file I want to do it progammatically: if the search button clicked, should show a new button not existed in the AnchorPane before I did this code but I don't know what is wrong with it:

private void searchButton(ActionEvent evt) { Button tab = new Button(); tab.setLayoutX(147); tab.setLayoutY(102); tab.setText("Tab1"); tab.setPrefHeight(27); tab.setPrefWidth(69); WebView wb = new WebView(); wb.setLayoutX(-1); wb.setLayoutY(128); wb.setPrefWidth(1604); wb.setPrefWidth(700); } 
5
  • You never add the Button to anything in the scene graph. Commented Apr 29, 2015 at 21:09
  • ok so can you show me how please ? Commented Apr 29, 2015 at 21:13
  • 1
    Not without more information. Where do you want the button to appear? Commented Apr 29, 2015 at 21:20
  • i want it to appear on the AnchorPane in the FXML file Commented Apr 29, 2015 at 21:22
  • So please show the code from the FXML file where the anchor pane is defined, and the code in the controller where it is injected. Commented Apr 29, 2015 at 21:47

1 Answer 1

1

I am assuming that you searchButton method is in controller attached to some FXML. Then all you need to do is this:

yourAnchorPane.getChildren().add(tab); 

If you don't have already published reference to anchorPane in your controller, then add this into your controller

@FXML AnchorPane yourAnchorPane; 

And in SceneBuilder select your anchorPane, go to code tab and enter "yourAnchorPane" as fx:id.

Further info on working with anchorpane is javadoc.


You probably also want to set some constraints on the tab to locate it at a position within the AnchorPane. For instance, the following code will locate your button tab relative to the top left corner of the AnchorPane: Ten pixels down and fifteen pixels to the right.

AnchorPane.setTopAnchor(tab, 10.0); AnchorPane.setLeftAnchor(tab, 15.0); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.