How do you create a custom popup using fxml in javafx? - java

to create a new release the user would be prompted to click on the plus button in the bottom left of the GUI, when they click on this I want a pane overlayed on top of the center of the GUI allowing users to enter settings and specify options for a new release, I've tried this:
private void addRelease(Event event) throws IOException {
Popup popup = new Popup();
NewReleasePopupController controller = new NewReleasePopupController();
FXMLLoader loader = new FXMLLoader(getClass().getResource("Resources/NewReleasePopup.fxml"));
loader.setController(controller);
loader.load();
popup.getContent().add((Parent)loader.load());
}
however it seems to throw errors while loading, I would like to avoid loading a separate stage if at all possible and would like to have the controller for the popup to be nested within the controller for the main stage.
I tried using the popup class if anyone can help me to get that working or has any better ways to achieve this help would be much appreciated !!
the error:
Caused by: javafx.fxml.LoadException: Controller value already specified.
/D:/DropDayAIO/out/production/DropDayAIO/DropDayAIO/Resources/newReleasePopup.fxml:6
at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2621)
at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:922)
at javafx.fxml/javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:980)
at javafx.fxml/javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:227)
at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:752)
at javafx.fxml/javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2722)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2552)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2435)
at DropDayAIO.HomeSceneController.addRelease(HomeSceneController.java:151)
at DropDayAIO.HomeSceneController.addNewRelease(HomeSceneController.java:128)
... 57 more

Based on the error you mentioned, I think you already have a controller declaration in the FXML file. like with attribute.. fx:controller
Usually while having this attribute declaration in fxml , if you try to set controller again, it throws Caused by: javafx.fxml.LoadException: Controller value already specified
And I believe this should be the case, because you can notice the error is raised at processAttribute method.
javafx.fxml/javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:980)
You can either remove it from the fxml or dont need to set in the code (through setController()). If you say, you want the reference of the controller, you can always get through loader.getController().
And top of that, if you manage to resolve Controller value already specified error, I think next you will encounter Caused by: javafx.fxml.LoadException: Root value already specified.Because you are trying to call the load() method of the FXMLLoader two times. Assign the node to a variable and use that variable to set in popup.
I believe the below code should solve the issue. Give a try!!
private void addRelease(Event event) throws IOException {
Popup popup = new Popup();
FXMLLoader loader = new FXMLLoader(getClass().getResource("Resources/newReleasePopup.fxml"));
Parent parent = (Parent)loader.load(); // I am not sure you need a cast here
newReleasePopupController controller = loader.getController();
popup.getContent().add(parent);
}
Suggestion: Please use proper naming conventions of class.

Related

Setting stage icon

My first intention was to set TextInputDialog icon. But I started from setting stage icon. I saw couple SO questions with marvelous answers containing usually 2 lines of code.
First I tried to put this icon to /resources/icons but exception "Invalid URL or resource not found" appeared. To be sure I don't make any mistake writing file path I moved this icon to /source/sample directory. I use code (I will post whole code):
public void start(Stage stage) throws Exception {
FXMLLoader loaderModyfikacjaKonfiguracji = new FXMLLoader(getClass().getResource("FXMLModyfikacjaKonfiguracji.fxml"));
Parent root = loaderModyfikacjaKonfiguracji.load();
stage.setTitle("Modyfikacja konfiguracji");
Image image = new Image("file:icon.png");
//stage.getIcons().removeAll();
stage.getIcons().add(image);
ControllerModyfikacjaKonfiguracji controllerModyfikacjaKonfiguracji = loaderModyfikacjaKonfiguracji.getController();
stage.setScene(new Scene(root, 510, 700));
stage.show();
}
Everywhere it looks so simple to set icon. I also tried .jpg. not using file: throws exception, using file: compiles but I see no effect of changed icon. What am I doing wrong or where is the problem?
I've successfully used this to set an icon before
primaryStage.getIcons().add(new Image(getClass().getResourceAsStream("AppIcon.png")));
In my case, the application fxml file and AppIcon.png are in the same directory.
If you dont want to go that route, i would suggest trying
Image image = new Image("file:./icon.png");
But that's a guess.

How to use (browser)autocomplete with Vaadin 10 TextField

