JavaFX and the use of FXML created in scene builder - java

I have been trying to figure out how to use two FXML files and their controllers at the same time in a program, but have found it difficult to find a simple example.
Would someone please demonstrate the use of Sample.fxml and Sample1.fxml at the same time, to where both are displayed. If you would be able to demonstrate this in the simplest and easiest possible way for a new Java and JavaFX programer to understand, I would be very great full.
Thanks.

You can solve this problem by adding both the fxml files into a single group, and then you have to add the group to the scene. It is a little more work if you want to make everything look polished, but this the most simple way to do this. Just be sure your controllers are defined in the fxml files.
GridPane root = new GridPane();
root.add((Node)(FXMLLoader.load(getClass().getResource("sample1.fxml"))) , 1 , 1);
root.add((Node)(FXMLLoader.load(getClass().getResource("sample2.fxml"))) , 1 , 2);
primaryStage.setTitle("Two For One Special");
primaryStage.setScene(new Scene(root, 800, 600));
primaryStage.show();

Related

How do I display and access a ComboBox made in a Java class in my FXML?

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.

JavaFX - Remove Element from SwingNode

Suppose I've done this -
SwingNode deviceView = new SwingNode();
JDesktopPane desktop = new JDesktopPane();
// Add elements to desktop
deviceView.setContent(desktop);
Is there a way remove desktop from deviceView? The best way I can come up with is -
deviceView.setContent(new JPanel());
But is there a way to simply remove it?
As noted in the comments I've tried deviceView.setContent(null); which appears to be bugged.
You may try this out and let us know how it goes, although I have not tried it out.
deviceView.getContent().removeAll();
Late to the party but here is what you could try.
deviceView.getContent().removeAll(); /****Note : The UI may not show any updates even if you run it inside the SwingUtilities.invokeLater method */
/**** Now set a new instance that would allow you to set any other content later. At least that is what i have done in one of our Java FX App.*/
deviceView = new SwingNode();

How to write to a file in Java of Javafx GUI componets?

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

JavaFX: Working with elements within another class

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

How to destroy and recreate a scene in andengine correctly?

In andAngine I need to destroy a Scene in andangine and to recreate it in order to restart the game variables and listeners and gamelogic.
i use this code:
scene.detachChildren();
scene.clearEntityModifiers();
scene.clearTouchAreas();
scene.clearUpdateHandlers();
System.gc();
thisengine.setScene(menuscene);
and then I recreate the scene
scene = new Scene();
scene.dosomestuff
thisengine.setScene(scene);
Something seems to go wrong when I recreate the third time the scene. Sprites doesn't display..are distorted or something doesn't display at all. Can anyone explain to me if I correctly initialize and destroy the scene ?
Personally, I would create the scene once the first time it is used.
To change the scene, do your removal stuff as you've shown, I wouldn't bother with the call to System.gc(), and then instead of creating a new Scene() - just call scene.reset(), scene.dosomestuff, etc
Creating a new Scene like you show looks like a major memory leak, or at least a possible leak.

Categories