The elements not added to JList?

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 the addElement(...) method that DefaultListModel does.

Up vote 2 down vote favorite share g+ share fb share tw.

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 = tokens0; 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 java swing netbeans jlist link|improve this question edited Dec 5 '11 at 6:56mKorbel32.9k6932 asked Dec 5 '11 at 0:02MichalB526 43% accept rate.

1 I've no idea why your code isn't working based on what you've posted. – Hovercraft Full Of Eels Dec 5 '11 at 0:04 Up-vote for adding important code -- thanks! – Hovercraft Full Of Eels Dec 5 '11 at 0:23 2 Don't let the GUI designer be an impediment.

– trashgod Dec 5 '11 at 0:34.

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 the addElement(...) method that DefaultListModel does. Something perhaps like so: if (returnVal == JFileChooser.

APPROVE_OPTION) { File file = fileChooser.getSelectedFile(); // note the cast since getModel() only returns a ListModel DefaultListModel model = (DefaultListModel)listLayer.getModel(); // changed** model. AddElement(file.getName()); }.

Thank you very much for your quick answer, however it still doesn't work. I don't know what can be the problem. Maybe it will help if I tell that my JList is defined inside JPanel but don't know if this information is connected with the problem occured (I think no).

– MichalB Dec 5 '11 at 0:29 +1 good catch. I think this is a related example. – trashgod Dec 5 '11 at 0:30 @trashgod - in which point do you see a relation between my problem and problem you showed please?

Thanks. – MichalB Dec 5 '11 at 0:42 @MichalB: Your program models a selection of files using DefaultListModel, which is tied to a particular component and hides a somewhat dated implementation using Vector. A List might be more flexible.

– trashgod Dec 5 '11 at 0:55 @trashgod - so if I understood it correctly you recommend me to use not DefaultListModel, but ListModel? – MichalB Dec 5 '11 at 0:59.

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 of File instances, then you may want to implement ListModel or even a shared model, as shown here.

The latter simply forwards some methods to the default implementation.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions