1

i have a JList component should me show a certain number of String when i click a jbutton component. I add every item (String) into a DefaultListModel and then pass it to the Jlist component in this way:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    DefaultListModel<String> listModel = new DefaultListModel<>();
    listModel.addElement("USA");
    listModel.addElement("India");
    listModel.addElement("Vietnam");
    listModel.addElement("Canada");
    listModel.addElement("Denmark");
    listModel.addElement("France");
    listModel.addElement("Great Britain");
    listModel.addElement("Japan");

    //create the list
    countryList = new JList<>(listModel);
}       

where JList component is defined in the following ways:

private JList<String> countryList;

when i run program my JList is empty and doesn't show me elements i added. If i use this command:

System.out.println(countryList.getModel());

for testing if items are added into the DefaultListModel component i can see them. Why JList component doesn't shows me elements? Thanks

xXJohnRamboXx
  • 739
  • 3
  • 10
  • 24
  • @FarazDurrani Hi! now i solved previously problem because i was forgot implementing toString method, but now the problem is the JList. I wrote this simple example for showing elements into a JList component but it doesn't work. I just copy and paste code from a working example but it doesn't work on my program. Could be a netbeans problem? – xXJohnRamboXx Feb 13 '16 at 11:25
  • can you please write the correct way to print elements into the jlist? i just followed this guide: http://www.codejava.net/java-se/swing/jlist-basic-tutorial-and-examples – xXJohnRamboXx Feb 13 '16 at 11:28
  • @FarazDurrani if i get System.out.println(countryList.getModel()); without toString() method i show correctly items but in my console!! not in my JList – xXJohnRamboXx Feb 13 '16 at 11:56
  • 1
    The code example you showed me from http://www.codejava.net/java-se/swing/jlist-basic-tutorial-and-examples works perfectly fine for me. I tried that on Eclipse. It opens a new window and shows me all the country list and shows me the close button and minimize button etc. I have no clue how to solve this issue. Have you tried looking around on stackoverflow or google? Have you looked at this: http://stackoverflow.com/questions/7878697/display-list-using-defaultlistmodel-and-jlist?lq=1 – Faraz Feb 13 '16 at 12:00
  • 1
    Write a program that does nothing but illustrate your problem. For the JList and the model, o the instantiation, adding to GUI container, adding data to the model, and notifying the list of a change in the model (if you do that) the same way as you do in your actual program (which I assume you do not want to post). You may understand what you've done wrong in the process; if not, you will have an entire example that you can post and that others can compile and run to determine what is wrong. There isn't enough information in your post as it is. – arcy Feb 13 '16 at 12:14
  • 1
    Your code creates a *brand new* JList which is not part of any Window. You must add it to a Window in order to see the results. Alternatively, if countryList already exists, you can pass your new listModel to that JList's `setModel` method. – VGR Feb 13 '16 at 12:32
  • @FarazDurrani Man i fixed my problem. The problem was: when i pass this countryList = new JList<>(listModel); i creates a new JList component different from which i created using palette, so for this reason i didn't show anything in my main JList. Thanks for helping me! ^^ – xXJohnRamboXx Feb 14 '16 at 09:09
  • Oh so you already had a JList. Oh okay now that makes sense xD Good for you – Faraz Feb 14 '16 at 10:17
  • @xXJohnRamboXx Can you upvote my answer here as some douchebag downvoted it when the answer was slightly wrong. Now he won't take his downvote back after the solution has been corrected. Plz upvote my answer here xD http://stackoverflow.com/a/35379753/4828463 – Faraz Feb 14 '16 at 10:18

0 Answers0