尝试通过应用filterlist和sortedlist过滤Observable列表时,值在表列中交换

时间:2017-03-02 19:12:11

标签: java sql javafx

我正在创建一个门通软件,它使用Table视图直接向MYSQL数据库添加数据,我添加了一个搜索textField来搜索来自Tutorial的条目

每当我使用该textField进行搜索时,一切正常,但在后台出现这些错误

"C:\Program Files\Java\jdk1.8.0_102\bin\java" -Didea.launcher.port=7537 "-Didea.launcher.bin.path=C:\Program Files (x86)\JetBrains\IntelliJ IDEA 2016.3.4\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_102\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_102\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_102\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_102\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_102\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_102\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_102\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_102\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_102\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_102\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_102\jre\lib\rt.jar;C:\Program Files (x86)\MySQL\Connector.J 5.1\mysql-connector-java-5.1.40-bin.jar;C:\Users\Suraj\IdeaProjects\DemoFinal\out\production\DemoFinal;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 2016.3.4\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain sample.Main
Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: argument type mismatch
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)

    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleKeyEvent(GlassViewEventHandler.java:246)
    at com.sun.glass.ui.View.handleKeyEvent(View.java:546)
    at com.sun.glass.ui.View.notifyKey(View.java:966)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:745)

当我按下一个键进行搜索时,每件事情都很有效,但在每次按键后都会产生这些错误。

在搜索之后,我的列值也会被交换。就像我有两个列一个用于地址而另一个用于电子邮件,在搜索之后它们开始向对方显示数据,其中的数据被交换。

package sample;

import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
import javafx.collections.transformation.SortedList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;

import java.net.URL;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Objects;
import java.util.ResourceBundle;
import java.util.function.Predicate;

public class DataController implements Initializable {

    Config config = new Config();
    Connection connection = DatabaseConnection.getConnection();


    int ids;

    @FXML
    private TextArea Address;
    @FXML
    private Label LabelFilter;

    @FXML
    private TextField TextFiledFilter;

    @FXML
    private TextField Lname;

    @FXML
    private TextField PhoneNo;

    @FXML
    private Button btnAdd;
    @FXML
    private TableColumn<EntryData, String> firstname;

    @FXML
    private TableColumn<EntryData, String> address;
    @FXML
    private TableColumn<?, ?> Timee;

    @FXML
    private TableColumn<EntryData, String> emailid;

    @FXML
    private TableColumn<EntryData, Double> phoneno;

    @FXML
    private TableColumn<EntryData, String> lastname;
    @FXML
    private TableColumn<EntryData, Integer> id;

    @FXML
    private TableColumn<?, ?> Datee;

    @FXML
    private TextField mailId;
    @FXML
    private Menu menuFile;
    @FXML
    private MenuBar MenuBar;
    @FXML
    private MenuItem MenuItemFilter;

    @FXML
    private TextField Fname;
    @FXML
    private TableView TableID;
    ObservableList<EntryData> list = FXCollections.observableArrayList();
    ObservableList<EntryData> FilterData = FXCollections.observableArrayList();

    @FXML
    void addBtnAction(ActionEvent event) throws SQLException {

        String query = "INSERT INTO DemoFinal(id,name,lastname,phone_no,email_id,Address) Values(?,?,?,?,?,?)";
        PreparedStatement statement = connection.prepareStatement(query);
        statement.setInt(1, ++ids);
        statement.setString(2, Fname.getText());
        statement.setString(3, Lname.getText());
        statement.setDouble(4, Double.parseDouble(PhoneNo.getText()));
        statement.setString(5, mailId.getText());
        statement.setString(6, Address.getText());
        statement.execute();



        Fname.clear();
        Lname.clear();
        mailId.clear();
        Address.clear();
        PhoneNo.clear();
        TableID.getItems().addAll(list);


        statement.close();


    }






    public DataController() throws Exception {
    }

    public void factorySettings() {

        id.setCellValueFactory(new PropertyValueFactory<EntryData, Integer>("id"));
        firstname.setCellValueFactory(new PropertyValueFactory<EntryData, String>("name"));
        lastname.setCellValueFactory(new PropertyValueFactory<EntryData, String>("lastname"));
        phoneno.setCellValueFactory(new PropertyValueFactory<EntryData, Double>("phone_no"));
        emailid.setCellValueFactory(new PropertyValueFactory<EntryData, String>("email"));
        address.setCellValueFactory(new PropertyValueFactory<EntryData, String>("address"));


    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        if (connection != null) {
            try {
                DatabaseConnection.getConnection();
                update();

            } catch (Exception e) {

                e.printStackTrace();

            }


        } else {
            config.alert(Alert.AlertType.ERROR, "Database Server May be Down Check Logs");

        }








        FilteredList <EntryData> filteredList= new FilteredList<EntryData>(list ,e-> true);
        TextFiledFilter.textProperty().addListener((observable, oldValue, newValue) ->{
            filteredList.setPredicate((Predicate<?super EntryData>) EntryData->{


                if(newValue==null||newValue.isEmpty()){
                    return true;
                }
                String iid= String.valueOf(EntryData.getId());

                String tolowercase= newValue.toLowerCase();

                if (EntryData.getName().toLowerCase().contains(tolowercase)){
                    return  true;
                }


                else if(EntryData.getLastname().contains(tolowercase)){
                    return true;
                }
                else if (EntryData.getEmail().toLowerCase().contains(tolowercase)){
                    return true;
                }
                else if(EntryData.getAddress().toLowerCase().contains(tolowercase)){
                    return true;
                }

               else if(String.valueOf(EntryData.getId()).contains(newValue)) {
                    return true;
                }
               else if (String.valueOf(EntryData.getPhone_no()).contains(newValue)){
                    return true;
                }
                return false;
            } );
        });
        SortedList<EntryData> sortedList= new SortedList<>(filteredList);
        sortedList.comparatorProperty().bind(TableID.comparatorProperty());
        TableID.setItems(sortedList);
    }






