Source Code : Class mysqlconnect.java package tableviewexample; /** * * @author amir */ import java.sql.*; import javafx.collecti...
Source Code :
Class mysqlconnect.java
package tableviewexample; /** * * @author amir */ import java.sql.*; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javax.swing.*; public class mysqlconnect { Connection conn = null; public static Connection ConnectDb(){ try{ Class.forName("com.mysql.jdbc.Driver"); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/market", "root", ""); JOptionPane.showMessageDialog(null, "Connection Established10"); return conn; }catch (Exception e){ JOptionPane.showMessageDialog(null, e); return null; } } public static ObservableList<Person>getDataPerson(){ Connection con =ConnectDb(); ObservableList<Person>list =FXCollections.observableArrayList(); try { PreparedStatement ps = con.prepareStatement("select * from users"); ResultSet rs = ps.executeQuery(); while(rs.next()){ list.add(new Person(Integer.parseInt(rs.getString("user_id")), rs.getString("username"), rs.getString("lname"), rs.getString("gmail"), rs.getString("yahoo"), rs.getString("phone"), rs.getString("country"))); } } catch (Exception e) { }finally{ try { } catch (Exception e) { } } return list; } } Class FXMLDocumentController.java
package tableviewexample;
import java.net.URL;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
/**
*
* @author amir
*/
public class FXMLDocumentController implements Initializable {
@FXML
private TableView<Person> table;
@FXML
private TableColumn<Person, Integer> id;
@FXML
private TableColumn<Person, String> name;
@FXML
private TableColumn<Person, String> lname;
@FXML
private TableColumn<Person, String> gmail;
@FXML
private TableColumn<Person, String> yahoo;
@FXML
private TableColumn<Person, String> phone;
@FXML
private TableColumn<Person, String> country;
ObservableList<Person>listF;
ObservableList<Person> getProductList(){
ObservableList<Person> pers = FXCollections.observableArrayList();
return pers;
}
int indexM = -1;
Connection conn = null;
ResultSet rs = null;
PreparedStatement pst = null;
/*
ObservableList<Person> list = FXCollections.observableArrayList(
new Person(1, "Amir", "code", "codeamirquestion@gmail.com", "mail@yahoo.com", "0254554444444","USA"),
new Person(2, "Yassin", "code", "ab@gmail.com", "b@yahoo.com", "0254554444444","UK"),
new Person(3, "Adam", "code", "bv@gmail.com", "c@yahoo.com", "0254555544444","Russia"),
new Person(4, "Panda", "code", "d@gmail.com", "d@yahoo.com", "0254554444444","China"),
new Person(5, "Sara", "code", "e@gmail.com", "e@yahoo.com", "0254554444444","France")
);*/
@Override
public void initialize(URL url, ResourceBundle rb) {
id.setCellValueFactory(new PropertyValueFactory<Person,Integer>("id"));
name.setCellValueFactory(new PropertyValueFactory<Person,String>("name"));
lname.setCellValueFactory(new PropertyValueFactory<Person,String>("lname"));
gmail.setCellValueFactory(new PropertyValueFactory<Person,String>("gmail"));
yahoo.setCellValueFactory(new PropertyValueFactory<Person,String>("yahoo"));
phone.setCellValueFactory(new PropertyValueFactory<Person,String>("phone"));
country.setCellValueFactory(new PropertyValueFactory<Person,String>("country"));
//table.setItems(list);
listF = mysqlconnect.getDataPerson();
table.setItems(listF);
}
}
Class TableViewEXample.java
package tableviewexample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
*
* @author amir
*/
public class TableViewEXample 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);
}
}
/*
* 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 tableviewexample;
/**
*
* @author amir
*/
public class Person {
int id;
String name,lname,gmail,yahoo,phone,country;
public void setCountry(String country) {
this.country = country;
}
public String getCountry() {
return country;
}
public Person(int id, String name, String lname, String gmail, String yahoo, String phone, String country) {
this.id = id;
this.name = name;
this.lname = lname;
this.gmail = gmail;
this.yahoo = yahoo;
this.phone = phone;
this.country = country;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setLname(String lname) {
this.lname = lname;
}
public void setGmail(String gmail) {
this.gmail = gmail;
}
public void setYahoo(String yahoo) {
this.yahoo = yahoo;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getName() {
return name;
}
public String getLname() {
return lname;
}
public String getGmail() {
return gmail;
}
public String getYahoo() {
return yahoo;
}
public String getPhone() {
return phone;
}
}
FXMLDocument.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="700.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="tableviewexample.FXMLDocumentController">
<children>
<TableView fx:id="table" layoutX="14.0" prefHeight="400.0" prefWidth="710.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<columns>
<TableColumn fx:id="id" prefWidth="86.0" text="ID" />
<TableColumn fx:id="name" prefWidth="103.0" text="Name" />
<TableColumn fx:id="lname" prefWidth="75.0" text="Last Name" />
<TableColumn prefWidth="174.0" text="Email">
<columns>
<TableColumn fx:id="gmail" prefWidth="75.0" text="gmail" />
<TableColumn fx:id="yahoo" prefWidth="75.0" text="yahoo" />
</columns>
</TableColumn>
<TableColumn fx:id="phone" prefWidth="144.0" text="phone" />
<TableColumn fx:id="country" prefWidth="109.0" text="Country" />
</columns>
</TableView>
</children>
</AnchorPane>
COMMENTS