Creating JavaFX Simple Application


Source Code

application.css
.root {
    -fx-font-size: 14px;
    -fx-font-family: sans-serif;
}
 
 
#btnClear 
{
    -fx-background-color: red;
    -fx-text-fill: white;
}
 
#btnSave 
{
    -fx-background-color: blue;
    -fx-text-fill: white;
}
To download raw file Click Here

Main.java
package application;
 
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
 
public class Main extends Application {
	@Override
	public void start(Stage primaryStage) {
		try {
			Parent root = FXMLLoader.load(getClass().getResource("users.fxml"));
			Scene scene = new Scene(root,1000,600);
			scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
			primaryStage.setScene(scene);
			primaryStage.setTitle("User Managment System");			
			primaryStage.show();
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
 
	public static void main(String[] args) {
		launch(args);
	}
}
 
To download raw file Click Here

userModel.java
package application;
 
public class userModel {
	private Integer id;
	private String name;
	private Integer age;
	private String city;
	public userModel(Integer id, String name, Integer age, String city) {
		this.setId(id);
		this.setName(name);
		this.setAge(age);
		this.setCity(city);
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
 
}
 
To download raw file Click Here

users.fxml
<?xml version="1.0" encoding="UTF-8"?>
 
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
 
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="1000.0" xmlns="http://javafx.com/javafx/16" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.users">
   <children>
      <Label layoutX="33.0" layoutY="23.0" text="User Management System" textFill="#ff3300">
         <font>
            <Font name="Calibri Bold" size="24.0" />
         </font></Label>
      <VBox layoutX="26.0" layoutY="106.0" prefHeight="167.0" prefWidth="100.0" spacing="20.0">
         <children>
            <Label fx:id="lblID" text="ID">
               <font>
                  <Font name="Calibri" size="14.0" />
               </font>
            </Label>
            <Label fx:id="lblName" text="NAME">
               <font>
                  <Font name="Calibri" size="14.0" />
               </font>
            </Label>
            <Label fx:id="lblAge" text="AGE">
               <font>
                  <Font name="Calibri" size="14.0" />
               </font>
            </Label>
            <Label fx:id="lblCity" text="CITY">
               <font>
                  <Font name="Calibri" size="14.0" />
               </font>
            </Label>
         </children>
      </VBox>
      <VBox layoutX="126.0" layoutY="106.0" prefHeight="167.0" prefWidth="230.0" spacing="15.0">
         <children>
            <TextField fx:id="txtID"   disable="true" />
            <TextField fx:id="txtName" />
            <TextField fx:id="txtAge" prefHeight="25.0" prefWidth="214.0" />
            <TextField fx:id="txtCity" />
         </children>
      </VBox>
      <Button fx:id="btnSave" layoutX="33.0" layoutY="276.0" mnemonicParsing="false" onAction="#btnSaveClicked" prefHeight="25.0" prefWidth="76.0" text="Save">
         <font>
            <Font name="Calibri" size="14.0" />
         </font>
      </Button>
      <Button fx:id="btnUpdate" layoutX="115.0" layoutY="276.0" mnemonicParsing="false" onAction="#btnUpdateClicked" prefHeight="25.0" prefWidth="76.0" text="Update">
         <font>
            <Font name="Calibri" size="14.0" />
         </font>
      </Button>
      <Button fx:id="btnDelete" layoutX="199.0" layoutY="276.0" mnemonicParsing="false" onAction="#btnDeleteClicked" prefHeight="25.0" prefWidth="76.0" text="Delete">
         <font>
            <Font name="Calibri" size="14.0" />
         </font>
      </Button>
      <Button fx:id="btnClear" layoutX="281.0" layoutY="276.0" mnemonicParsing="false" onAction="#btnClearclicked" prefHeight="25.0" prefWidth="76.0" text="Clear">
         <font>
            <Font name="Calibri" size="14.0" />
         </font>
      </Button>
      <TableView fx:id="table" layoutX="385.0" layoutY="106.0" onMouseClicked="#tableClicked" prefHeight="430.0" prefWidth="581.0">
        <columns>
          <TableColumn fx:id="colID" prefWidth="75.0" text="ID" />
          <TableColumn fx:id="colName" prefWidth="276.0" text="NAME" />
            <TableColumn fx:id="colAge" prefWidth="66.0" text="AGE" />
            <TableColumn fx:id="colCity" prefWidth="162.0" text="CITY" />
        </columns>
      </TableView>
   </children>
</AnchorPane>
 
To download raw file Click Here

users.java
package application;
import java.sql.*;
import javax.swing.JOptionPane;
 
import java.net.URL;
import java.util.ResourceBundle;
 
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseEvent;
 
 
 
public class users {
 
	//Database Connection String
		Connection con = null;
		PreparedStatement pst;
		ResultSet rs;
		Statement st;
 
 
		public void Connect(){
 
			try {
				Class.forName("com.mysql.cj.jdbc.Driver");
				String url = "jdbc:mysql://localhost:3306/dbjoes?characterEncoding=utf8";
				String username = "root";
				String password = "root";
				con = DriverManager.getConnection(url, username, password);
 
			} catch (Exception e) {
				e.printStackTrace();
 
			}
 
		}
 
 
		public ObservableList<userModel>  getUserList() {
			ObservableList<userModel> userList = FXCollections.observableArrayList();
			String sql = "SELECT ID,NAME,AGE,CITY from users";
			try {
				st = con.createStatement();
				rs = st.executeQuery(sql);
				userModel user;
				while (rs.next()) {
					user = new userModel(rs.getInt("ID"), rs.getString("NAME"), rs.getInt("AGE"), rs.getString("CITY"));
					userList.add(user);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			return userList;
		}
 
 
		//Show User Details
			public void loadData() {
				ObservableList<userModel> list = getUserList();
				colID.setCellValueFactory(new PropertyValueFactory<userModel, Integer>("id"));
				colName.setCellValueFactory(new PropertyValueFactory<userModel, String>("name"));
				colAge.setCellValueFactory(new PropertyValueFactory<userModel, Integer>("age"));
				colCity.setCellValueFactory(new PropertyValueFactory<userModel, String>("city"));
				table.setItems(list);
			}	
 
 
 
 
 
    @FXML
    private Label lblID;
 
    @FXML
    private Label lblName;
 
    @FXML
    private Label lblAge;
 
    @FXML
    private Label lblCity;
 
    @FXML
    private TextField txtID;
 
    @FXML
    private TextField txtName;
 
    @FXML
    private TextField txtAge;
 
    @FXML
    private TextField txtCity;
 
    @FXML
    private Button btnSave;
 
    @FXML
    private Button btnUpdate;
 
    @FXML
    private Button btnDelete;
 
    @FXML
    private Button btnClear;
 
    @FXML
    private TableView<userModel> table;
 
    @FXML
    private TableColumn<userModel, Integer> colID;
 
    @FXML
    private TableColumn<userModel, String> colName;
 
    @FXML
    private TableColumn<userModel, Integer> colAge;
 
    @FXML
    private TableColumn<userModel, String> colCity;
 
    @FXML
    void btnClearclicked(ActionEvent event) {
    	txtID.setText("");
		txtName.setText("");
		txtAge.setText("");
		txtCity.setText("");
    }
 
    @FXML
    void btnDeleteClicked(ActionEvent event) {
    	// Delete Details
		String id = txtID.getText();
		if (!txtID.getText().isEmpty()) {
 
			int result = JOptionPane.showConfirmDialog(null, "Sure? You want to Delete?", "Delete",
					JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
 
			if (result == JOptionPane.YES_OPTION) {
				try {
					String sql = "delete from users where ID=?";
					pst = con.prepareStatement(sql);
					pst.setString(1, id);
					pst.executeUpdate();
					JOptionPane.showMessageDialog(null, "Data Deleted Success");
					btnClearclicked(event);
					loadData();
 
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}
		}
    }
 
    @FXML
    void btnSaveClicked(ActionEvent event) {
    	// Save Details
    			String name = txtName.getText();
    			String age = txtAge.getText();
    			String city = txtCity.getText();
 
    			if (name == null || name.isEmpty() || name.trim().isEmpty()) {
    				JOptionPane.showMessageDialog(null, "Please Enter Name");
    				txtName.requestFocus();
    				return;
    			}
    			if (age == null || age.isEmpty() || age.trim().isEmpty()) {
    				JOptionPane.showMessageDialog(null, "Please Enter Age");
    				txtAge.requestFocus();
    				return;
    			}
    			if (city == null || city.isEmpty() || city.trim().isEmpty()) {
    				JOptionPane.showMessageDialog(null, "Please Enter City");
    				txtCity.requestFocus();
    				return;
    			}
 
    			if (txtID.getText().isEmpty()) {
    				try {
    					String sql = "insert into users (NAME,AGE,CITY) values (?,?,?)";
    					pst = con.prepareStatement(sql);
    					pst.setString(1, name);
    					pst.setString(2, age);
    					pst.setString(3, city);
    					pst.executeUpdate();
    					JOptionPane.showMessageDialog(null, "Data insert Success");
    					btnClearclicked(event);
    					loadData();
 
    				} catch (SQLException e1) {
    					e1.printStackTrace();
    				}
    			}
    }
 
    @FXML
    void btnUpdateClicked(ActionEvent event) {
		// Update Details
		String id = txtID.getText();
		String name = txtName.getText();
		String age = txtAge.getText();
		String city = txtCity.getText();
 
		if (name == null || name.isEmpty() || name.trim().isEmpty()) {
			JOptionPane.showMessageDialog(null, "Please Enter Name");
			txtName.requestFocus();
			return;
		}
		if (age == null || age.isEmpty() || age.trim().isEmpty()) {
			JOptionPane.showMessageDialog(null, "Please Enter Age");
			txtAge.requestFocus();
			return;
		}
		if (city == null || city.isEmpty() || city.trim().isEmpty()) {
			JOptionPane.showMessageDialog(null, "Please Enter City");
			txtCity.requestFocus();
			return;
		}
 
		if (!txtID.getText().isEmpty()) {
			try {
				String sql = "update users set NAME=?,AGE=?,CITY=? where ID=?";
				pst = con.prepareStatement(sql);
				pst.setString(1, name);
				pst.setString(2, age);
				pst.setString(3, city);
				pst.setString(4, id);
				pst.executeUpdate();
				JOptionPane.showMessageDialog(null, "Data Update Success");
				btnClearclicked(event);
				loadData();
 
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}
    }
 
    @FXML
    void tableClicked(MouseEvent event) {
    	userModel user=table.getSelectionModel().getSelectedItem();
		txtID.setText(String.valueOf(user.getId()));
		txtName.setText(user.getName());
		txtAge.setText(String.valueOf(user.getAge()));
		txtCity.setText(user.getCity());
    }
 
    @FXML
    void initialize() {
    	Connect();
    	loadData();
    }
}
 
To download raw file Click Here

dbjoes.sql
CREATE DATABASE IF NOT EXISTS dbjoes;
USE dbjoes;
 
DROP TABLE IF EXISTS users;
CREATE TABLE users (
  ID int(11) NOT NULL AUTO_INCREMENT,
  NAME varchar(50) DEFAULT NULL,
  AGE int(11) DEFAULT NULL,
  CITY varchar(50) DEFAULT NULL,
  PRIMARY KEY (ID)
) ENGINE=InnoDB AUTO_INCREMENT=4;
 
 
INSERT INTO users (ID,NAME,AGE,CITY) VALUES 
 (1,'Ram Kumar',25,'Salem'),
 (2,'Sam Kumar',25,'Salem'),
 (3,'Rakesh',25,'Salem');
 
To download raw file Click Here

Basic Programs



Learn All in Tamil © Designed & Developed By Tutor Joes