Skip to main content

An Example to JavaFX

Hi Friends,
In previous post i was provided you an small introduction to JavaFX. Lets see an example based on JavaFX. Hope you have downloaded and setup IntelliJ Idea IDE.

Note: This example is based on only java classes(Pure java Example).

Setting up the JavaFX project(Gradle based)


  1. Go to File-> New-> Project and click.One popup dialog will appear.
  2. On left panel of dialog, click on gradle.
  3. On right panel you will see some check boxes. Select Java and click Next.
  4. You will asked to fill some text fields.
  5. Enter project's package name in GroupId(i.e com.coderzduniya.fxdemo).
  6. Enter project name in ArtifactId(i.e JavaFX Demo).
  7. Leave Version as it is and go to Next.
  8. Go to Next again and Finish.
  9. After doing it well your project will be created.







  • Now right click on java folder inside src/main and go to New-> JavaFXApplication
  • Enter the class name(i.e Main) and click OK.

Now copy the following code and paste inside your start method(Method start(...) will be created automatically).

I have explained the structure and Code flow in video tutorial.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
 //Base Container
        final GridPane root = new GridPane();

        //Positioning contents
        root.setHgap(15);
        root.setVgap(15);
        root.setAlignment(Pos.CENTER);

        //Creating User name fields
        final Label labelUserName = new Label("UserName:");
        final TextField tfUserName = new TextField();

        //Creating password fields
        final Label labelPassword = new Label("Password:");
        final TextField tfPassword = new TextField();

        //Creating button
        final Button btnOK = new Button("Click Me");
        btnOK.setAlignment(Pos.BOTTOM_RIGHT);

        //Adding contents to the root
        root.add(labelUserName, 0, 0);
        root.add(tfUserName, 1, 0);
        root.add(labelPassword, 0, 1);
        root.add(tfPassword, 1, 1);
        root.add(btnOK, 1, 3);

        //Creating a window on which root will be displayed with provided width and height
        final Scene scene = new Scene(root, 400, 300);

        //Set scene to the stage
        primaryStage.setScene(scene);

        //Set app title
        primaryStage.setTitle("JavaFX Demo");

        //Show/Launch the app
        primaryStage.show();

        //Adding click listener to button
        btnOK.setOnAction(event -> System.out.println("Button clicked!"));


This is a basic example of JavaFX. Hope this will be helpful to you.
Keep reading my blog and send me your suggestions. Thankx :)

Comments

Popular posts from this blog

app-policy

PRIVACY POLICY Last updated April 19, 2023 This privacy notice for Team CoderzDuniya ( " Company ," " we ," " us ," or " our " ), describes how and why we might collect, store, use, and/or share ( " process " ) your information when you use our services ( " Services " ), such as when you: Download and use our mobile application ( Revenue Calculator) , or any other application of ours that links to this privacy notice Engage with us in other related ways, including any sales, marketing, or events Questions or concerns?  Reading this privacy notice will help you understand your privacy rights and choices. If you do not agree with our policies and practices, please do not use our Services. If you still have any questions or concerns, please contact us at droidamar007@gmail.com . SUMMARY OF KEY POINTS This summary provides key points from our privacy notice, but you can find out more details about any of these t...

Java Socket Basics(Socket Programming in Java) Part-2(UDP)

Hi friends, We are going to discuss about UDP Socket Programming . In previous post  we discussed about the differences between TCP & UDP and the sample example of TCP Socket Programming. Below is the sample example of chat application using UDP  Socket Programming . UDP Sample :-  We are going to create an small example which contains two classes. UdpServer.java:-  This is a server class. Means this class will serve the purpose of socket connection. DatagramSocket is the java class and serve the purpose of Server and Client both. The overloaded constructor of DatagramSocket class matters. DatagramPacket is the java class which is responsible to transmit the data/packet over the network from server to client and vice-versa. UdpClient.java:-  This is client class. This serves the purpose of client which will communicate to server and send data to server and receive the data sent by server. UdpServer.java import java.net.DatagramPac...

Working with MPAndroidChart (how to create Bar Chart using MPAndroidChart)

Hi Friends, In this tutorial i am going to show, "How to create Bar Chart using MPAndroidChart". There is a lot of libraries for creating charts in android like AChartEngine, MpAndroidChart, AndroidPlot etc. Your first question may be, Why MPAndroidChart. So MpAndroidChart provides better animation functionality and easy to use in comparision. Using  MPAndroidChart library  we can draw a: ·          Simple Bar Chart ·          Grouped Bar Chart ·          Horizontal Bar Chart ·          Simple Line Chart ·          Line Chart with Cubic Lines ·          Grouped Line Chart ·          Combined Line and Bar Chart ·          Pie Chart ·...