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

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 ·          Scatter Chart ·          Candlestick Chart ·          Radar Chart Here we will create Simple Bar Chart with multiple color based on ranging. Ok We start with coding part now. First of all create your project in Android Studio and add gradle dependency for  MPAndroidChart library  into y

JavaFX WebView- Creating Browser Sample

Hi Friends, In this post, i am going to give an overview about JavaFX WebView . This is an embedded browser component which is based on WebKit . If allow you to use Css, JavaScript, HTML5 and more to customise your embedded browser. The embedded browser enables you to perform the following tasks in your JavaFX applications: Render HTML content from local and remote URLs Obtain Web history Execute JavaScript commands Perform upcalls from JavaScript to JavaFX Manage web pop-up windows Apply effects to the embedded browser  I am going to provide and explain you a sample example to create your embedded browser. This is a JavaFX sample example. if you want to take an introduction about JavaFX please visit my previous blog . I have use IntelliJ Idea IDE for this example. You can visit this link  to understand how to create JavaFX application. I am attaching the project structure image below- In this sample- we have two java class. First

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.DatagramPacket ; import java.net.Datag