Class FXMLDocumentControlle.java package tableviewusers; import java.net.URL; import java.sql.Connection; import java.sql.PreparedStatem...
Class FXMLDocumentControlle.java package tableviewusers; 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.collections.transformation.FilteredList; import javafx.collections.transformation.SortedList; import javafx.fxml.FXML; import javafx.fxml.Initializable; 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.KeyEvent; import javafx.scene.input.MouseEvent; import javax.swing.JOptionPane; /** * * @author amir */ public class FXMLDocumentController implements Initializable { @FXML private TableView<users> table_users; @FXML private TableColumn<users, Integer> col_id; @FXML private TableColumn<users, String> col_username; @FXML private TableColumn<users, String> col_password; @FXML private TableColumn<users, String> col_email; @FXML private TableColumn<users, String> col_type; @FXML private TextField txt_username; @FXML private TextField txt_password; @FXML private TextField txt_email; @FXML private TextField txt_type; @FXML private TextField txt_id; @FXML private TextField filterField; ObservableList<users> listM; ObservableList<users> dataList; int index = -1; Connection conn =null; ResultSet rs = null; PreparedStatement pst = null; public void Add_users (){ conn = mysqlconnect.ConnectDb(); String sql = "insert into users (username,password,email,type)values(?,?,?,? )"; try { pst = conn.prepareStatement(sql); pst.setString(1, txt_username.getText()); pst.setString(2, txt_password.getText()); pst.setString(3, txt_email.getText()); pst.setString(4, txt_type.getText()); pst.execute(); JOptionPane.showMessageDialog(null, "Users Add succes"); UpdateTable(); search_user(); } catch (Exception e) { JOptionPane.showMessageDialog(null, e); } } //////// methode select users /////// @FXML void getSelected (MouseEvent event){ index = table_users.getSelectionModel().getSelectedIndex(); if (index <= -1){ return; } txt_id.setText(col_id.getCellData(index).toString()); txt_username.setText(col_username.getCellData(index).toString()); txt_password.setText(col_password.getCellData(index).toString()); txt_email.setText(col_email.getCellData(index).toString()); txt_type.setText(col_type.getCellData(index).toString()); } public void Edit (){ try { conn = mysqlconnect.ConnectDb(); String value1 = txt_id.getText(); String value2 = txt_username.getText(); String value3 = txt_password.getText(); String value4 = txt_email.getText(); String value5 = txt_type.getText(); String sql = "update users set user_id= '"+value1+"',username= '"+value2+"',password= '"+ value3+"',email= '"+value4+"',type= '"+value5+"' where user_id='"+value1+"' "; pst= conn.prepareStatement(sql); pst.execute(); JOptionPane.showMessageDialog(null, "Update"); UpdateTable(); search_user(); } catch (Exception e) { JOptionPane.showMessageDialog(null, e); } } public void Delete(){ conn = mysqlconnect.ConnectDb(); String sql = "delete from users where user_id = ?"; try { pst = conn.prepareStatement(sql); pst.setString(1, txt_id.getText()); pst.execute(); JOptionPane.showMessageDialog(null, "Delete"); UpdateTable(); search_user(); } catch (Exception e) { JOptionPane.showMessageDialog(null, e); } } public void UpdateTable(){ col_id.setCellValueFactory(new PropertyValueFactory<users,Integer>("id")); col_username.setCellValueFactory(new PropertyValueFactory<users,String>("username")); col_password.setCellValueFactory(new PropertyValueFactory<users,String>("password")); col_email.setCellValueFactory(new PropertyValueFactory<users,String>("email")); col_type.setCellValueFactory(new PropertyValueFactory<users,String>("type")); listM = mysqlconnect.getDatausers(); table_users.setItems(listM); } @FXML void search_user() { col_id.setCellValueFactory(new PropertyValueFactory<users,Integer>("id")); col_username.setCellValueFactory(new PropertyValueFactory<users,String>("username")); col_password.setCellValueFactory(new PropertyValueFactory<users,String>("password")); col_email.setCellValueFactory(new PropertyValueFactory<users,String>("email")); col_type.setCellValueFactory(new PropertyValueFactory<users,String>("type")); dataList = mysqlconnect.getDatausers(); table_users.setItems(dataList); FilteredList<users> filteredData = new FilteredList<>(dataList, b -> true); filterField.textProperty().addListener((observable, oldValue, newValue) -> { filteredData.setPredicate(person -> { if (newValue == null || newValue.isEmpty()) { return true; } String lowerCaseFilter = newValue.toLowerCase(); if (person.getUsername().toLowerCase().indexOf(lowerCaseFilter) != -1 ) { return true; // Filter matches username } else if (person.getPassword().toLowerCase().indexOf(lowerCaseFilter) != -1) { return true; // Filter matches password }else if (person.getType().toLowerCase().indexOf(lowerCaseFilter) != -1) { return true; // Filter matches password } else if (String.valueOf(person.getEmail()).indexOf(lowerCaseFilter)!=-1) return true;// Filter matches email else return false; // Does not match. }); }); SortedList<users> sortedData = new SortedList<>(filteredData); sortedData.comparatorProperty().bind(table_users.comparatorProperty()); table_users.setItems(sortedData); } @Override public void initialize(URL url, ResourceBundle rb) { UpdateTable(); search_user(); // Code Source in description } }
***********************************************************************************************
Class mysqlconnect.java
package tableviewusers; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javax.swing.JOptionPane; /** * * @author amir */ public class mysqlconnect { Connection conn = null; public static Connection ConnectDb(){ try { Class.forName("com.mysql.jdbc.Driver"); Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/market","root",""); // JOptionPane.showMessageDialog(null, "Connection Established"); return conn; } catch (Exception e) { JOptionPane.showMessageDialog(null, e); return null; } } public static ObservableList<users> getDatausers(){ Connection conn = ConnectDb(); ObservableList<users> list = FXCollections.observableArrayList(); try { PreparedStatement ps = conn.prepareStatement("select * from users"); ResultSet rs = ps.executeQuery(); while (rs.next()){ list.add(new users(Integer.parseInt(rs.getString("user_id")), rs.getString("username"), rs.getString("password"), rs.getString("email"), rs.getString("type"))); } } catch (Exception e) { } return list; } }
***********************************************************************************************
Class users.java
package tableviewusers;
/**
*
* @author amir
*/
public class users {
int id ;
String username, password, email , type;
public void setId(int id) {
this.id = id;
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
public void setEmail(String email) {
this.email = email;
}
public void setType(String type) {
this.type = type;
}
public int getId() {
return id;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public String getEmail() {
return email;
}
public String getType() {
return type;
}
public users(int id, String username, String password, String email, String type) {
this.id = id;
this.username = username;
this.password = password;
this.email = email;
this.type = type;
}
}
***********************************************************************************************
FXMLDocument.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="550.0" prefWidth="700.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="tableviewusers.FXMLDocumentController">
<children>
<AnchorPane layoutY="6.0" prefHeight="543.0" prefWidth="228.0" style="-fx-background-color: #28252e;" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<Button layoutX="24.0" layoutY="320.0" mnemonicParsing="false" onAction="#Add_users" prefHeight="40.0" prefWidth="180.0" style="-fx-background-color: green;" text="Add" />
<Button layoutX="24.0" layoutY="376.0" mnemonicParsing="false" onAction="#Edit" prefHeight="40.0" prefWidth="180.0" style="-fx-background-color: Orange;" text="Update" />
<Button layoutX="24.0" layoutY="430.0" mnemonicParsing="false" onAction="#Delete" prefHeight="40.0" prefWidth="180.0" style="-fx-background-color: red;" text="Delete" />
<TextField fx:id="txt_username" layoutX="24.0" layoutY="60.0" onKeyReleased="#search_user" prefHeight="30.0" prefWidth="180.0" promptText="Username" />
<TextField fx:id="txt_password" layoutX="24.0" layoutY="110.0" prefHeight="30.0" prefWidth="180.0" promptText="Password" />
<TextField fx:id="txt_email" layoutX="24.0" layoutY="159.0" prefHeight="30.0" prefWidth="180.0" promptText="email" />
<TextField fx:id="txt_type" layoutX="24.0" layoutY="207.0" prefHeight="30.0" prefWidth="180.0" promptText="type" />
<TextField fx:id="txt_id" layoutX="24.0" layoutY="21.0" prefHeight="23.0" prefWidth="180.0" promptText="ID" />
</children>
</AnchorPane>
<TableView fx:id="table_users" layoutX="228.0" layoutY="76.0" onMouseClicked="#getSelected" prefHeight="474.0" prefWidth="469.0">
<columns>
<TableColumn fx:id="col_id" prefWidth="75.0" text="ID" />
<TableColumn fx:id="col_username" prefWidth="75.0" text="Username" />
<TableColumn fx:id="col_password" prefWidth="92.0" text="Password" />
<TableColumn fx:id="col_email" prefWidth="97.0" text="Email" />
<TableColumn fx:id="col_type" prefWidth="115.0" text="Type" />
</columns>
</TableView>
<TextField fx:id="filterField" layoutX="350.0" layoutY="30.0" promptText="Search" />
</children>
</AnchorPane>
***********************************************************************************************
Class TableViewUsers.java
package tableviewusers;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
*
* @author amir
*/
public class TableViewUsers 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);
}
}
COMMENTS