简体   繁体   English

如何在Java中将对象添加到已经实例化的JList中?

[英]How to add objects to a already instantiated JList in Java?

I want to add objects to a JList which has already been instantiated by the Netbeans genrated code. 我想将对象添加到已经由Netbeans生成代码实例化的JList中。

I can't pass my own list model in the JList constructor as I can't modify the Netbeans generated code. 我无法在JList构造函数中传递自己的列表模型,因为我无法修改Netbeans生成的代码。

How can I add object to that JList. 如何将对象添加到该JList。

As per my comment to nfechner, it really depends on how you've constructed your JList. 根据我对nfechner的评论,这实际上取决于你如何构建你的JList。 If you've used the JList(ListModel) or JList(Vector) constructors you can potentially amend the consents of your model through a call to getModel() , or in the case of Vector , simply maintain a reference to the Vector and amend that (providing you fire an event aftewards to notify any model listeners). 如果你已经使用了JList(ListModel)JList(Vector)构造函数,你可以通过调用getModel()修改模型的同意,或者在Vector的情况下,只需保持对Vector的引用并修改它(让你在事后发射事件通知任何模特听众)。

However, I'm fairly sure there are "hooks" in the Netbeans IDE to allow you to specify a specific model implementation, which will then cause the code to be auto-generated to include this. 但是,我很确定Netbeans IDE中有“钩子”允许您指定特定的模型实现,然后这将导致代码自动生成以包含此实现。

Modify the generated list model. 修改生成的列表模型。 You can get it via: 你可以通过以下方式获得:

See JList#getModel() 请参阅JList#getModel()

It's possible to set your own model to the already instantiated JList, see JList#setModel(ListModel) 可以将自己的模型设置为已经实例化的JList,请参阅JList#setModel(ListModel)

You can pass the model extended from DefaultListModel, which supports add and remove methods. 您可以传递从DefaultListModel扩展的模型,该模型支持添加和删除方法。

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

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