I try to build an reservation Form with vaadin 10 while building it i encounterd the problem, that the autocompletion we know from every form on te web doesn't work. I put in an name field the name press submit an the next time i want to re-enter the name i need to write it out again.
My code looks like that (shortend):
TextField name = new TextField();
Button save = new Button("submit");
save.addClickListener(event -> save());
name.setAutocomplete(Autocomplete.ON);
add(name);
add(save);
i had the hopes that Autocomplete.On does the magic for me but it seems not to work. Maybe the way the save methode works screw things up?
the methode is rather big i just simplify it
private void save() {
--save everything to db
--remove all fields
--replace the fields with text saying reservation done
}
found out that someone created issue https://github.com/vaadin/vaadin-text-field/issues/156
seems to be an Shadow DOM limitation
Related issues:
https://bugs.chromium.org/p/chromium/issues/detail?id=746593
https://bugs.webkit.org/show_bug.cgi?id=172567
Edit:
for auto completion for my loginForm i got it working with adding
class xyz extends Div implements PageConfigurator{
...
#Override
public void configurePage(InitialPageSettings settings) {
settings.addInlineWithContents(
InitialPageSettings.Position.PREPEND,
"window.customElements=window.customElements||{};
window.customElements.forcePolyfill=true;
window.ShadyDOM={force:true};",
InitialPageSettings.WrapMode.JAVASCRIPT);
}
I came across this issue recently with Vaadin 14 and a custom login form.
Chrome only offers auto-filling fields (and also save login details) if it can see the input tags with name attributes in the Light DOM, but Vaadin creates TextFields with all of its elements inside of a Shadow DOM hidden.
Solution is to create a reference with <input slot="input"> inside the parent <vaadin-text-field> as part of Light DOM. All the styles and everything will still be in the Shadow DOM but Chrome now can see the input fields for auto-completion.
Kotlin code:
val username = TextField().apply {
element.setAttribute("name", "username")
element.appendChild(Element("input").setAttribute("slot", "input"))
}
val password = PasswordField().apply {
element.setAttribute("name", "password")
element.appendChild(Element("input").setAttribute("slot", "input"))
}
add(username, password)

JavaFX lssue when loading fxml file and seting its Label text

I made a similar question yesterday but I think it wasnt well explained, so I wanted to ask it again but with some changes I made in my code. I apologise if i write too much but i want to make everything understandable.
So, Im making a pokemon simulator where you can catch and train pokemon.
I have one main fxml file which contains buttons to access to the diferent fxml files like catch, battle, shop, bag...
Yesterday i was doing the bag where all your items will be stored.
Everything is working fine and the windows are switching between them properly. The problem comes when i was trying to add a Label to each item in the bag, which was suposed to show the user the cuantity he has of each item. So i created all the Labels with no text so they are empty.
They will get filled from the information i get from a database, this thing also works properly, i connect to the db and get the item cuantity. The problem comes when i want to show that item cuantity in my bag window.
Why? Because as you can imagine, i want that when you click on the bag button, the bag file loads with all the Labels filled with the cuantity of each item. The Labels are defined on the bag fxml controller, so if i want to fill them with some text, i cant do it from my main windows which uses another controller, i need to do it throught the bag controller.
This is the code i tried to make it work(Located in main controller):
#FXML
void mochila (ActionEvent event) throws IOException, SQLException {
AnchorPane pane = FXMLLoader.load(getClass().getResource("mochila.fxml"));
anchorPaneContent.getChildren().setAll(pane);
anchorPane2.setStyle("-fx-background-color: #3f3f3f;");
cm.getCantidad();
}
getCantidad is a function that i have in my bag controller and this is it:
public void getCantidad() {
lblpokeballCount.setText("Cantidad: "+pokeballcount);
lblsuperballCount.setText("Cantidad: "+superballcount);
lblultraballCount.setText("Cantidad: "+ultraballcount);
lblmasterballCount.setText("Cantidad: "+masterballcount);
}
So when i try to run this function from the main controller, it returns me null pointer exception. That means the labels are not initialized but when i type first AnchorPane pane = FXMLLoader.load(getClass().getResource("mochila.fxml"));Shoudlnt all the resources from the file be loaded?
Because i creaded a button in my bag file, when on clicked runs the same function, and it works correctly because im calling it from the same controller/file.
So now i dont really know what to do, this is a proyect for school but my programing teachers have never touched javafx so they dont even know what im doing. You are my only hope. I tried to understand this post: post
But i dont understand it at all as im new to all this stuff. So guys if you can help me i would be really gratefull, thanks!
edit:
#FXML
void mochila (ActionEvent event) throws IOException, SQLException {
FXMLLoader loader = new FXMLLoader(getClass().getResource("mochila.fxml"));
anchorPaneContent.getChildren().setAll(loader);
controladorMochila controller = loader.<controladorMochila>getController();
controller.getCantidad();
}
anchorPaneContent is an anchorpane thats inside the main pane. All the buttons are in the main pane, and depending on the button you click, anchorpanecontent will change for another fxml file. I tried to do it like in the post i mentioned above. But i cant do anchorPaneContent.getChildren().setAll(loader); because it says: the node setAll is not aplicable for the arguments(FXMLLoader)
You are trying to add an FXMLLoader to an anchor pane, which will not work, since an FXMLLoader is not a visual component (it is a thing that loads FXML files).
Additionally, you are trying to get the controller from the FXMLLoader without actually loading the FXML file; this won't work because the controller class is specified in the FXML file (so the FXMLLoader doesn't know what kind of controller to create until it loads the file).
You need to load the FXML file and add the result to the anchor pane:
#FXML
void mochila (ActionEvent event) throws IOException, SQLException {
FXMLLoader loader = new FXMLLoader(getClass().getResource("mochila.fxml"));
anchorPaneContent.getChildren().setAll(loader.load());
controladorMochila controller = loader.<controladorMochila>getController();
controller.getCantidad();
}
or, if you want to be a bit more explicit:
#FXML
void mochila (ActionEvent event) throws IOException, SQLException {
FXMLLoader loader = new FXMLLoader(getClass().getResource("mochila.fxml"));
Parent pane = loader.load();
anchorPaneContent.getChildren().setAll(pane);
controladorMochila controller = loader.<controladorMochila>getController();
controller.getCantidad();
}

