File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Fill java.awt.Choice from a List generated by a resultSet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Fill java.awt.Choice from a List generated by a resultSet" Watch "Fill java.awt.Choice from a List generated by a resultSet" New topic
Author

Fill java.awt.Choice from a List generated by a resultSet

Ron Ingram
Ranch Hand

Joined: Mar 11, 2011
Posts: 63
Hello,

I'm trying to drill into a selection on a swing application. I'm calling the database running a select statement and putting the resultset into a List. From there, I want to get the value in index(3) for each iteration of the List. The problem is, I get only the first value correctly, but not the next iterations...

Can someone please review the code and let me know what I'm doing wrong? Thanks!

ActionPerformed..


Database Helper


Olivier Legat
Ranch Hand

Joined: Nov 17, 2007
Posts: 176

Hi Ron,

Do you get an exception? If so, please post the stacktrace.


Olly
Ron Ingram
Ranch Hand

Joined: Mar 11, 2011
Posts: 63
Still working on it.. No exception. Just doesn't work right. I get the same value 3 times...
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

CASE_SELECT returns a list with 11 elements per row; if there are 5 rows it will have 55 elements. That's because you add 11 column values per row.
In control_choiceItemStateChanged you keep getting the 4th element of that list. That's always the same.

I see two approaches:
1) Don't add 11 column values per row. Instead, create a new class for containing the values of each row. In CASE_SELECT you then create one instance for each row, and add that instance to the List. In control_choiceItemStateChanged you then get each element of the List, and get the right value from that element.

2) First get the 4th element (get(3)). Then get the 15th (get(14)). Next is the 26th (get(25)). Etc. In other words, increase the index with 11 each time.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ron Ingram
Ranch Hand

Joined: Mar 11, 2011
Posts: 63
Rob,

Thanks for the tip. Would you mind explaining the first recommendation for me? I'm trying to go that route.. But still no prevail. If I statically call each column needed by adding 11, it works fine. The problem is that the result will change so I need a populated dynamic way to get the 3rd element in each iteration..

Ron
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

Of course "MyClass" isn't a good name, so replace it with something else. I also added a bit more generics.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Fill java.awt.Choice from a List generated by a resultSet