未添加到 JList 的元素
我正在尝试将文件名添加到我的 JList 但没有成功。这是代码片段:
DefaultListModel model = new DefaultListModel();
listLayer.setModel(model);
model.addElement(file.getName());
listLayer 是一个 JList,我想在其中添加文件名。有关信息,我正在 netBeans 中编写 GUI 应用程序,因此我无法在此代码中创建新的 JList 对象,因为在将 JList 添加到我的布局时它已经自动创建了。因此我可以通过它的方法访问它。
多谢, 米哈尔.
-------------------------------------------------- -----------------------
好的,我会尝试更多地扩展它:
private void openActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser fileChooser = new JFileChooser("C:/");
FileFilter filter1 = new MyCustomFilter();
fileChooser.setFileFilter(filter1);
int returnVal = fileChooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
//String[] tokens = file.getName().split(".");
//String name = tokens[0];
DefaultListModel model = new DefaultListModel();
listLayer.setModel(model);
model.addElement(file.getName());
} else {
System.out.println("File access cancelled by user.");
}
}
是的,我的名为 listLayer 的 JList 在代码的不可修改部分中声明,例如这: 私有 javax.swing.JList listLayer;
再次感谢您的帮助。米哈尔
I am trying to add file names to my JList but without success. Here is the piece of the code:
DefaultListModel model = new DefaultListModel();
listLayer.setModel(model);
model.addElement(file.getName());
listLayer is a JList into which I would like to add file name. For information, I am writing my GUI application in netBeans so I can not create a new JList object within this code as it was already created automatically when added JList to my layout. Therefore I can just access it through its methods.
Thanks a lot,
Michal.
-------------------------------------------------------------------------
Ok I will try to extend it more:
private void openActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser fileChooser = new JFileChooser("C:/");
FileFilter filter1 = new MyCustomFilter();
fileChooser.setFileFilter(filter1);
int returnVal = fileChooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
//String[] tokens = file.getName().split(".");
//String name = tokens[0];
DefaultListModel model = new DefaultListModel();
listLayer.setModel(model);
model.addElement(file.getName());
} else {
System.out.println("File access cancelled by user.");
}
}
and yes, my JList called listLayer is declared in non-modifiable section of the code like this:
private javax.swing.JList listLayer;
Thanks again for any help. Michal
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
感谢您发布更多代码。现在我们很可能可以回答您的问题。我看到的一个问题是,每次按下按钮时,您都会重新创建一个 DefaultListModel,并使用这个新模型设置 JList,从而有效地删除列表之前保存的所有数据。避免这样做的一种方法是简单地获取 JList 已有的模型(应该是 DefaultListModel),并向其中添加项目。您需要转换
getModel()
返回的对象,因为根据 API,Java 只知道这是一个 ListModel 对象,而 ListModel 没有addElement(...)
DefaultListModel 所做的方法。也许是这样的:
Thanks for posting more code. Now quite possibly we can answer your question. A problem I see is that you're recreating a DefaultListModel each time the button is pressed and setting the JList with this new model effectively removing all data that was previously held by the list. A way to avoid doing this is to simply get the model that the JList already has, which should be a DefaultListModel, and add items to it. You will need to cast the object returned by
getModel()
since per the API, Java only knows this to be a ListModel object, and ListModel doesn't have theaddElement(...)
method that DefaultListModel does.Something perhaps like so:
我对你的程序的其余部分了解不够,无法说。如果您有一个相对静态的
JList
,DefaultListModel
可能是完美的。如果您的程序对不断变化的File
实例选择进行建模,那么您可能需要实现ListModel
甚至共享模型,如下所示 此处。后者只是将一些方法转发给默认实现。I don't know enough about the rest of your program to say. If you have a single, relatively static
JList
,DefaultListModel
may be perfect. If your program models a constantly changing selection ofFile
instances, then you may want to implementListModel
or even a shared model, as shown here. The latter simply forwards some methods to the default implementation.