How does a DefaultListModel store it's elements..?

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 708 Views - Last Post: 11 April 2019 - 03:48 PM Rate Topic: -----

#1 Joshuaparsons18   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 56
  • Joined: 07-May 16

How does a DefaultListModel store it's elements..?

Posted 06 April 2019 - 08:03 PM

Strangely, after writing a HashSet<String> to DeafultListModel (DLM) it adds the element, but then I request to remove the element in the HashSet, after adding our element. When, I do this it causes the line to be written blank ([]). However, if I remove the .remove(temp); line then it reads the contents, of the entire hashmap. I need it to read the entire hashmap and add each one to the JList one at a time.

I have looked at the documentation and the DLM uses vectors instead of collection implementation. Possibly this could have something to do with it? (Unfamiliar area).

the addElement just says that it will add whatever is passed to the end of the list. Fantastic, but what's it not telling you is... remove the content from the HashSet and it will remove from the JList to?

	private void addToJListModelFromHashSet(HashSet<String> hashSet) {
		// Pased hashSet size
		int hashSetSize = hashSet.size();
		
		// We need a HashSet<String> that stores one element at a time and writes it
		// to the listModel. instead of a bunch at a time. We will have to remove 
		// the element after it's written... This is only a temp HashSet
		HashSet<String> writesOneElement = new HashSet<String>();
		Iterator<String> it = hashSet.iterator();
		String temp;
		
		for(int x = 0; x < hashSetSize; x++) {
			if(x > hashSetSize)
				break;
			temp = it.next();
			// adds the next element in the iterator of the passed HashSet<String>
			writesOneElement.add(temp);
			System.out.println("WritesoneElement (temp) = " + writesOneElement);
			// adds the element to the listModel
			listModel.addElement(writesOneElement);
			System.out.println(listModel.get(x));
			// Remove the element passed
			writesOneElement.remove(temp);
		}
	}


Image below contains the above code, console output, and example of adding to the hash

https://imgur.com/a/cSsdr5Z

(Also, sorry I am posting a lot.)

Is This A Good Question/Topic? 0
  • +

Replies To: How does a DefaultListModel store it's elements..?

#2 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3627
  • View blog
  • Posts: 16,660
  • Joined: 20-September 08

Re: How does a DefaultListModel store it's elements..?

Posted 07 April 2019 - 01:47 AM

Can you tell us what your code is meant to do, since to me it doesn't seem to make a lot of sense?
Is it really just to turn a HashSet into a ListModel?

This post has been edited by g00se: 07 April 2019 - 03:56 AM
Reason for edit:: Clarification

Was This Post Helpful? 0
  • +
  • -

#3 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 765
  • View blog
  • Posts: 5,767
  • Joined: 25-December 13

Re: How does a DefaultListModel store it's elements..?

Posted 07 April 2019 - 04:37 AM

Can you post the definition of the listModel variable?
Was This Post Helpful? 0
  • +
  • -

#4 Joshuaparsons18   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 56
  • Joined: 07-May 16

Re: How does a DefaultListModel store it's elements..?

Posted 07 April 2019 - 07:09 AM

View Postg00se, on 07 April 2019 - 01:47 AM, said:

Can you tell us what your code is meant to do, since to me it doesn't seem to make a lot of sense?
Is it really just to turn a HashSet into a ListModel?


We pass the HashSet that we are storing in the JList via the addToJListModelFromHashSet(HashSet<String> hashSet) method
When we get in we store the size of the passed HashSet in an int to loop through.
Create a HashSet that takes one element at a time from the passed HashSet
...because the listModel is looking for HashSet<String> we store the single element in a String and pass that to the writesOneElement HashSet<String>. Then, I remove the element from writesOneElement because we do not want to add more than one element at a time. E.g if I were to read the element without limiting one at a time the HashSet would contain [cgc], [appl], [tsla], [msft]. Then, when we attempt to read the HashSet and print to the listModel it would add the contents to listModel like this

example_output_1
[cgc], [appl], [tsla], [msft]
[cgc], [appl], [tsla], [msft]
[cgc], [appl], [tsla], [msft]
[cgc], [appl], [tsla], [msft]

we want our output to be

example_output_2 (what we want)
[cgc]
[aapl]
[tsla]
[msft]

If I remove the line writesOneElement.remove(temp) it stores the HashSet in the listModel like example_output_1. Which is why it makes me think that the DefaultListModel might store data differently.

Currently, with the line writesOneElement.remove(temp) our output is

[]
[]
[]
[]

But, I can verify with Sysout that they are adding to the listModel, but not displaying.

View PostNormR, on 07 April 2019 - 04:37 AM, said:

Can you post the definition of the listModel variable?


