?
Solved

Need java Code translation PLZ!

Posted on 2011-10-11
13
Medium Priority
?
284 Views
Last Modified: 2013-12-14
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
0
Comment
Question by:snajalm
  • 5
  • 5
  • 3
13 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 36947534
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?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36947535
It just creates listModel for your JList which wil show strings Item1, Item2, etc
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36947537
Just put values of your NodelIst inside the array strings inside this code
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:CEHJ
ID: 36947546
e.g.
ListModel nodes = new DefaultListModel();
for(int i = 0;i < nodeList.getLength();i++) {
	nodes.add(nodeList.item(i);
}
jList_DataSet.setModel(nodes);

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 36947552

Something like that inside this model:
String[] strings = new String[nl.getLength()];
for(int j=0; j<strings.length; j++)strings[j] = nl.item().getVlaue();

Open in new window

0
 

Author Comment

by:snajalm
ID: 36947565
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!
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36947589
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
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36947629
>>
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
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36947677
I think this should work:

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 ]; }
}

Open in new window

0
 

Author Comment

by:snajalm
ID: 36947705
There is no "add" method in the ListModel or DefaultListModel classes CEHJ.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 36947721
Sorry that should be
DefaultListModel nodes = new DefaultListModel();
for(int i = 0;i < nodeList.getLength();i++) {
	nodes.add(nodeList.item(i);
}
jList_DataSet.setModel(nodes);

Open in new window

0
 

Author Closing Comment

by:snajalm
ID: 36947997
Thanks!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36948094
:)
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
Suggested Courses
Course of the Month16 days, 4 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question