• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Cannot get my JList to contain elements of a local DefaultListModel

 
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, so this method works perfectly.  

I'm creating a group of cabins with campers added to them.  I'm doing this with a Map that has the Cabin as the key and the Camper elements.  

I have one JList that references all campers registered.  The 2nd list is supposed to add campers just to that cabin.  The first list prints fine because it is referencing a list declared in the class.  However, if I make the 2nd list a DefaultListModel throughout the whole class, it shows all campers added to any cabin, not just that particular cabin.  So, I needed to create a method that returns a DefaultListModel with a local DefaultListModel, not part of the class.  

The problem is, when I click the add button, it adds campers to that DefaultListModel and to the ArrayList that I used to reference it, but it will not add them to the JList!  No matter what I try.  The JList directly references that model, but it will simply not put any of the elements in it.  




This is the getOrCreateGroup method....




Both these methods were tested and work.  The Sets are printing right, the listAdded is printing right, and the dfm is printing right.  The only issue is it is not on the JList.  It is declared like this...


 
 
Marshal
Posts: 64642
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I am finding your problem confusing. Please explain how many Lists you have got, how many are fields, and whether anything is marked static. I think you will have to show us more code, assuming you are allowed to.

Thank you for using the code button, but you didn't get it right. [‍code=java] goes before the code and [‍/code] goes after it. I have corrected that for you and doesn't it look better
 
Rancher
Posts: 507
15
Notepad Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Kenneth Milota wrote:...
The problem is, when I click the add button, it adds campers to that DefaultListModel and to the ArrayList that I used to reference it, but it will not add them to the JList!  No matter what I try.  The JList directly references that model, but it will simply not put any of the elements in it.  
...



Post the code for the add button's listener (so that I can see what is happening and if there is an issue there).

Also, to print the elements in the DefaultListModel you can try using this:
System.out.println(Arrays.toString(dfm.toArray()));
-instead of-
//System.out.println(dfm);
 
Nathan Milota
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Prasad Saya wrote:

Kenneth Milota wrote:...
The problem is, when I click the add button, it adds campers to that DefaultListModel and to the ArrayList that I used to reference it, but it will not add them to the JList!  No matter what I try.  The JList directly references that model, but it will simply not put any of the elements in it.  
...



Post the code for the add button's listener (so that I can see what is happening and if there is an issue there).

Also, to print the elements in the DefaultListModel you can try using this:
System.out.println(Arrays.toString(dfm.toArray()));
-instead of-
//System.out.println(dfm);




Button Add = new Button("Add");
Add.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

add((Cabin)comboBox.getSelectedItem(), (Camper)camperJList.getSelectedValue() );
NewCamper.camperList2.remove((Camper)camperJList.getSelectedValue());



campersModel.removeElement((Camper)camperJList.getSelectedValue());

getModelForCabin((Cabin)comboBox.getSelectedItem()).addElement((Camper)camperJList.getSelectedValue());

System.out.println(getModelForCabin((Cabin)comboBox.getSelectedItem()));


//System.out.println(Arrays.toString(getOrCreateGroup((Cabin)comboBox.getSelectedItem()).toArray()));
}
});


That's the event handler for the add button.  However, I don't think that's where the problem is.  If I create a static DLM,  the button works, but if I include a local DLM, it does not work.  There's no other way I can think of to do this other than creating a local variable though, because it needs to be a new list for every cabin
 
Prasad Saya
Rancher
Posts: 507
15
Notepad Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One of these will, I think, work  in your case. Assuming the JList is already built and the model is set up:

1. If adding multiple elements:



2. Adding a single element:



3. Using a new model:


Note, this code goes into the add listener.
 
Nathan Milota
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Prasad Saya wrote:One of these will, I think, work  in your case. Assuming the JList is already built and the model is set up:

1. If adding multiple elements:



2. Adding a single element:



3. Using a new model:


Note, this code goes into the add listener.




Do you mean getModelForCabin()?  I don't have a getModel() method.  Is that something else?
 
Nathan Milota
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, so I tried your above suggestion, but it said "cannot convert from void to DefaultListModel.  So, I'm thinking that is saying your method won't work because getModel() is a void method.  
 
Prasad Saya
Rancher
Posts: 507
15
Notepad Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Do you mean getModelForCabin()?  I don't have a getModel() method.  Is that something else?



The getModel() method is of JList.
 
Nathan Milota
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Prasad Saya wrote:

Do you mean getModelForCabin()?  I don't have a getModel() method.  Is that something else?



The getModel() method is of JList.



I've tried all those steps, but for some reason it still doesn't work.  It prints out a blank model when I do it as it's not adding the element to it.  

I'm mostly interested in knowing if anyone can tell me why this method works, but isn't showing up on the JList when I already declared the JList to be from that method that returns the DefaultListModel.  It doesn't make any sense and nobody I've asked can figure out why it isn't working thus far.  
 
Nathan Milota
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Prasad Saya wrote:One of these will, I think, work  in your case. Assuming the JList is already built and the model is set up:

