I created a table view using fx which displays information from a database and whenever I search through the database and display the results, for some reason it will display the last value retrieved times the amount of results found but what it should do is display every item in the observableList
. What am I doing wrong?
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.util.ArrayList;
/**
* Created by user on 02/06/2016.
*/
public class Main extends Application {
TableView<TableViewData> table;
ObservableList<TableViewData> itemList = FXCollections.observableArrayList();
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Welcome");
primaryStage.setResizable(false);
TableColumn<TableViewData, String> nameColumn = new TableColumn<>("Name");
nameColumn.setMinWidth(200);
nameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
TableColumn<TableViewData, String> brandColumn = new TableColumn<>("Brand");
nameColumn.setMinWidth(200);
nameColumn.setCellValueFactory(new PropertyValueFactory<>("brand"));
TableColumn<TableViewData, Integer> quantityColumn = new TableColumn<>("Quantity");
nameColumn.setMinWidth(200);
nameColumn.setCellValueFactory(new PropertyValueFactory<>("quantity"));
TableColumn<TableViewData, Double> priceColumn = new TableColumn<>("Price");
nameColumn.setMinWidth(200);
nameColumn.setCellValueFactory(new PropertyValueFactory<>("price"));
TableColumn<TableViewData, String> typeColumn = new TableColumn<>("Type");
nameColumn.setMinWidth(200);
nameColumn.setCellValueFactory(new PropertyValueFactory<>("type"));
table = new TableView<>();
table.getColumns().addAll(nameColumn, quantityColumn, priceColumn, typeColumn, brandColumn);
TextField query = new TextField();
query.setPromptText("Search");
ComboBox column = new ComboBox();
//adds the optons for the combo box
column.getItems().addAll(
"Name",
"Price",
"Item Type",
"Brand",
"Details"
);
Button searchButton = new Button("Search");
searchButton.setOnAction(e -> {
try {
itemList = DatabaseHandling.search(column.getValue().toString(), query.getText());
table.getItems().addAll(itemList);
} catch (Exception ex) {
Errors.error("Please select a an option from the box", "error");
}
});
HBox searchBox = new HBox();
searchBox.getChildren().addAll(query, column, searchButton);
searchBox.setPadding(new Insets(10, 10, 10, 10));
searchBox.setSpacing(10);
Button createItem = new Button("Create");
createItem.setOnAction(e -> MainFunctions.createItem());
Button deleteItem = new Button("Delete");
Button editItem = new Button("Edit");
editItem.setOnAction(e -> MainFunctions.editItem());
HBox itemManipulationBox = new HBox();
itemManipulationBox.getChildren().addAll(createItem, deleteItem, editItem);
itemManipulationBox.setPadding(new Insets(10, 10, 10, 10));
itemManipulationBox.setSpacing(10);
VBox layout = new VBox();
layout.getChildren().addAll(table, searchBox, itemManipulationBox);
Scene scene = new Scene(layout);
primaryStage.setScene(scene);
primaryStage.show();
}
}
Here is the search function the search button calls:
public static ObservableList<TableViewData> search(String column, String query){
String name=null;
String brand = null;
String type=null;
int quantity=0;
double price =0.00;
String newQuery = query.replace(" ","_");
String s = "SELECT * FROM ITEM " +
"WHERE "+column+" LIKE '%"+newQuery+"%';";
Connection con =db_connectionHandler.connect();
ObservableList<TableViewData> items = FXCollections.observableArrayList();
items.clear();
try{
Statement st=con.createStatement();
st.executeUpdate(s);
ResultSet rs=st.executeQuery(s);
while(rs.next()){
items.add(new TableViewData(rs.getString("NAME"),rs.getInt("QUANTITY"),rs.getDouble("PRICE"),rs.getString("TYPE"),rs.getString("BRAND")));
}
for(TableViewData i: items) {
System.out.println();
}
db_connectionHandler.endConnect(con,st,rs);
return items;
}
catch(SQLException e){
System.out.print("error: "+e.getMessage());
}
db_connectionHandler.endConnect(con,null,null);
return items;
}
Class for the TableViewData
object:
public class TableViewData {
private static SimpleStringProperty name;
private static SimpleStringProperty brand;
private static int quantity;
private static double price;
private static SimpleStringProperty type;
//constructor for item object
public TableViewData(String Fname, int Fquantity, double Fprice, String Ftype, String Fbrand ){
TableViewData.name=new SimpleStringProperty(Fname);
TableViewData.quantity= new Integer(Fquantity);
TableViewData.price= new Double(Fprice);
TableViewData.type=new SimpleStringProperty(Ftype);
TableViewData.brand=new SimpleStringProperty(Fbrand);
}
public static String getName() {
return name.get();
}
public static void setName(String name) {
TableViewData.name.set(name.replace(" ","_"));
}
public static String getBrand() {
return brand.get();
}
public static void setBrand(String brand) {
TableViewData.brand.set(brand.replace(" ","_"));
}
public static int getQuantity() {
return quantity;
}
public static void setQuantity(int quantity) {
TableViewData.quantity = quantity;
}
public static double getPrice() {
return price;
}
public static void setPrice(double price) {
TableViewData.price = price;
}
public static String getType() {
return type.get();
}
public static void setType(String type) {
TableViewData.type.set(type.replace(" ","_"));
}
}