Handling different users in JavaFX

I am learning Java and JavaFX. I have an application that has some ComboBox components that are only visible if logged in as an admin My problem is that the application populates the Combobox with system variables when it starts, If a user logs in then i get a null pointer and the application doesnt start because the ComboBox doesnt exist.. When an admin logs in the application starts correctly. Here is how i am trying to get it to work.
private void loginpressed(ActionEvent event) throws IOException
{
if (BCrypt.checkpw(userId.getText() + passwordfield.getText(), passwordhashuser))
{
Parent root = FXMLLoader.load(getClass().getResource("LaserControllerUserUI.FXML"));
Scene home_page_scene = new Scene(root);
Stage app_stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
app_stage.setScene(home_page_scene);
app_stage.show();
}
else if (BCrypt.checkpw(userId.getText() + passwordfield.getText(), passwordhashadmin))
{
Parent root = FXMLLoader.load(getClass().getResource("LaserControllerUI.FXML"));
Scene home_page_scene = new Scene(root);
Stage app_stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
app_stage.setScene(home_page_scene);
app_stage.show();
}
else{
errorMessage.setText("Login Incorrect!");
System.out.print("false");}
}
So i have two seperate FXML depending on who logs in. How do i handle this?
In general, you should avoid making your application in a way in which it will need administrative privileges, if it does need these privileges you will need to give this feedback to the user, or enforce this condition, you can google how to do this such as in this link for Microsoft: https://technet.microsoft.com/en-us/library/ff431742.aspx. Note that you can have access to the current user through the System properties in Java, and you can also save properties based on a user as well, if that helps.
That casting your doing completely unnecessary. I would look up some GUI tutorials on MVC for Java if I was you. Once you learn this pattern you will have a light bulb moment and everything will just make sense. There are alterations of this pattern you should learn the main ones.
Not following any GUI patterns will cause you immense pain and complications. I would estimate over 100,000 questions on SO would not exist if people knew this pattern. This includes your question. GUI and functionality should be completely separated. Your question would simply be: how do I get a list of all Users, which would be a duplicate and not need to be asked.
I would use one FXML Document. If they have pretty much the same components, I would set the locations and visibility of the components after a user logs in. What is set to visible should depend on the users account type.

Switching Scenes with several FXML files

I am new to Java FX and trying to build up an "easy" application which consists of a header button bar and a "content area" below.
I've managed a big part like that:
MainWindow.fxml with a Borderpane: MenuBar in the Top Area
And a Pane with fx:id: content in the center area.
Several X.fxml files for the content ()
One Controller which creates the obj content:
#FXML
private Pane content;
and switches the content:
content.getChildren().clear();
content.getChildren().add(FXMLLoader.load(getClass().getResource("Navi.fxml")));`
Main file which initializes the parent and scene:
Parent root = FXMLLoader.load(getClass().getResource("MainWindow.fxml"));
Stage Window = primaryStage;
Scene scene = new Scene(root);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
Window.setTitle("ICIS - In Car Interactive System");
Window.setScene(scene);
Window.show();`
So far everything works fine!
But now I want to apply a SplitPane and do the "content change" in each side of the Split Pane (TwoWindows.fxml): So I've extended the
Controller with obj. for every Pane of the Split Pane, assigned the fx:id to that pane and want to control them analog to the example before.
#FXML
private Pane SecondWindow1;
SecondWindow1.getChildren().add(FXMLLoader.load(getClass().getResource("Navi.fxml")));
Well, during compilation everything is fine, but while running I get the Null error exception, so that I assume SecondWindow1 (left half of SlitPane) is not known to the controller.
I also assume, it is because I initialize at the beginning only MainWindow.fxml (which includes the content area) but not the TwoWindows.fxml (SplitPane) which inlcude the SecondWindow1 Object.
Well I've tried since hours now to solve it, but apparently I am overlooking sth. Somebody knows how to fix that problem? Do I need one Controller for every FXML File?

Categories