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

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

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.

java fx printing simple form document

i'm writing simple application in Java Fx which main ability is to print form which i've created in scene builder (some labels plus textfields which can be fill by user and PRINT button in the bottom of page). Do you have any ideas how can i write something like this?I read about PrinterJob class but i'm not sure how to use it properly.Maybe there are better ways to achieve my goal?
Regards
As you have already said PrinterJob is the starting place of JavaFX printing. Just follow the documentation:
https://docs.oracle.com/javase/8/javafx/api/javafx/print/PrinterJob.html
And here are a few more examples:
http://www.programcreek.com/java-api-examples/index.php?api=javafx.print.PrinterJob

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

JavaFX and the use of FXML created in scene builder

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

How to set custom text on buttons in JFace Wizard (Java)

I am using JFace Wizard and I want to set my own text on buttons Next, Back, Finish and Cancel. I found only very old advices which are completely useless today. I also found some solution with external jar files, but I really don't want to add whole library to project only for setting text on 4 buttons...
Is there any reasonable solution?
Thanks in advance
After massive finding and trying, I have to say there is no such way. There are some brutal solutions, but compared with them, adding one jar file to project is much easier and nicer.
I will cite best working solution for me:
You need to download a language pack from here:
http://archive.eclipse.org/eclipse/downloads/drops/L-3.2.1_Language_Packs-200609210945/index.php
NLpack2-eclipse-SDK-3.2.1-gtk.zip works for me while I'm using Eclipse
3.7.2.
Extract org.eclipse.jface.nl2_3.2.1.v200609270227.jar (or other nl for
your language) from the archive and add it to your project. It will be
used automatically.
This do not let you to set texts on buttons, but at least gives you texts translated into your language.
Saw this post. Seems to be the answer to your question.
It basically says to create a dialog using WizardDialog class. Create a class that inherits from Wizard with the implementation of your choice then do below:
WizardDialog wizardDialog = new CustomWizardDialog(shell, new YourWizard());
and then in your CustomWizardDialog do the following:
public class CustomWizardDialog {
#Override
protected void createButtonsForButtonBar(Composite parent) {
super.createButtonsForButtonBar(parent);
Button finishButton = getButton(IDialogConstants.FINISH_ID);
finishButton.setText("FinishButtonText");
Button cancelButton = getButton(IDialogConstants.CANCEL_ID);
cancelButton.setText("CancelButtonText");
}
}
All that is left is to perform wizardDialog.open() to open dialog.

Categories