How to creat a button programmatically with JavaFX? - button

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

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

Related

Adding tabs into a TabPane depending on the clicked button [duplicate]

This question already has an answer here:
How do I determine the correct path for FXML files, CSS files, Images, and other resources needed by my JavaFX Application?
(1 answer)
Closed 2 years ago.
I have a file named Home.fxml which is a BorderPane. This component has buttons on the left and an empty TabPane called mainTabPane in the centre. I would like, when a user clicks on any button on the left, to add a tab to mainTabPane. I am able to add one tab when one button is clicked. But when I try to do the same for the second button, I get this error:
java.lang.IllegalStateException: Location not set
Here is the code for HomeController.java, where all is done:
public void initialize(URL loc, Resource Rse) {
createReportsTab();
loadCreateItemTab();
}
/*this is the one which works well*/
#FXML
void CreateReportTab() {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(ReportsController.class.getResource("gui/Reports.fxml"));
Parent parent = loader.load();
Tab reportsTab = new Tab("Reporting Engine");
reportsTab.setClosable(true);
reportsTab.setcontent(parent);
mainTabPane.getTabs().add(reportsTab),
}
/*this is the one which produces the error*/
#FXML
void loadCreateItemTab() {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(AddNewItemController.class.getResource("gui/addNewItem.fxml"));
Parent parent = loader.load();
Tab newItemTab= new Tab("New Item");
newItemTab.setClosable(true);
newItemTab.setcontent(parent);
mainTabPane.getTabs().add(newItemTab),
}
I am confused; why the Reporting Engine loads well but the second one is producing that error above?
Here is my project structure in IntelliJ IDEA:
All controller classes are in the controllers package and all FXML files are in the GUI package. The main class is in Companion package.
Error suggests that addNewItem.fxml is not read. Change:
AddNewItemController.class.getResources("gui/addNewItem.fxml");
to
ReportsController.class.getResources("gui/addNewItem.fxml");
, since this path worked for you already.

Gluon AppBar keep in the view

I'm trying to build a message window with App Bar on the top
This screen consist of Gluon App Bar on the top and VBox in bottom which has rest of the element shown in the screen.
Issue is when i click on the text area to enter text the App Bar goes out of scope.
Is there a way I can set App Bar to be always on top?
If you are deploying your app on Android, the TextField and TextArea controls include a built-in check: when those input controls get focused, if required the scene is moved up to prevent the software keyboard from hiding the control.
These translation affects the whole scene, so all the nodes in it (View, VBox, TextField, TextArea, AppBar, ...), will be translated as well.
So if you want to keep the AppBar node always visible and in the same top position, a possible fix could be counteracting that translation whenever it happens.
If you have a BasicView for instance (Gluon IDE plugin -> single view project), with a TextField control at the bottom of the view:
public BasicView() {
Label label = new Label("Hello JavaFX World!");
Button button = new Button("Change the World!");
button.setGraphic(new Icon(MaterialDesignIcon.LANGUAGE));
button.setOnAction(e -> label.setText("Hello JavaFX Universe!"));
VBox controls = new VBox(15.0, label, button, new TextField());
controls.setAlignment(Pos.BOTTOM_CENTER);
controls.setPadding(new Insets(20));
setCenter(controls);
}
you can modify it like this:
public BasicView() {
...
setOnShown(e -> {
MobileApplication.getInstance().getAppBar().translateYProperty()
.bind(controls.getScene().getRoot().translateYProperty().multiply(-1));
});
}
Make sure that the view is already added to the scene when you add this binding (to prevent a NPE).
Now when the textfield gets the focus, the whole scene will be moved up, and the appBar will be moved down the same amount:

Loading a new FXML file in the same scene

I'm new to JavaFX and i have a little problem with the UI.
I'm using JavaFX 8 and want to create my UI with FXML files.
I have created a pane with a header, a menubar and the placeholder for the content (This is the place where i want to load the others FXML files in when i press a Button from the menu bar). The content area is a AnchorPane.
I have tryed it with this:
#FXML
private AnchorPane aContent;
#FXML
protected void test(ActionEvent event) {
aContent.getChildren().setAll(FXMLLoader.load("view/ezMovieContent.fxml"));
}
The AnchorPane aContent is the content area in my scene. When i use this code i always get the message on the load() method
The method load(InputStream) in the type FXMLLoader is not applicable for the arguments (String)
Thank you for your help,
With regards, Timo

javafx TitledPane click event only for Title

Is there any way I can have an event that only triggers if I click the Title of a TitledPane?
I have several Nodes in a Graph Editor and currently they are draggable.
But I want them only to drag when i drag the Title not if I click anywhere on the pane.
the mouseClick event seems not to work for me.
Does anyone have suggestions?
Don't set the text on the titled pane, but instead create a label and set it as the graphic for the titled pane. Then you can register a mouse handler with the label:
private TitledPane createClickableTitledPane(String text) {
Label label = new Label(text);
label.setOnMouseClicked(e -> System.out.println("Click on "+text));
TitledPane titledPane = new TitledPane();
titledPane.setGraphic(label);
return titledPane ;
}
StackPane titleRegion = (StackPane) titledPane.lookup(".title");
titleRegion.setOnMouseClicked(System.out::println);
EDIT:
Sometimes titledPane.lookup(".title") returns null which means CSS is not applied to the node. To resolve this issue, you need to use applyCss() and layout() on the pane that contains the TitledPane.
See:
JavaFX TitledPane lookup(.title) returns null

How to save the changes permanently in fxml file while adding menuItems through code

The following piece of code works good as long as the window is open. Menuitem added through code is visible in dropdown as expected
#FXML
public static MenuButton youradd;
#FXML
public void static addingMenuItem()
{
yourAdd.getItems().addAll(new MenuItem(newName));
}
But the issue is , when the particular stage or window is closed, the added menuitem disappers in the dropdown. I want the menuitems added to be stored permanently in the fxml code so that the menuitems are visible everytime i run the stage
NOTE: MenuButton has been created through scenebuilder. but adding MenuItems through code

Resources