Definition of DefaultListModel is "This class loosely implements the java.util.Vector API, in that it implements the 1.1.x version of java.util.Vector, has no collection class support and notifies the ListDataListeners when changes occur. Presently it delegates to a Vector, in a future release it will be a real Collection implementation."

Then, the addElement method says "Adds the specified component to the end of this list."

Lastly, the remove method says "Removes the element at the specified position in this list."

In case you meant declaration...

	DefaultListModel<HashSet<String>> listModel = new DefaultListModel<HashSet<String>>();

This post has been edited by Joshuaparsons18: 07 April 2019 - 07:12 AM

Was This Post Helpful? 0
  • +
  • -

#5 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 765
  • View blog
  • Posts: 5,767
  • Joined: 25-December 13

Re: How does a DefaultListModel store it's elements..?

Posted 07 April 2019 - 07:34 AM

Why add a Set with one element to the list model? And then remove the contents of that Set, leaving a reference to an empty Set in the list model?
Was This Post Helpful? 0
  • +
  • -

#6 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3627
  • View blog
  • Posts: 16,660
  • Joined: 20-September 08

Re: How does a DefaultListModel store it's elements..?

Posted 07 April 2019 - 07:44 AM

Quote

example_output_1

I'm confused about what follows. A representation of a HashSet<String> that contains strings a, b, c would be
[a, b, c]

therefore your very first line implies four different HashSet instances

This post has been edited by g00se: 07 April 2019 - 07:44 AM
Reason for edit:: typo

Was This Post Helpful? 0
  • +
  • -

#7 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 765
  • View blog
  • Posts: 5,767
  • Joined: 25-December 13

Re: How does a DefaultListModel store it's elements..?

Posted 07 April 2019 - 07:54 AM

Quote

Currently, with the line writesOneElement.remove(temp) our output is

[]
[]
[]
[]

That looks like the output for a list with 4 empty Sets.
Or 4 references to an empty Set.
See my last post.

The comments in the code are redundant. The say a call to an add method is adding an element and a call to the remove method is removing an element.

The question that needs to be documented is:
why are those methods being used?
See post#2.

This post has been edited by NormR: 07 April 2019 - 08:21 AM

Was This Post Helpful? 0
  • +
  • -

#8 Joshuaparsons18   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 56
  • Joined: 07-May 16

Re: How does a DefaultListModel store it's elements..?

Posted 11 April 2019 - 08:51 AM

View PostNormR, on 07 April 2019 - 07:34 AM, said:

Why add a Set with one element to the list model? And then remove the contents of that Set, leaving a reference to an empty Set in the list model?


The reason we are adding a Set with one element to the list model is because our listModel takes type HashSet<String>. If when reading a multi string HashSet E.g hashSet = [a], [b] and storing it to the listModel it reads the entire hashSet instead of one index. So the list would read [a], [b]

Image showing the JList after it reads a HashSet (not one at a time).
https://imgur.com/a/uwflNbj

But, you say something that is what I think the key factor is. In the documentation I don't see that DefaultListModel references a set. Especially when reading the documentation for .add() and the naming convention. It is feels to me incredibly misleading to say we are adding to a ListModel. When we are instead then addingToReference(). In that case it makes sense that if after I remove it would not display. Would anybody be able to confirm that DFL references and does not store locally?

EDIT:

Looking at the source code. They start by creating a vector of type<E> named delegate.

    private Vector<E> delegate = new Vector<E>();



Now, we look at addElement(Element e)

    
/**
     * Adds the specified component to the end of this list.
     *
     * @param   element   the component to be added
     * @see Vector#addElement(Object)
     */
    public void addElement(E element) {
        int index = delegate.size();
        delegate.addElement(element);
        fireIntervalAdded(this, index, index);
    }



Here it appears we store the size of our current Vector in an int known as index. Then, we say to add the element we are passing in back to the current element. Now, personally I am having trouble finding a method fireIntervalAdded(), so perhaps it's extended from AbstractListModel<E>. But, in this case still It appears to me that we are storing and not referencing. So why is it when I remove the element from the HashSet the DLM is kinda like "No you don't..."

Any solutions for a work around or a diagnosis as to why?

This post has been edited by Joshuaparsons18: 11 April 2019 - 09:25 AM

Was This Post Helpful? 0
  • +
  • -

#9 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3627
  • View blog
  • Posts: 16,660
  • Joined: 20-September 08

Re: How does a DefaultListModel store it's elements..?

Posted 11 April 2019 - 10:38 AM

Your notation is confusing. Your image clearly shows multiples of

[a, b, c]

Quote

So the list would read [a], [b]


shows sets with one element, which could lead to confusion.

Anyway, Lists can consist of anything. The problem with DefaultListModel is that it's essentially broken for collections. See http://technojeeves....nsnicelistmodel

All you need do is create new CollectionsNiceListModel<HashSet<String>>(hashSet) and use it to create your component

