So the generally Idea I have is having a basic JavaFX app. So I make a new project and it has the default stuff such as
#Override
public void start(Stage stage) throws Exception
{
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
and it has the basic button that when you click it prints hello world. So so far all i am trying to do is show you it is a basic default project.
now lets assume I have another class. Lets say that class runs on another thread and that when I type in a name into the console (in my case NetBeans) then it will update the button to say something else.
This is very general, I been googling and trying things with no perfect result. In hindsight, I am trying to make it so, I can have other classes edit the FXML document or elements on the page, then it will show on my program. I am not sure if I have to reload the FXML doc or reload the stage or what. I just wnat a guide or help on how to reference and change stuff around on a JavaFX FXML program. I been working on a chat program and been using normal Javafx with NO FXML and run into problems. So I am thinking about doing it on FXML. but I have times where I a class handle the user names and a class handle the messages for everyone to read and I need these classes to be able to edit the FXML document and reload it to show up. SO, if i user connects to the server, for example, the ListView object will now show that person name. like I said before, I have been able to do this working with Javafx without an FXML file, but my code is like spaghetti. Thanks in advance
Related
I am making an application using only Java and FXML for a school project. I am not allowed to use scene builder. In the appplication, the user enters their info into a form. This info is then used to create an instance of one of three possible classes. One of form's fields is 'Nationality', hence I would like to use a dropdown containing countries for them to pick from. I created a ComboBox in my Main.java class (it was initally in my controller) with the help of MBec's answer to this question: link.
My question is: How do I access the ComboBox I made in Main.java from within my FXML file and display it on my existing scene? I currently have a placeholder ComboBox in there but it is not populated.
Populated ComboBox from Main.java:
ObservableList<String> all_countries = Stream.of(Locale.getISOCountries())
.map(locales -> new Locale("", locales))
.map(Locale::getDisplayCountry)
.collect(Collectors.toCollection(FXCollections::observableArrayList));
final ComboBox<String> country_list = new ComboBox<>(all_countries);
I tried setting an onAction property on an empty ComboBox made in FXML with a method that created and then returned the populated ComboBox from Main.java but as expected this did not work.
I did manage to verify that the ComboBox works by setting it as the root of a new Scene. This was just to ensure myself that the ComboBox itself wasn't the cause of the issue. New scene used to test:
I also tried tried making the ComboBox a different way (see Keyuri Bhanderi's answer here: link), however this also did not work.
Code for my existing scene:
Parent root = FXMLLoader.load(getClass().getResource("view/sample.fxml"));
primaryStage.setTitle("form");
primaryStage.setScene(new Scene(root, 600, 600));
I was expecting to be able to access the ComboBox 'country_list' from within sample.fxml and display it on my existing scene, hence that is my aim. I am new to Java and FXML so the answer may be obvious but I have been stuck on this for a day or two. Apologies for any bad formatting; this is my first time using SO.
If anyone has time to spare I also have an additional question. Is getISOCountries(), the best Locale method to use when asking for nationality? I noticed it had a lot more options than forms tend to do when asking for nationality / country, and it also was not completely in alphabetical order. Thank you all in advance.
I managed to figure it out after browsing some other SO questions regarding similar problems. I made a HBox inside my FXML like so:
<HBox
id="country_container"
fx:id="country_container"
GridPane.columnIndex="1"
GridPane.rowIndex="12"
/>
It is simply there to act as a container for the ComboBox. I then did the following in my Controller's Initialize method:
countries_combo();
combo_box.getChildren().add(country_list);
The first line calls a method which creates and returns the ComboBox and the second adds it as a child to the manually-created HBox.
Here is the problem,
I have a JavaFX application, main display element is a BorderPane.
I use the top of the BorderPane to display buttons, that will act like "navigation" buttons, and the center to display the current chosen application part.
Some solution, described on this link should do the trick :
https://community.oracle.com/thread/2598756
I'm really confused about the code, The only thing I really understand here is that it sets up some kind of changeListener on a list, supposed to hold the "selectedView".
Once the list changes, it triggers a function in the main part of the application, and this fuction SHOULD set the center element display with a new fxml file loaded.
So, I tried to implement quite the same example, only with 2 levels (main -> center) and get nothing working.
Here are the current files I created :
ScreenSwitcher.java
// Main class, loads the "main" fxml component
Main.fxml
// Main fxml component, BorderPane (buttons + center -empty-)
MainController.java
// Main controller, the magic should happen here
PaneOne.fxml & PaneTwo.fxml
// Children fxml, should only be displayed in the main->center element
PanelOneController.java & PaneTwoController.java
// Corresponding controllers
ViewControllerInterface.java
// An interface, as described in the given link explanation
https://github.com/Julo0sS/ScreenSwitcher
First, at app starting, I get this warning :
WARNING: Exception while evaluating select-binding [selectedView]
Then, when clicking on a button to switch the main display, nothing happens... I do have the console showing "CALL TO GOPANEWO" (or GOPANEONE), so it goes into the function code, but seems like the "changeListener" does not work...
Maybe I'm just wrong in the code, maybe there's a better/faster way to achieve what I'm trying, but actually, I'm just stuck on this...
Thanks for reading / help
Update One
Can get a part of the app working now,
The change currentView "event" is sent, and works correctly, WHEN sent by the child view itself (by click on a button in PaneOne, or PaneTwo), but does NOT work when triggered in the main view (in the top part of the borderpane, which is supposed to be a navbar)
Git repo updated with latest modifications...
I made a bit of a mess when coding my program however do not have time to fix it before submission. My program opens up to a 3D molecule viewer for Water, Methane and Methyl, this is done without FXML and simply creates panes, borders, canvas and cameras which you can manipulate to view 360 around the molecule. One button on my side bar switches into an FXML Gui for a chemistry database, in which I want to display my SQL data.
However, once the scene has been switched I cannot switch back. I have attempted to reference the main class in the original starting .java through the FXMLController however it simply says that Handler method is not assesable, I add the #FXML and again nothing fixes. So Instead I attempted to make a public method which calls the main from the same class, this would work other than the fact that it now says that the handler method is not found.
I have searched high and low and cannot seem to find an answer. I am very new to java and javafx and would love to see this project through to the end.(Especially because completing this one simple(or not so apparently) function will complete my project bar loading the SQL database inside the GUI.
I will post a link to the FXML code and the class in which the main class is found.
Thanks
https://gist.github.com/anonymous/753797aeca8f12c8086628b740525625 --> FXMLDocument
https://gist.github.com/anonymous/60c58df1268aa5adeafca8faabf14c4c --> Class that has main(String[] args)
How can I write a file of GUI components in javafx? For Example:
public void start(Stage primaryStage) throws Exception {
TextField userTextField = new TextField();
primaryStage.setScene(new Scene(userTextField));
primaryStage.show();
}
I want to write this code into xml or simple text; how?
Please help me.
You mean, how can you write the view class, that specifies all the elements of a stage/window?
In JavaFX you have two different ways of doing it. FXML (which a lot of people seem to prefer - not me though). And the pure code way.
There's tons of tutorials out there showing you the fxml way and a few (not many - and some bad ones) that show you how to do it in code.
Just google "javafx tutorial" or search on youtube, there is tons of stuff that will get you going
However if you follow my so question here:
https://stackoverflow.com/questions/35338964/need-review-of-minimal-javafx-example-did-i-understand-those-design-patterns-co
You can see a minimal example of a few design patterns and ideas. Lets see if SO tells us, if I did it correctly ;)
I have AnchorPane created with SceneBuilder and corresponding Scene and Stage. AnchorPane contains VBox. All sizes of AnchorPane are USE_COMPUTED_SIZE. Heght of VBox is changed programmicaly, but sizes of Scene and Stage do not change.
How can I make them being autoadjusted to content size?
The start() method of your Application class is passed a Stage object (called stage by the Wizard). If you declare a member variable of type Stage in that class (mStage), then the first thing you do in start() is save it:
mStage = stage ;
You can now do this anytime you need to (in your Application class of course):
mStage.sizeToScene() ;
If you need to do it from your controller class, you can give your controller a reference to your Application (or probably better, have your Application class implement an interface for the controller). Ah, you say, but my Application class doesn't have a reference to the controller, and visa-versa). Well, I've never liked the code that is generated by the wizard, because inflates the controller from the XML file without giving you a reference to it, which makes using the document-view-controller pattern hard. If you want to see how you can get a reference to the controller, see slide 8 (not page 8) of the following. If you further want to instantiate and then specify your own controller (I have a use case for which that is highly desirable) see slides 10 and 11:
https://docs.google.com/viewer?a=v&pid=sites&srcid=ZGVmYXVsdGRvbWFpbnxwc2NoaW1wZjk5fGd4OjVlODJiOTg2OTVlYzky