简体   繁体   English

编组DefaultListModel不会写入数据

[英]Marshalling DefaultListModel does not write the data

I am making a simple GUI where JLists are populated by DefaultListModels. 我正在创建一个简单的GUI,其中JLists由DefaultListModels填充。

I have a class: 我有一节课:

@XmlRootElement (name="data")
@XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
public class Data {
    public DefaultListModel data1;
    public DefaultListModel data2;

And I am marshalling it in the normal way. 而且我正在以正常的方式进行编组。

File file = new File("file.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Data.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
Marshaller marshaller = jaxbContext.createMarshaller();        
marshaller.marshal(this.parts, file);

The problem is that the XML file is looking like this: 问题是XML文件看起来像这样:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<data>
    <data1>
        <size>6</size>
    </data1>
    <data2>
        <size>1</size>
    </data2>
</data>

In other words, it's not saving the data (which is a vector of Strings). 换句话说,它不是保存数据(这是字符串的向量)。 Does this have something to do with the fact that DefaultListModel does not contain the actual vector of strings? 这与DefaultListModel不包含字符串的实际向量这一事实有关吗? Do I have to do this with Vector instead? 我不得不用Vector做这件事吗?

The DefaultListModel class is the underlying model of a GUI component ( JList ). DefaultListModel类是GUI组件( JList )的基础模型。 Your business data is represented by your Data class. 您的业​​务数据由您的Data类表示。 I suggest to put there the Vector of Strings and then handle them in a DefaultListModel . 我建议将字符串Vector放在那里,然后在DefaultListModel处理它们。 You can marshall/unmarshall on that, because the JAXB framework does not know about the DefaultListModel type, but it will handle basic Java type. 你可以编组/取消编组,因为JAXB框架不知道DefaultListModel类型,但它将处理基本的Java类型。

@XmlRootElement (name="data")
@XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
public class Data {
    public List<String> data1;
    public List<String> data2;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM