gpt4 book ai didi

javafx 由 : java. lang.UnsupportedOperationException 引起

转载 作者:行者123 更新时间:2023-12-01 21:14:30 32 4
gpt4 key购买 nike

当我尝试将“对象”添加到数据库然后将其显示到 TableView 时,它显示 UnsupportedOperationException 。一切都很好,直到我将此代码添加到“public void initialize()”以使文本字段成为“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);

输出:

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
}

最佳答案

您已将 TableView 的支持列表 (items) 设置为排序列表,该列表无法直接修改(因为它始终应该是其基础列表的排序版本)。因此,table.getItems()返回SortedListtable.getItems().clear()尝试修改它并引发异常。

您应该修改底层列表,在第一个代码块中将其称为data。您尚未显示代码块的任何上下文,因此不清楚该变量的范围是什么,但您本质上需要 data.clear() 而不是 table.getItems( ).clear()

(此外,您不想在 addToTableFromDatabase 的循环中调用 table.setItems(data),因为这将删除过滤和排序。)

关于javafx 由 : java. lang.UnsupportedOperationException 引起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40492670/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com