JavaFx Using Scene Builder and With JDBC Connectivity
JavaFx Using Scene Builder and With JDBC Connectivity
store in the path as suggested by the system and make note of the path where the
scene builder is downloaded.
Step 2: Open netbeans- javafx-> javafxml application - >give the project name
and let the project gets opened
Step 4: The fxml project looks as given below- let us take the hospital system
Step 5: After double click on the FXMLDocument.fxml, the scene builder opens
Step 6: Design and give the id for all the usable controls with the handler
Step7: Once the scene builder is designed and id and handler names are given
the following files gets updated by clicking "Make controller" in
FXMLDocument.fxml.
FXMLdocument.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ChoiceBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
FXMLDocumentController.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package hospitalfxml;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
/**
*
* @author admin
*/
public class FXMLDocumentController implements Initializable {
ObservableList disease = FXCollections.observableArrayList();
@FXML
private Label label;
@FXML
private TextField t1;
@FXML
private TextField t2;
@FXML
private ChoiceBox<String> choicebox;
@FXML
private Button b1;
@FXML
private Button b2;
@Override
public void initialize(URL url, ResourceBundle rb) {
additems();//mandatory to intialize the controls of choicebox.
}
public void additems()//user created method to initalize the choice box items
{
String d1="Fever";
String d2="Hair fall";
String d3 = " Cough";
String d4="Viral fever";
disease.addAll(d1,d2,d3,d4); For jdbc connection
choicebox.getItems().addAll(disease); for the button "insert
record"
}
@FXML
private void handleButtonActionb1(ActionEvent event) {
String a = t1.getText();// text filed value
String b = t2.getText(); //text field value
String c = (choicebox.getValue()).toString();//choice values
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/hospital","root",""
);
Statement s = con.createStatement();
String s1 = "insert into patient( patientid,patientname,disease) values
('"+a+"','"+b+"','"+c+"');";
System.out.println("sucess");
s.executeUpdate(s1);
}catch(Exception e){System.out.println(e);}
}
@FXML
private void handleButtonActionb2(ActionEvent event) {
System.exit(0);
}
}
FOR JDBC CONNECTIVITY KINDLY GO THROUGH THE "STEPS FOR
JDBC ONNECTIVITY"MANUAL
Hospitalfxml.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package hospitalfxml;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
*
* @author admin
*/
public class Hospitalfxml extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root =
FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
Mysql database: