Build Desktop application software using Java Tutorial | Sample Examples -Part 1

By | September 14, 2016

Today  we will use Java for developing cross-platform applications/softwares which can be run on Windows, Mac os, Linux etc.  You need to install Java Runtime Environment in the Operating System where you want to run your java based software application.

PreRequisite:

  • Basic knowledge of Java Programming.
  • Basic understanding of Object Oriented Programming.
  • JDK8 installed on working Operating System. Download if not from here.
  • Eclipse IDE

As you are already familiar with Java/ C++ Programming, you might have seen that those applications run in a console that looks like cmd / terminal etc. But now we will see how to develop an application which has a graphical user interface using Java.

All the programming goes same as the normal console app which you might have been already familiar with except for the GUI part. We will use JavaFX for integrating graphical UI in our java based software.

What is JavaFX?

JavaFX is a java library which provides APIs used for developing UI elements in a java application similar to Swing API. JavaFX is the latest library which has solved the problem of bad looking applications or say which has replaced the old windows 98 style look of java based softwares with a highly advanced client rich application’s look and feel. The apps developed using JavaFX can be given beautiful interface with as ease as building a website. It also supports CSS!  You can visit this for more information about JavaFX.

Table of Contents

  • Creating A Simple Window
  • Handle User Events
  • Anonymous Inner Classes And Lambda Expressions
  • Switching Scenes
  • Creating Alert Boxes
  • Communicating Between Windows
  • GridPane
  • Extract And Validate Input
  • CheckBoxes
  • ChoiceBox DropDown List
  • ComboBox
  • ListViews
  • TreeView
  • TableView
  • TableView Adding And Deleting
  • Menus
  • CSS
  • Properties
  • Binding
  • Fxml

Lets Begin..

Creating A Simple Window

We will be using eclipse IDE for this tutorial. So create a new Java Project in eclipse and name it anything.

Sample JavaFx App

Please note that in the JRE section of New Java Project Window, you have to set JAVA SE 8 or above as specified in the above screenshot.

Now go to the src folder of your project and create a new class named Main.Java with the following content :

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class Main extends Application {

    Stage window;
    Button button;

    public static void main(String[] args) {
        launch(args); // It calls start method defined bellow...
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        window = primaryStage;
        window.setTitle(" TutorialsFace - JavaFX");
        // Creating a simple UI button
        button = new Button("Click me");
        // StackPane is a type of layout which we will look at later..
        StackPane layout = new StackPane();
     
        //Adding the button to the layout
        layout.getChildren().add(button);

        // Create scene with resolution of 300x250 and add layout to it...
        Scene scene = new Scene(layout, 300, 250);

        // Add scene to the stage i.e to the window
        window.setScene(scene);
        window.show();
    }
}

Now right click on the Main.java and run as Java Application.

layout JAVAFX

The Scene and Stage can be understood in the screenshot above.

So this is the very basic application with a Graphic User Interface which does nothing :p

We will see the rest tutorial in the next part.

1,901 total views, 1 views today

(Visited 1,095 times, 2 visits today)