    public void update() throws SQLException {

        factorySettings();

        ResultSet resultSet = null;
        try {
            resultSet = connection.createStatement().executeQuery("SELECT * FROM DemoFinal");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        while (resultSet.next()) {
            ids = resultSet.getInt("id");
            list.add(new EntryData(ids, resultSet.getDouble("phone_no"),
                    resultSet.getString("name"), resultSet.getString("lastname"), resultSet.getString("email_id"),
                    resultSet.getString("Address")));

        }

        TableID.getItems().addAll(list);

        resultSet.close();

    }


    @FXML
    void OnMenuFilter(ActionEvent event) {

        LabelFilter.setText("Filter");
        TextFiledFilter.setScaleX(1);
        TextFiledFilter.setScaleY(1);


    }

    @FXML
    void OnactionTextField(ActionEvent event) {

    }


    @FXML
    void keyRelesed(ActionEvent event) {


    }



}

FXML文件

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="716.0" prefWidth="1224.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.DataController">
   <center>
      <TableView fx:id="TableID" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER">
         <columns>
            <TableColumn fx:id="id" prefWidth="51.0" text="ID" />
            <TableColumn fx:id="firstname" prefWidth="133.0" text="First Name" />
            <TableColumn fx:id="lastname" prefWidth="106.0" text="Last Name" />
            <TableColumn fx:id="emailid" prefWidth="121.0" text="Email-Id" />
            <TableColumn fx:id="phoneno" prefWidth="75.0" text="Phone No" />
            <TableColumn fx:id="address" prefWidth="233.0" text="Address" />
            <TableColumn fx:id="Timee" minWidth="0.0" prefWidth="88.0" text="Time of entry" />
            <TableColumn fx:id="Datee" minWidth="0.0" prefWidth="132.0" text="Date of Entry" />
         </columns>
      </TableView>
   </center>
   <left>
      <VBox prefHeight="528.0" prefWidth="255.0" BorderPane.alignment="CENTER">
         <children>
            <TextField fx:id="Fname" promptText="First Name">
               <VBox.margin>
                  <Insets bottom="40.0" left="5.0" right="5.0" top="5.0" />
               </VBox.margin>
            </TextField>
            <TextField fx:id="Lname" promptText="Last Name">
               <VBox.margin>
                  <Insets bottom="40.0" left="5.0" right="5.0" />
               </VBox.margin>
            </TextField>
            <TextField fx:id="mailId" promptText="Email-Id">
               <VBox.margin>
                  <Insets bottom="40.0" left="5.0" right="5.0" />
               </VBox.margin>
            </TextField>
            <TextField fx:id="PhoneNo" promptText="Phone_No">
               <VBox.margin>
                  <Insets bottom="40.0" left="5.0" right="5.0" />
               </VBox.margin>
            </TextField>
            <TextArea fx:id="Address" prefHeight="200.0" prefWidth="200.0" promptText="Address">
               <VBox.margin>
                  <Insets left="5.0" right="5.0" />
               </VBox.margin>
            </TextArea>
            <Button fx:id="btnAdd" mnemonicParsing="false" onAction="#addBtnAction" text="Add">
               <VBox.margin>
                  <Insets left="180.0" top="20.0" />
               </VBox.margin>
            </Button>
         </children>
      </VBox>
   </left>
   <top>
      <MenuBar fx:id="MenuBar" BorderPane.alignment="CENTER">
        <menus>
          <Menu fx:id="menuFile" mnemonicParsing="false" text="File">
            <items>
              <MenuItem fx:id="MenuItemFilter" mnemonicParsing="false" onAction="#OnMenuFilter" text="Filter" />
                  <MenuItem mnemonicParsing="false" text="Close" />
            </items>
          </Menu>
          <Menu mnemonicParsing="false" text="Edit">
            <items>
              <MenuItem mnemonicParsing="false" text="Delete" />
            </items>
          </Menu>
          <Menu mnemonicParsing="false" text="Help">
            <items>
              <MenuItem mnemonicParsing="false" text="About" />
            </items>
          </Menu>
        </menus>
      </MenuBar>
   </top>
   <bottom>
      <HBox BorderPane.alignment="CENTER">
         <children>
            <Label fx:id="LabelFilter" />
            <TextField fx:id="TextFiledFilter" onAction="#OnactionTextField" onKeyReleased="#keyRelesed" scaleX="0.0" scaleY="0.0" />
         </children>
         <BorderPane.margin>
            <Insets bottom="20.0" left="255.0" />
         </BorderPane.margin>
      </HBox>
   </bottom>
</BorderPane>

0 个答案:

没有答案