JavaFx using Scene builder and with JDBC connectivity
1. Step 1 : Download Scene builder
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 3: Now link the scene builder to netbeans by
selecting tools menu -> options-> java->javafx-> browse the path -> apply and
click OK
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
Similarly do for all controls and design as above
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?>
<AnchorPane id="AnchorPane" prefHeight="390.0" prefWidth="419.0"
xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.171"
fx:controller="hospitalfxml.FXMLDocumentController">
<children>
<Label fx:id="label" layoutX="126" layoutY="120" minHeight="16"
minWidth="69" />
<Label layoutX="30.0" layoutY="30.0" text="Hospital Management
System">
<font>
<Font size="25.0" />
</font>
</Label>
<VBox layoutX="30.0" layoutY="77.0" prefHeight="91.0"
prefWidth="100.0">
<children>
<Label text="Patient id" />
<Label text="Patient Name" />
<Label text="Disease" />
</children>
</VBox>
<VBox layoutX="216.0" layoutY="68.0" prefHeight="91.0"
prefWidth="100.0">
<children>
<TextField fx:id="t1" />
<TextField fx:id="t2" />
<ChoiceBox fx:id="choicebox" prefWidth="150.0" />
</children>
</VBox>
<HBox layoutX="61.0" layoutY="202.0" prefHeight="100.0"
prefWidth="299.0">
<children>
<Button fx:id="b1" mnemonicParsing="false"
onAction="#handleButtonActionb1" text="Insert record" />
<Button fx:id="b2" mnemonicParsing="false"
onAction="#handleButtonActionb2" text="Exit" />
</children>
</HBox>
</children>
</AnchorPane>
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;
private void handleButtonAction(ActionEvent event) {
System.out.println("You clicked me!");
label.setText("Hello World!");
}
@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:
Database name : hospital
Table name : patient
Thus the Javafx with jdbc connectivity is established to store the data from
javafx to My-SQL