1. If adding multiple elements:



2. Adding a single element:



3. Using a new model:


Note, this code goes into the add listener.



A couple things here, on your adding a single element, the code words, but it is not referencing myModel.  It says the variable is never used.  So how do I get it to add the element to myModel?
 
Nathan Milota
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried the method suggested above, and i got the items to show up on the right JList, but I still have the problem where it doesn't create a new blank list when switching to another cabin, so that was essentially the same as creating a static DLM that stores all campers added to any cabin, and erases when I close and reopen the AssignToCabin class.

So, this won't work.  It has to work inside the method I created and inside that method ONLY.  Anyone have an idea how to do that?
 
Prasad Saya
Rancher
Posts: 507
15
Notepad Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I cannot see whats on the GUI and I am trying to understand what you are explaining. Can you post a screenshot of the GUI.
 
Nathan Milota
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Prasad Saya wrote:I cannot see whats on the GUI and I am trying to understand what you are explaining. Can you post a screenshot of the GUI.



I attached the picture here.  

What I'm trying to do is move items from the left list to the right.

The left list is loading correctly, because that just includes all campers.  The right list will not display anything.  It is referencing a method that returns a DefaultListModel.  I printed the model to the screen, and it is right, but will not show up on the list.
GUI-Cabin.png
[Thumbnail for GUI-Cabin.png]
 
Nathan Milota
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I played around with the code suggested a little bit, and now it shows up on the other list.  

The problem though, is when I close the screen and reopen it, it starts out blank again.  When I click the add button again, all the names I added in the previous time I used that screen show up on the list again.  How do I get it so that it restores the old list right when I open the screen?
 
Prasad Saya
Rancher
Posts: 507
15
Notepad Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Kenneth Milota wrote:I played around with the code suggested a little bit, and now it shows up on the other list.  

The problem though, is when I close the screen and reopen it, it starts out blank again.  When I click the add button again, all the names I added in the previous time I used that screen show up on the list again.  How do I get it so that it restores the old list right when I open the screen?



Confirm if this is correct:

1. The left-list has a list of campers
2. Initially, the right-list is empty
3. When you click the Add button, the selected camper in the left-list is added to the right-list

Then what happens?
What else the app does? How is it supposed to behave right from the start of the app and later.
And, what is not working. Refer to the picture and reply. Thanks.
 
Nathan Milota
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Prasad Saya wrote:

Kenneth Milota wrote:I played around with the code suggested a little bit, and now it shows up on the other list.  

The problem though, is when I close the screen and reopen it, it starts out blank again.  When I click the add button again, all the names I added in the previous time I used that screen show up on the list again.  How do I get it so that it restores the old list right when I open the screen?



Confirm if this is correct:

1. The left-list has a list of campers
2. Initially, the right-list is empty
3. When you click the Add button, the selected camper in the left-list is added to the right-list

Then what happens?
What else the app does? How is it supposed to behave right from the start of the app and later.
And, what is not working. Refer to the picture and reply. Thanks.




When I open that screen, it is supposed to have all the elements that I previously added to the other JList for that particular cabin.  It is storing them correctly in the HashMap I made for each cabin, but when I reopen it starts out as an empty JList.  Once I click the add button for a new camper, that camper goes to the right list, and then every camper I added in a previous session with this screen pops up with it.  

When I use the drop down menu to go to a new cabin, it doesn't change the JList in the right until I add a camper, then it erases all the campers from a different cabin, and then adds the camper to the list.
 
Prasad Saya
Rancher
Posts: 507
15
Notepad Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

When I open that screen, it is supposed to have all the elements that I previously added to the other JList



Okay, where are the elements (or contents or items or campers) of the both the lists (left-list and right-list) stored. I mean, where are these saved. When you close the screen it means exiting the app, correct?
 
Nathan Milota
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I figured that part out.  I needed to put the

list.setModel(getModelForCabin((Cabin)comboBox.getSelectedItem()));

in the Class outside of the event handler for the add button as well, because it wasn't showing that list until that part was executed.

I'm going to make sure all the other parts of the program are still working now that I changed so much.  I guess I should have started out with some Unit Testing.
 
Prasad Saya
Rancher
Posts: 507
15
Notepad Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Kenneth Milota wrote:I figured that part out.  I needed to put the

list.setModel(getModelForCabin((Cabin)comboBox.getSelectedItem()));

in the Class outside of the event handler for the add button as well, because it wasn't showing that list until that part was executed.

I'm going to make sure all the other parts of the program are still working now that I changed so much.  I guess I should have started out with some Unit Testing.



Yes, testing is a good idea. Just, one function/feature at a time.

Here is a link to Oracle's Java Tutorials's topics about using Swing Components; you might find some useful JList and other GUI component examples there:
https://docs.oracle.com/javase/tutorial/uiswing/components/index.html

Also, in future UseCodeTags
 
Campbell Ritchie
Marshal
Posts: 64642
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Kenneth Milota wrote:I figured that part out.  . . . .

Well done
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!