the 2nd one with an index of 1 will still have an index of one
All things are lawful, but not all things are profitable.
http://www.ChristopherAdams.com - Developer in training.
Norm Radder wrote:
the 2nd one with an index of 1 will still have an index of one
What do you do to see that?
After deleting the 1st one with an index of 0,the previously 2nd one with an index of 1 still has an index of 1 instead of 0 as it should since the first one got removed!
stelios papamichael wrote:added 3 clients, each one is assigned an index 1st one 0, 2nd one 1 and 3rd one 2. After deleting the 1st one with an index of 0,the previously 2nd one with an index of 1 still has an index of 1 instead of 0 as it should since the first one got removed!
Knute Snortum wrote:I have a suggestion: how about adding a ListDataListener to the model? Then you don't have to ask for a counter (index) when removing an element. It could also change the way you do other things in the app.
i cannot seem to make it work
Junilu Lacar wrote:
stelios papamichael wrote:added 3 clients, each one is assigned an index 1st one 0, 2nd one 1 and 3rd one 2. After deleting the 1st one with an index of 0,the previously 2nd one with an index of 1 still has an index of 1 instead of 0 as it should since the first one got removed!
What are you basing this assertion on? Are you perhaps printing out the element and seeing something like "2nd Client ... | Position: (1)"?
Norm Radder wrote:
i cannot seem to make it work
Please explain what you want it to do.
And post the code you are having problems with.
I print out the name, last name, afm number and position
Norm Radder wrote:
I print out the name, last name, afm number and position
The position must be determined when the element is retrieved from the model for showing in the list's window.
Norm Radder wrote:You need to do some research and reading about what the DefaultListModel does and how the JList class uses it.
There are some details in the API doc for those two classes: http://docs.oracle.com/javase/8/docs/api/index.html
and the tutorial: http://docs.oracle.com/javase/tutorial/uiswing/components/list.html
public interface ListModel<E>
This interface defines the methods components like JList use to get the value of each cell in a list
All things are lawful, but not all things are profitable.
Norm Radder wrote:Think how the JList class gets the elements from the ListModel. The ListModel's API doc says:
public interface ListModel<E>
This interface defines the methods components like JList use to get the value of each cell in a list
If you override the ListModel's method that is defined in the DefaultListModel class that returns the "value of each cell", you could append the position value to the end of the String.
In your method, call the super's method and append the String you want to see to the value that is returned.
Knute Snortum wrote:Well, here's my impression of what you could do: don't use a DefaultListModel. It seems that a DLM will reorder the elements in its underlying Vector (like an Array). You will never know what the new indices are.
What I think you need is a way to get which element is selected and I don't think a DLM will do that for you. Look at the web page Norm Radder gave you. I think a plain JList will do. When the delete button is pressed, check if there is a selected element and warn the user if not. If there is a selected element, get its index and delete it.
That's an important part of OOP: allowing a class that extends another class to redefine what a method in the super class does. You need to understand that.what does override do
PS. Really really sorry if my questions are dumb(and sorry for my bad English but i am not a native speaker)
All things are lawful, but not all things are profitable.
Norm Radder wrote:
That's an important part of OOP: allowing a class that extends another class to redefine what a method in the super class does. You need to understand that.what does override do
Besides explicitly extending a class, you can create an anonymous class by following the call to the class's constructor with {}s that wrap the methods you are overriding:
See the tutorial: http://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html
creating an anonymous class.?Like this? :
Norm Radder wrote:
creating an anonymous class.?Like this? :
What happened when you compiled and executed that code?
Also while testing new code it is useful to print out the values of the variables used in the code:
1) to see if the code is being executed
2) to see the values the program gets
Campbell Ritchie wrote:That method does not print anything. If you want anything printed, add a print call to that method. If you print model.capacity(), you do not appear to be doing anything with that method. Try printing dlm.capacity().
And yes, that is a correct implementation of an anonymous class.
All things are lawful, but not all things are profitable.
All things are lawful, but not all things are profitable.
Knute Snortum wrote:From what I can see, you need to put the DLM in a JList, then listen to the JList for selected items.
stelios papamichael wrote:Now that i got that out of the way, you mentioned adding a dlm in a JList.How does that happen.
All things are lawful, but not all things are profitable.
Knute Snortum wrote:It looks like from this code that you are wrapping the DLM in a JList already.
So start by getting selected element from the list:
Now my 2nd problem is saving the list and any change made to it when i the user presses the File -> Save file JMenu button.
All things are lawful, but not all things are profitable.
Knute Snortum wrote:
Now my 2nd problem is saving the list and any change made to it when i the user presses the File -> Save file JMenu button.
That depends a lot on how you are going to "save" the list. Probably the two easiest are to save the contents of the List to a text file, or using an ObjectOutputStream (that's a link).
Will Saving the list's content in a .txt file do the job?