java.lang.UnsupportedOperationException JavaFX ArrayList<;字符串>;可观察列表<;StringProperty>;
我在javaFX方面遇到了问题,我目前正在尝试序列化一个对象,为此我创建了一个类,将对象从javaFX转换为可序列化对象。 但当我试图加载回对象时,我得到java.lang.UnsupportedOperationException JavaFX ArrayList<;字符串>;可观察列表<;StringProperty>;,java,arraylist,javafx,observablelist,unsupportedoperation,Java,Arraylist,Javafx,Observablelist,Unsupportedoperation,我在javaFX方面遇到了问题,我目前正在尝试序列化一个对象,为此我创建了一个类,将对象从javaFX转换为可序列化对象。 但当我试图加载回对象时,我得到 Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXM
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
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.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.control.MenuItem.fire(MenuItem.java:462)
at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.doSelect(ContextMenuContent.java:1405)
at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.lambda$createChildren$343(ContextMenuContent.java:1358)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
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.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:380)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:294)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:416)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:415)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
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(Unknown Source)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.Trampoline.invoke(Unknown Source)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1771)
... 43 more
Caused by: java.lang.UnsupportedOperationException
at java.util.AbstractList.add(Unknown Source)
at java.util.AbstractList.add(Unknown Source)
at view.OverviewInterroController.handleLoad(OverviewInterroController.java:172)
... 53 more
导致它的代码:
package model;
import java.io.Serializable;
import java.util.ArrayList;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
public class QuestionSerializable implements Serializable {
private final String question;
private final String typeReponse;
private final int nbLignesReponse;
private final ArrayList<String> reponseData = new ArrayList<String>();
public QuestionSerializable(QuestionModel model)
{
this.question = model.getQuestion().getValue();
this.typeReponse = model.getTypeReponse().getValue();
this.nbLignesReponse = model.getNbLignesReponse().getValue();
for(StringProperty val : model.getReponseData().getValue())
{
reponseData.add(val.getValue());
}
}
public QuestionModel convert()
{
QuestionModel qm = new QuestionModel(question);
qm.getNbLignesReponse().setValue(this.nbLignesReponse);
qm.getTypeReponse().setValue(typeReponse);
ObjectProperty<ObservableList<StringProperty>> reponseData2 =qm.getReponseData();
for(String val : reponseData)
{
reponseData2.getValue().add(new SimpleStringProperty(val));
}
return qm;
}
}
包模型;
导入java.io.Serializable;
导入java.util.ArrayList;
导入javafx.beans.property.SimpleStringProperty;
导入javafx.beans.property.StringProperty;
导入javafx.collections.FXCollections;
公共类QuestionSerializable实现了Serializable{
私人最终字符串问题;
私有最终字符串类型响应;
私人最终国际木材报告;
private final ArrayList reponseData=new ArrayList();
公共问题可序列化(问题模型)
{
this.question=model.getQuestion().getValue();
this.TypeResponse=model.getTypeResponse().getValue();
this.nbLignesReponse=model.getNbLignesReponse().getValue();
对于(StringProperty val:model.getReponseData().getValue())
{
reponseData.add(val.getValue());
}
}
公共模型转换()
{
QuestionModel qm=新的QuestionModel(问题);
qm.getNbLignesReponse().setValue(this.nbLignesReponse);
qm.getTypeResponse().setValue(TypeResponse);
ObjectProperty reponseData2=qm.getReponseData();
for(字符串val:reponseData)
{
reponseData2.getValue().add(新的SimpleStringProperty(val));
}
返回qm;
}
}
特别是:
ObjectProperty<ObservableList<StringProperty>> reponseData2 =qm.getReponseData();
for(String val : reponseData)
{
reponseData2.getValue().add(new SimpleStringProperty(val));
}
ObjectProperty reponseData2=qm.getReponseData();
for(字符串val:reponseData)
{
reponseData2.getValue().add(新的SimpleStringProperty(val));
}
和概览控制器
package view;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import com.itextpdf.text.xml.simpleparser.handler.NeverNewLineHandler;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.FileChooser;
import model.QuestionModel;
import model.QuestionSerializable;
import principale.Main;
public class OverviewInterroController {
@FXML
private TableView<QuestionModel> questionTable;
@FXML
private TableColumn<QuestionModel,String> questionColumn;
@FXML
private Label questionLabel;
@FXML
private Label typeLabel;
@FXML
private Label nbLigneLabel;
@FXML
private TableView<StringProperty> reponseTable;
@FXML
private TableColumn<ObservableList<StringProperty>,String> reponseColumn;
// Reference to the main application.
private Main mainApp;
@FXML
private void initialize()
{
questionColumn.setCellValueFactory(cellData -> cellData.getValue().getQuestion());
showQuestionDetails(null);
questionTable.getSelectionModel().selectedItemProperty().addListener(
(observable,oldvalue,newValue)-> showQuestionDetails(newValue));
}
public void showReponseTable(QuestionModel question)
{
if(question.getTypeReponse().getValue().equals("Cocher"))
{
reponseTable.setItems(question.getReponseData().getValue());
reponseColumn.setCellValueFactory(new PropertyValueFactory<>("value"));
reponseTable.setVisible(true);
}else
{
this.reponseTable.setVisible(false);
}
}
public void setMainApp(Main mainApp) {
this.mainApp = mainApp;
questionTable.setItems(mainApp.getQuestionData());
}
public void handleImprimer()
{
mainApp.showImprimerDialog();
}
public void handleClose()
{
System.exit(0);
}
public void showQuestionDetails(QuestionModel question)
{
if(question != null)
{
questionLabel.setText(question.getQuestion().getValue());
typeLabel.setText(question.getTypeReponse().getValue());
nbLigneLabel.setText(Integer.toString(question.getNbLignesReponse().getValue()));
reponseTable.setItems(FXCollections.emptyObservableList());
showReponseTable(question);
}else
{
questionLabel.setText("");
typeLabel.setText("");
nbLigneLabel.setText("0");
reponseTable.setVisible(false);
}
}
public void handleDelete()
{
int selectedIndex = questionTable.getSelectionModel().getSelectedIndex();
if(selectedIndex>=0)
{
questionTable.getItems().remove(selectedIndex);
}else
{
Alert alert = new Alert(AlertType.ERROR);
alert.initOwner(mainApp.getPrimaryStage());
alert.setTitle("Aucune question séléctionnée !");
alert.setContentText("Veuillez séléctionner une question valide !");
alert.showAndWait();
}
}
public void handleSave() throws IOException
{
FileChooser fileChooser = new FileChooser();
//Set extension filter
FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("CTRL Files (*.ctrl)", "*.ctrl");
fileChooser.getExtensionFilters().add(extFilter);
//Show save file dialog
File file = fileChooser.showSaveDialog(this.mainApp.getPrimaryStage());
if(file != null){
FileOutputStream fout = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(fout);
ArrayList<QuestionSerializable> qs = new ArrayList<QuestionSerializable>();
for(QuestionModel qm : this.mainApp.getQuestionData())
{
qs.add( new QuestionSerializable(qm));
}
oos.writeObject(new ArrayList<QuestionModel>(this.mainApp.getQuestionData()));
oos.close();
fout.close();
}
}
@SuppressWarnings("unchecked")
public void handleLoad() throws Exception
{
FileChooser fileChooser = new FileChooser();
//Set extension filter
FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("CTRL Files (*.ctrl)", "*.ctrl");
fileChooser.getExtensionFilters().add(extFilter);
//Show save file dialog
File file = fileChooser.showSaveDialog(this.mainApp.getPrimaryStage());
if(file != null){
FileInputStream fout = new FileInputStream(file);
ObjectInputStream oos = new ObjectInputStream(fout);
ArrayList<QuestionSerializable> qm = (ArrayList<QuestionSerializable>) oos.readObject();
ObservableList<QuestionModel> qo = FXCollections.emptyObservableList();
for(QuestionSerializable q : qm)
{
qo.add(q.convert());
}
this.mainApp.setQuestionData(qo);
this.questionTable.refresh();
this.showQuestionDetails(null);
oos.close();
fout.close();
}
}
public void handleNew()
{
QuestionModel tempQuestion = new QuestionModel("");
tempQuestion.getReponseData().getValue().add(new SimpleStringProperty(""));
boolean okClicked = mainApp.showPersonEditDialog(tempQuestion);
if (okClicked) {
mainApp.getQuestionData().add(tempQuestion);
}
}
public void handleEdit()
{
QuestionModel selectedQuestion = questionTable.getSelectionModel().getSelectedItem();
if(selectedQuestion!=null)
{
boolean okClicked = mainApp.showPersonEditDialog(selectedQuestion);
if (okClicked) {
showQuestionDetails(selectedQuestion);
this.reponseTable.refresh();
}
}else
{
Alert alert = new Alert(AlertType.ERROR);
alert.initOwner(mainApp.getPrimaryStage());
alert.setTitle("Aucune question séléctionnée !");
alert.setContentText("Veuillez séléctionner une question valide !");
alert.showAndWait();
}
}
}
包视图;
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.FileNotFoundException;
导入java.io.FileOutputStream;
导入java.io.IOException;
导入java.io.ObjectInputStream;
导入java.io.ObjectOutputStream;
导入java.util.ArrayList;
导入com.itextpdf.text.xml.simpleparser.handler.NeverNewLineHandler;
导入javafx.beans.property.SimpleStringProperty;
导入javafx.beans.property.StringProperty;
导入javafx.collections.FXCollections;
导入javafx.collections.ObservableList;
导入javafx.fxml.fxml;
导入javafx.scene.control.Alert;
导入javafx.scene.control.Alert.AlertType;
导入javafx.scene.control.Label;
导入javafx.scene.control.TableColumn;
导入javafx.scene.control.TableView;
导入javafx.scene.control.cell.PropertyValueFactory;
导入javafx.stage.FileChooser;
导入模型。问题模型;
导入model.QuestionSerializable;
主要进口产品;
公共类概览InterroController{
@FXML
私有表视图问题表;
@FXML
私有表列;
@FXML
自有标签;
@FXML
私人标签;
@FXML
自有品牌nbLigneLabel;
@FXML
私有TableView-responsetable;
@FXML
private TableColumn reponseColumn;
//对主应用程序的引用。
私有主应用程序;
@FXML
私有void初始化()
{
questionColumn.setCellValueFactory(cellData->cellData.getValue().getQuestion());
showQuestionDetails(空);
questionTable.getSelectionModel().SelectEditeProperty().addListener(
(可观察、旧值、新值)->showQuestionDetails(新值);
}
公共无效显示响应表(问题模型问题)
{
if(question.getTypeResponse().getValue().equals(“Cocher”))
{
setItems(question.getReponseData().getValue());
responseColumn.setCellValueFactory(新属性ValueFactory(“值”));
responsetable.setVisible(true);
}否则
{
this.responseTable.setVisible(false);
}
}
公共无效setMainApp(主mainApp){
this.mainApp=mainApp;
questionTable.setItems(mainApp.getQuestionData());
}
公共无效手册
{
mainApp.showinImpimerDialog();
}
公共无效handleClose()
{
系统出口(0);
}
公共问题详细信息(问题模型问题)
{
如果(问题!=null)
{
questionLabel.setText(question.getQuestion().getValue());
typeLabel.setText(question.getTypeResponse().getValue());
nbLigneLabel.setText(Integer.toString(question.getNbLignesReponse().getValue());
setItems(FXCollections.emptyObservableList());
可展示的(问题);
}否则
{
questionLabel.setText(“”);
typeLabel.setText(“”);
nbLigneLabel.setText(“0”);
ResponseTable.setVisible(假);
}
}
公共无效handleDelete()
{
int-selectedIndex=questionTable.getSelectionModel().getSelectedIndex();
如果(已选择索引>=0)
{
questionTable.getItems().remove(selectedIndex);
}否则
{
Alert Alert=新警报(AlertType.ERROR);
alert.initOwner(mainApp.getPrimaryStage());
警惕。setTitle(“Aucune question séléactionée!”);
setContentText(“Veuillez séléctionner une question valide!”);
alert.showAndWait();
}
}
public void handleSave()引发IOException
{
FileChooser FileChooser=newfilechooser();
//设置扩展筛选器
FileChooser.ExtensionFilter extFilter=newfilechooser.ExtensionFilter(“CTRL文件(*.CTRL)”,“*.CTRL”);
fileChooser.getExtensionFilters().add(extFilter);
//显示保存文件对话框
File File=fileChooser.showsavedilog(this.mainApp.getPrimaryStage());
如果(文件!=null){
FileOutputStream fout=新的FileOutputStream(文件);
ObjectOutputStream oos=新的ObjectOutputSt
ObservableList<QuestionModel> qo = FXCollections.emptyObservableList();
for(QuestionSerializable q : qm)
{
qo.add(q.convert());
}
package model;
import java.io.Serializable;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
public class QuestionModel{
private final StringProperty question;
private final StringProperty typeReponse = new SimpleStringProperty("Remplir Ligne");
private final IntegerProperty nbLignesReponse = new SimpleIntegerProperty(1);
private ObjectProperty<ObservableList<StringProperty>> reponseData = new SimpleObjectProperty<ObservableList<StringProperty>>(FXCollections.observableArrayList());
/**
* @return the reponseData
*/
public ObjectProperty<ObservableList<StringProperty>> getReponseData() {
return reponseData;
}
public void setReponseData(ObjectProperty<ObservableList<StringProperty>> reponseData)
{
this.reponseData = reponseData;
}
public QuestionModel(String question)
{
this.question = new SimpleStringProperty(question);
}
/**
* @return the nbLignesReponse
*/
public IntegerProperty getNbLignesReponse() {
return nbLignesReponse;
}
/**
* @return the question
*/
public StringProperty getQuestion() {
return question;
}
/**
* @return the typeReponse
*/
public StringProperty getTypeReponse() {
return typeReponse;
}
}
FXCollections.emptyObservableList()
for(QuestionSerializable q : qm)
{
qo.add(q.convert());
}
FXCollections.observableArrayList()
ObservableList<QuestionModel> qo = FXCollections.observableArrayList();