This post has been edited by g00se: 11 April 2019 - 10:39 AM
Reason for edit:: Clarification

Was This Post Helpful? 0
  • +
  • -

#10 Joshuaparsons18   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 56
  • Joined: 07-May 16

Re: How does a DefaultListModel store it's elements..?

Posted 11 April 2019 - 11:20 AM

View Postg00se, on 11 April 2019 - 10:38 AM, said:

Your notation is confusing. Your image clearly shows multiples of

[a, b, c]

Quote

So the list would read [a], [b]


shows sets with one element, which could lead to confusion.

Anyway, Lists can consist of anything. The problem with DefaultListModel is that it's essentially broken for collections. See http://technojeeves....nsnicelistmodel

All you need do is create new CollectionsNiceListModel<HashSet<String>>(hashSet) and use it to create your component


Yes, when the computer stores [a, b, c] it technically is doing what it is supposed to. It read the hash and looped the length of the HashSet. Which I said to do, but this is why I need to only store one hashSet in a temp hash then write to the elemet, so the JList will read

Example_A
[a]
[b]
[c]

and not...

Example_B
[a, b, c]
[a, b, c]
[a, b, c]


Example_C
[]
[]
[]

But, it only displays Example_B if I remove the line

writesOneElement.remove(temp);


Else, if I do not remove the line or comment it out. I get the result for Example_C. I will look into using a different ListModel like you mention above. I do wonder though, in the link you refereced the OP states that it's to prevent having to call addElement in a loop, but I already do this and am not getting the desired result. It is showing the listModel as empty. When I get an opportunity later I will attempt using the other CollectionsNiceListModel<T>. Is there a library I could reference or only the source code?
Was This Post Helpful? 0
  • +
  • -

#11 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 765
  • View blog
  • Posts: 5,767
  • Joined: 25-December 13

Re: How does a DefaultListModel store it's elements..?

Posted 11 April 2019 - 11:24 AM

To get Example_A there needs to be 3 unique Sets, each with one letter.
Example_B shows there is only one Set that has been added 3 times
Example_C shows one Set that is empty that has been added 3 times.

This post has been edited by NormR: 11 April 2019 - 11:25 AM

Was This Post Helpful? 0
  • +
  • -

#12 Joshuaparsons18   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 56
  • Joined: 07-May 16

Re: How does a DefaultListModel store it's elements..?

Posted 11 April 2019 - 11:27 AM

View PostNormR, on 11 April 2019 - 11:24 AM, said:

To get Example_A there needs to be 3 unique Sets, each with one letter.
Example_B shows there is only one Set that has been added 3 times
Example_C shows one Set that is empty that has been added 3 times.


That's why I set a HashSet to store 1 element. Then, add to the JList. Then proceed to remove from the HashSet to add the next new element to the JList. However, after I remove from the HashSet it removes from the JList, but according to the Java source it looks like it should be stored in the vector delegate and referenced there?

I guess I do not understand then why I need to store each ticker in a unique Hash, when I could iterate through each index?

This post has been edited by Joshuaparsons18: 11 April 2019 - 11:29 AM

Was This Post Helpful? 0
  • +
  • -

#13 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 765
  • View blog
  • Posts: 5,767
  • Joined: 25-December 13

Re: How does a DefaultListModel store it's elements..?

Posted 11 April 2019 - 11:30 AM

Quote

it removes from the JList

Please explain?

Try moving the code on line 8 after line 16 so that a unique Set is created and added to the list
for each String. Don't remove the contents of the Set. The results for 3 Strings should be like Example_A
Was This Post Helpful? 0
  • +
  • -

#14 Joshuaparsons18   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 56
  • Joined: 07-May 16

Re: How does a DefaultListModel store it's elements..?

Posted 11 April 2019 - 11:35 AM

View PostNormR, on 11 April 2019 - 11:30 AM, said:

Quote

it removes from the JList

Please explain?

Try moving the code on line 8 after line 16 so that a unique Set is created and added to the list
for each String. Don't remove the contents of the Set. The results for 3 Strings should be like Example_A


What I mean by "It removes from the JList," is that if I display the contents of JList before I remove the element in the HashSet it prints the JList contents correctly, but if I check after I remove the element from the HashSet (writesOneHash) then, it displays that the JList is empty.

I will check out your recommendation this afternoon to re-position the lines from 8 to 16 NormR.
Was This Post Helpful? 0
  • +
  • -

#15 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 765
  • View blog
  • Posts: 5,767
  • Joined: 25-December 13

Re: How does a DefaultListModel store it's elements..?

Posted 11 April 2019 - 11:42 AM

Quote

it displays that the JList is empty.

Where is there a posted display of that?
The posted display shows a list that contains 3 empty Sets.

This post has been edited by NormR: 11 April 2019 - 11:43 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2