Ertan Hasani
Ertan Hasani

Reputation: 817

javafx Caused by: java.lang.UnsupportedOperationException

when i try to add an "object" to database and then show it to TableView it shows me UnsupportedOperationException . Everything was fine until i add this code to "public void initialize()" to make textfields as "SearchBoxes":

FilteredList <Paisjet> filteredData = new FilteredList<>(data,e -> true);

    paisjaSearch.textProperty().addListener((observableValue,oldValue,newValue) -> 
            {
                filteredData.setPredicate( paisjet -> 
                {
                    if(newValue == null || newValue.isEmpty())
                    {
                        return true;
                    }
                    String lowerCaseFilter = newValue.toLowerCase();
                    if(paisjet.getPaisja().toLowerCase().contains(lowerCaseFilter))
                    {
                        return true;
                    }
                    return false;
                });
            });
    kategoriaSearch.textProperty().addListener((observableValue,oldValue,newValue) -> 
            {
                filteredData.setPredicate( paisjet -> 
                {
                    if(newValue == null || newValue.isEmpty())
                    {
                        return true;
                    }
                    String lowerCaseFilter = newValue.toLowerCase();
                    if(paisjet.getKategoria().toLowerCase().contains(lowerCaseFilter))
                    {
                        return true;
                    }
                    return false;
                });
            });
    prodhuesiSearch.textProperty().addListener((observableValue,oldValue,newValue) -> 
            {
                filteredData.setPredicate( paisjet -> 
                {
                    if(newValue == null || newValue.isEmpty())
                    {
                        return true;
                    }
                    String lowerCaseFilter = newValue.toLowerCase();
                    if(paisjet.getProdhuesi().toLowerCase().contains(lowerCaseFilter))
                    {
                        return true;
                    }
                    return false;
                });
            });
    modeliSearch.textProperty().addListener((observableValue,oldValue,newValue) -> 
            {
                filteredData.setPredicate( paisjet -> 
                {
                    if(newValue == null || newValue.isEmpty())
                    {
                        return true;
                    }
                    String lowerCaseFilter = newValue.toLowerCase();
                    if(paisjet.getModeli().toLowerCase().contains(lowerCaseFilter))
                    {
                        return true;
                    }
                    return false;
                });
            });

    SortedList <Paisjet> sortedData = new SortedList<>(filteredData);
    sortedData.comparatorProperty().bind(tableView.comparatorProperty());
    tableView.setItems(sortedData);

OUTPUT:

Caused by: java.lang.UnsupportedOperationException
at java.util.AbstractList.remove(AbstractList.java:161)
at java.util.AbstractList$Itr.remove(AbstractList.java:374)
at java.util.AbstractList.removeRange(AbstractList.java:571)
at java.util.AbstractList.clear(AbstractList.java:234)
at main.MainWindowController.clearTable(MainWindowController.java:315)
at main.MainWindowController.addToTableFromDatabase(MainWindowController.java:320)
at main.MainWindowController.addToDatabase(MainWindowController.java:309)
... 61 more

clearTable():

public void clearTable()
{
    tableView.getItems().clear(); // line 315 at OUTPUT ERROR
}

addToTableFromDatabase():

public void addToTableFromDatabase() throws ClassNotFoundException, SQLException
{
    clearTable();  //line 320 at OUTPUT ERROR
    Class.forName("com.mysql.jdbc.Driver");
    String url="jdbc:mysql://***.***.**.*:****/*********";
    String uname="*****";
    String pass="*********";
    connect = (Connection) DriverManager.getConnection(url,uname,pass);

    Statement statement;
    String query = "SELECT * FROM paisjettable" ;
    statement = connect.createStatement();
    ResultSet rs = statement.executeQuery(query);

    while(rs.next())
    {

        int id = rs.getInt("id");
        String prodhuesi = rs.getString("prodhuesi");
        String modeli = rs.getString("modeli");
        String paisja = rs.getString("paisja");
        String pjesa = rs.getString("pjesa");
        String infoshtese = rs.getString("infoshtese");
        double qmimi = rs.getDouble("qmimi");
        double punedore = rs.getDouble("punedore");
        double pagesa = rs.getDouble("pagesa");
        int sasia = rs.getInt("sasia");

        paisjet = new Paisjet(id,prodhuesi,modeli,paisja,pjesa,qmimi,punedore,pagesa,sasia,infoshtese);
        data.add(paisjet);
        tableView.setItems(data);
    }

    rs.close();
    connect.close();
}

addToDatabase():

public void addToDatabase() throws ClassNotFoundException, SQLException
{
    addToDatabaseMethod(count,prodhuesiField.getText(),modeliField.getText(),paisjaField.getText(),pjesaField.getText(),Double.parseDouble(qmimiField.getText()),Double.parseDouble(puneDoreField.getText()),Integer.parseInt(sasiaField.getText()),infoArea.getText());
    count++;
    prodhuesiField.clear();
    modeliField.clear();
    paisjaField.clear();
    pjesaField.clear();
    qmimiField.clear();
    puneDoreField.clear();
    sasiaField.clear();
    infoArea.clear();
    addToTableFromDatabase(); // line 309 from OUTPUT ERROR
}

Upvotes: 0

Views: 1877

Answers (1)

James_D
James_D

Reputation: 209269

You have set the table view's backing list (items) to a sorted list, which cannot be directly modified (because it is always supposed to be a sorted version of its underlying list). So table.getItems() returns the SortedList and table.getItems().clear() attempts to modify it and throws the exception.

You should modify the underlying list, which you call data in your very first code block. You haven't shown any context for the blocks of code, so it's not clear what the scope of that variable is, but you essentially need data.clear() instead of table.getItems().clear().

(Also, you do not want to call table.setItems(data) in your loop in addToTableFromDatabase, as this will remove the filtering and sorting.)

Upvotes: 3

Related Questions