| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
In this guide, you will learn how to build an executable JavaFX desktop application and later can run it without IDE by just clicking .exe file.
The problem with JavaFX is that Oracle is ruining this awesome tool by making it harder to execute on JDK 11, but referencing this link I have just chosen to download JDK 8 since it is building JavaFX without any issues.
To do that go there and download a particular version for your OS
Go to the site and download the app.
Go to the site and download JavaFX Windows SDK. Then you unzip in your chosen directory. You will source this for each JavaFX project. (Unfortunately each time the same steps).
--module-path \javafx-sdk-11.0.2\lib --add-modules javafx.controls,javafx.fxmlInstead of \javafx-sdk-11.0.2\lib you need to set your own directory of JavaFX lib folder.
For the sake of simplicity, I will build a simple currency converter app which will use API.

You now can preview the app, go to [Preview] -> [Show Preview in Window] or simply Ctrl+P Now steps to use this template in our app as well as set-up IDs for further uses.
package sample;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.text.Text;
public class Controller {
@FXML
private ResourceBundle resources;
@FXML
private URL location;
@FXML
private TextField amountField;
@FXML
private TextField ccyOne;
@FXML
private TextField ccyTwo;
@FXML
private Button applyBtn;
@FXML
private Text rateCcy;
@FXML
private Text conversionTotal;
@FXML
private Text dateStamp;
@FXML
void initialize() {
assert amountField != null : "fx:id=\"amountField\" was not injected: check your FXML file 'sample.fxml'.";
assert ccyOne != null : "fx:id=\"ccyOne\" was not injected: check your FXML file 'sample.fxml'.";
assert ccyTwo != null : "fx:id=\"ccyTwo\" was not injected: check your FXML file 'sample.fxml'.";
assert applyBtn != null : "fx:id=\"applyBtn\" was not injected: check your FXML file 'sample.fxml'.";
assert rateCcy != null : "fx:id=\"rateCcy\" was not injected: check your FXML file 'sample.fxml'.";
assert conversionTotal != null : "fx:id=\"conversionTotal\" was not injected: check your FXML file 'sample.fxml'.";
assert dateStamp != null : "fx:id=\"dateStamp\" was not injected: check your FXML file 'sample.fxml'.";
}
}Firstly, let's quickly fix Main.java:
Now we can go to Contoller.java and do all work there.
There you could see the initialize(). From this method we can drop warnings and rewrite the code to check is our template is functional or not. Let’s just add sout command to get output in the console by clicking the button:
@FXML
void initialize() {
applyBtn.setOnAction(event -> {
System.out.println("IT WORKS!");
});
}You might notice the red warning. This is because of compitability issues of JavaFX. Just go to .fxml file in AnchorPane and replace xmlns="http://javafx.com/javafx/15.0.1" to which version it asks (xmlns="http://javafx.com/javafx/11.0.2")
Next let's install JSON jar from here or here. Store it in any directory and go to [File] -> [Project Structure…] or Ctr+Alt+Shift+S -> [Libraries] -> [New Project Library (+) sign] -> [Java] -> [Choose jar file] -> [Press okay]
you actually can do any other API if you like. I choose first link in google. I won't explain how to set-up - it is easy. But once you logged in go to how to start and find the api link with your assigned token like i did http://data.fixer.io/api/latest?access_key=27ad197ccf98251c14ebc0b13134de39&symbols=USD
Even if you click this link it will open JSON object in your browser, so we can now use it in our application.
Before we need to build a class getUrlContent which will buffer data from the GET.
private static String getUrlContent(String urlAddress) {
StringBuffer content = new StringBuffer();
try {
URL url = new URL(urlAddress);
URLConnection urlConn = url.openConnection();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
String line;
while ((line = bufferedReader.readLine()) != null) {
content.append(line + "\n");
}
bufferedReader.close();
} catch (Exception e) {
System.out.println("Error in conversion");
}
return content.toString();
}Now in the method initialize() we can build up our parser and output insted of sout function:
@FXML
void initialize() {
applyBtn.setOnAction(event -> {
String output = getUrlContent("http://data.fixer.io/api/latest?access_key=" +
"27ad197ccf98251c14ebc0b13134de39&symbols="+ccyOne.getText().toUpperCase()+","+ccyTwo.getText().toUpperCase());
if(!output.isEmpty()) {
JSONObject obj = new JSONObject(output);
dateStamp.setText(obj.getString("date"));
double ccyTwoValue = obj.getJSONObject("rates").getDouble(ccyTwo.getText()) /
obj.getJSONObject("rates").getDouble(ccyOne.getText());
String ccyTwoStr = String.format("%.2f", ccyTwoValue) + " " + ccyTwo.getText();
rateCcy.setText(ccyTwoStr);
double amount = Double.parseDouble(amountField.getText());
double total = ccyTwoValue * amount;
String totalStr = String.format("%.2f", total) + " " + ccyTwo.getText();
conversionTotal.setText(totalStr);
}
});
}And finally if it works then you can convert any currency and amount you would like.
If everything works then we can finally build our application as .exe file to use it without IDE.
If there is no errors it should create in out.artifacts folder bundle. Go there and you will the last folder where is .exe file. You can move this folder out of directory and it can works without IDE.
| Back | FazBrowse Home | New Git URL |