Create a Java FX GUI program that allows the user to insert data into a SQL data
ID: 3776401 • Letter: C
Question
Create a Java FX GUI program that allows the user to insert data into a SQL database. The goal of this project is to write a basic Java FX GUI database program. The program should have a list component which on launching the program shows the contents of the database. Also the program should present the user with a form of TextFields for capturing data to insert into a database (use the CPU database you created in previous projects). The form should also have a submit button which will attempt to create a new record in the database. The data should be validated first (numbers, text, blank, etc.). If the data is invalid nothing should be inserted into the database and a warning message should be presented to the user via a Label component. If a record is inserted into the database the List should be updated to include the new record. The report needs to be in the following format: Intel Core i7-6700HQ @ 2.60GHz: $1,509.00 Intel Core i7-3770K @ 3.50GHz: $560.50 Intel Core i5-3570K @ 3.40GHz: $477.23 Intel Core i7-4700MQ @ 2.40GHz: $467.40 SQL database: create database intelprocessors; use intelprocessors; show tables; create table computers(id int not null auto_increment primary key, cpuname varchar( 32 ) not null, performance varchar( 32 ) not null, price float); describe computers;
Data in the csv file:
CPU Name Performance Price (USD) Intel Core i7-3770K @ 3.50GHz 9,556 $560.50 Intel Core i7-3770 @ 3.40GHz 9,327 $335.55 Intel Core i7-3820 @ 3.60GHz 8,990 $404.38 AMD FX-8350 Eight-Core 8,940 $149.99 Intel Core i7-2600K @ 3.40GHz 8,501 $379.97 Intel Core i7-2600 @ 3.40GHz 8,242 $214.99 Intel Core i7-4720HQ @ 2.60GHz 8,046 NA AMD FX-8320 Eight-Core 8,008 $145.99 Intel Core i7-6700HQ @ 2.60GHz 7,997 $1 509 Intel Core i7-4710HQ @ 2.50GHz 7,826 NA Intel Core i5-6600K @ 3.50GHz 7,762 $239.99 Intel Core i7-4700HQ @ 2.40GHz 7,754 $383.00 Intel Core i7-4700MQ @ 2.40GHz 7,736 $467.40 Intel Core i5-4690K @ 3.50GHz 7,690 $239.99 AMD FX-8150 Eight-Core 7,619 $165.99 Intel Core i7-3630QM @ 2.40GHz 7,604 $304.49 Intel Core i5-4670K @ 3.40GHz 7,598 $249.99 Intel Core i5-4690 @ 3.50GHz 7,542 $224.99 Intel Core i7-3610QM @ 2.30GHz 7,460 $399.99 Intel Core i5-4670 @ 3.40GHz 7,342 $226.99 Intel Core i5-4590 @ 3.30GHz 7,174 $199.99 Intel Core i7-4702MQ @ 2.20GHz 7,146 NA Intel Core i5-3570K @ 3.40GHz 7,130 $477.23
This is what I have so far.
//Computer.java
import javafx.beans.property.*;
import java.util.Date;
public class Computer{
private SimpleIntegerProperty id;
private SimpleStringProperty cpu;
private SimpleStringProperty speed;
private SimpleStringProperty price;
public Computer( String c, String s, String p ){
cpu = new SimpleStringProperty( c );
speed = new SimpleStringProperty( s );
price = new SimpleStringProperty( p );
}
/*
public Computer( int i, String c, String s, String p ){
id = new SimpleIntegerProperty( i );
cpu = new SimpleStringProperty( c );
speed = new SimpleStringProperty( s );
price = new SimpleStringProperty( p );
}
public void setID( int id ){
this.id.set( id );
}
public int getID(){
return id.get();
}
*/
public void setCPU( String c ){
cpu.set( c );
}
public String getCPU(){
return cpu.get();
}
public void setSpeed( String s ){
speed.set( s );
}
public String getSpeed(){
return speed.get();
}
public void setPrice( String p ){
price.set( p );
}
public String getPrice(){
return price.get();
}
/*
public StringProperty getPriceProperty(){
return price;
}
public String toString(){
return id + " " + cpu + " " + speed + " " + price;
//return cpu.get();
}
*/
}
-----------------------------------------------------------------------
//CPUTable.java
import javafx.application.*;
import javafx.beans.value.*;
import javafx.collections.*;
import javafx.geometry.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.*;
import java.util.Date;
import javafx.scene.control.cell.*;
import java.sql.Connection;
import java.sql.DriverManager;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import java.sql.*;
import java.util.*;
public class CPUTable extends Application{
private Label response;
private Label heading;
private TextField cpuField;
private TextField speedField;
private TextField priceField;
private Button addBtn;
// The table graphic component
TableView< Computer > CPUTable;
public static void main( String[] args ){
launch( args );
}
public void start( Stage myStage ){
myStage.setTitle( "CPU Table" );
FlowPane rootNode = new FlowPane( 10, 10 );
rootNode.setAlignment( Pos.CENTER );
Scene myScene = new Scene( rootNode, 400, 400 );
myStage.setScene(myScene);
response = new Label( "" );
heading = new Label( "CPU Table" );
// The data structure that is under the table - it alerts the table when
// it is modified
ObservableList< Computer > ComputerList = FXCollections.observableArrayList(
new Computer( "TitleA", "111", "2016" ),
new Computer( "TitleB", "222", "2016" )
);
// The view that is connected to the list.
CPUTable = new TableView< Computer >( ComputerList );
// Sets up a column in the table.
// By using properties with field names that follow the convention
// we can easily connect a field from the datatype to a column in our
// table.
TableColumn< Computer, String > cpuCol = new TableColumn<>( "CPU name" );
cpuCol.setCellValueFactory( new PropertyValueFactory<>( "cpu name" ) );
CPUTable.getColumns().add( cpuCol );
TableColumn< Computer, String > speedCol = new TableColumn<>( "Speed" );
speedCol.setCellValueFactory( new PropertyValueFactory<>( "speed" ) );
CPUTable.getColumns().add( speedCol );
TableColumn< Computer, String > priceCol = new TableColumn<>( "Price" );
priceCol.setCellValueFactory( new PropertyValueFactory<>( "price" ) );
CPUTable.getColumns().add( priceCol );
CPUTable.setPrefWidth( 400 );
CPUTable.setPrefHeight( 200 );
// The selection model is what we need to look at to connect listeners to
// to be able to handle acitons.
TableView.TableViewSelectionModel< Computer > ComputerSelectionModel = CPUTable.getSelectionModel();
ComputerSelectionModel.selectedIndexProperty().addListener(
new ChangeListener< Number >(){
public void changed( ObservableValue< ? extends Number > changed, Number oldValue, Number newValue){
Computer tempComputer = ComputerList.get( newValue.intValue() );
response.setText( "SelectedRow: " + newValue + ", with title: " + tempComputer.getCPU() );
}
}
);
cpuField = new TextField();
speedField = new TextField();
priceField = new TextField();
addBtn = new Button( "Submit" );
addBtn.setOnAction(
new EventHandler< ActionEvent >(){
public void handle( ActionEvent ae ){
ComputerList.add(
new Computer(
cpuField.getText(),
speedField.getText(),
priceField.getText()
)
);
cpuField.clear();
speedField.clear();
priceField.clear();
}
}
);
rootNode.getChildren().addAll( heading, CPUTable, response, cpuField, speedField, priceField, addBtn );
myStage.show();
}
}
Explanation / Answer
}
GUI Front-end with Swing
Swing framework as a JDBC front-end
GUI Front-end with JavaFX
JavaFX framework as JDBC front-end
GUI Front-end with QtJambi
QtJambi framework as a JDBC front-end