Clicky

Can someone please translate this line of code for me, taken from Netbeans JList autogenerated code!  I just can't make anything out of this but Netbeans understands this!  

I'd like to modify this code so I could populate this list object with values of a "NodeList" object retreived from another getter method returning "NodeList" type!  I'd appreciate it if you could also help me perform this task!

jList_DataSet.setModel(new javax.swing.AbstractListModel() {
    String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
    public int getSize() { return strings.length; }
    public Object getElementAt(int i) { return strings[ i ]; }
});

regards

asked 10/11/2011 04:44

snajalm's gravatar image

snajalm ♦♦


13 Answers:
All you need do is replace the ListModel of jList_DataSet with one of your own. Are you talking about the jdk api NodeList class?
link
CEHJ's gravatar image

CEHJ

It just creates listModel for your JList which wil show strings Item1, Item2, etc
link
for_yan's gravatar image

for_yan

Just put values of your NodelIst inside the array strings inside this code
link
for_yan's gravatar image

for_yan

e.g.
1:
2:
3:
4:
5:
ListModel nodes = new DefaultListModel();
for(int i = 0;i < nodeList.getLength();i++) {
	nodes.add(nodeList.item(i);
}
jList_DataSet.setModel(nodes);
link
CEHJ's gravatar image

CEHJ


Something like that inside this model:
1:
2:
String[] strings = new String[nl.getLength()];
for(int j=0; j<strings.length; j++)strings[j] = nl.item().getVlaue();
link
for_yan's gravatar image

for_yan

I know what it does but I don't understand this construct in specific!  It's instantiating an AbstractListModel but how can you come up with the following "{" and the items in there which I have no clue how they're put together this way and why're they there for!

new javax.swing.AbstractListModel() {
    String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
    public int getSize() { return strings.length; }
    public Object getElementAt(int i) { return strings[ i ]; }
}

And yes "CEHJ", that's the NodeList from jdk api!
link
snajalm's gravatar image

snajalm

If you make your NodeList instance variable - II guess you can access it insioed the anonymous class
- then just assign the vluaes of your node to Strings array - something like I posted maybe with some modifications deopending on particular NodeList
- I think I used this from ww3c.dom


You can create your own class - just extend DefaultListModel - it is much more understandable than with these anonymous things
link
for_yan's gravatar image

for_yan

>>
It's instantiating an AbstractListModel but how can you come up with the following "{" and the items in there which I have no clue how they're put together this way and why're they there for!
>>

It's just making an anonymous inner class model for the benefit of its gui builder
link
CEHJ's gravatar image

CEHJ

I think this should work:

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
NodeList nl; //declare it as an instance variable


new javax.swing.AbstractListModel() {
//    String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
String[] strings = new String[nl.getLength()];
for(int j=0; j<strings.length; j++)strings[j]=nl.item().getNodeValue();

    public int getSize() { return strings.length; }
    public Object getElementAt(int i) { return strings[ i ]; }
}
link
for_yan's gravatar image

for_yan

There is no "add" method in the ListModel or DefaultListModel classes CEHJ.
link
snajalm's gravatar image

snajalm

Sorry that should be
1:
2:
3:
4:
5:
DefaultListModel nodes = new DefaultListModel();
for(int i = 0;i < nodeList.getLength();i++) {
	nodes.add(nodeList.item(i);
}
jList_DataSet.setModel(nodes);
link
CEHJ's gravatar image

CEHJ

Thanks!
link
snajalm's gravatar image

snajalm

Your answer
[hide preview]

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Tags:

×141
×11

Asked: 10/11/2011 04:44

Seen: 356 times

Last updated: 10/10/2011 11:51