File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes JList selection to int not working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "JList selection to int not working" Watch "JList selection to int not working" New topic
Author

JList selection to int not working

Patrick de Kruijf
Ranch Hand

Joined: Mar 02, 2010
Posts: 63
I want to get the index number from the JList list and convert it to a int so I can use it as argument for a setter method in another class called menukaart. menukaart.setMenuId.

I've read the api a dozen times, googled for hours and hours and used the search engine on the ranch here. I must say I learned a lot, and I think I'm almost there, but I don't get any further.

All my other code seem to work; When I make a extra textfield and fill in an indexnumber by hand, and use that as an argument for the menukaart.setMenuId, I can remove data from the list just fine. It just won't work by selection in the list itself.

What am I doing wrong?



Thanks in advance for your time and effort!


------------------8<------------------
please cut here
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

There's a couple of things missing from your problem description:

  • You didn't say what the code was supposed to do
  • You didn't say what it does instead


  • Patrick de Kruijf
    Ranch Hand

    Joined: Mar 02, 2010
    Posts: 63
    I have to make a program with wich I can fill a menu database for a restaurant.

    I have three classes to get this working:

    - class GUI: This class has 4 textfields (name of food, price, ingredients etc.), 4 buttons to add, delete and update records, and a JList wich displays all the menus in the sql database.
    - class MenuKaart: This class holds the methods for adding, deleting and updating records from the database and a vector method wich displays the sql tables on a jlist.
    - class SQLConnector: This class connects to the database.

    Most things work fine, I can add records to the database through the GUI just. The thing is I can't delete or update them, because then I have to know the indexnumber of the selection in the JList.

    I want to know why the index number from the JList in class GUI is not passed to the menukaart.setMenuId() method. That's why I didn't write out the whole purpose of the program, I thought it was irrelevant.

    Darryl Burke
    Bartender

    Joined: May 03, 2008
    Posts: 4531
        
        5

    Add a sysout for debugging.
    And you still haven't expounded on the difference between your expectations and the actual result.

    edit Did you add the listener to the list?


    luck, db
    There are no new questions, but there may be new answers.
    Patrick de Kruijf
    Ranch Hand

    Joined: Mar 02, 2010
    Posts: 63
    I did add the listener to the list, this is a part from the code in my first post:



    I already tested it with a sysout. But not like you advise, in my test I just let the code print something when it registered a selection in the JList and that worked. But I never tried to let it print out the SelectionIndex(). I will try this, thanks!

    I'm sorry that I still didn't tell what my expactation are, I thought I did, but I guess it was not clear at all. Here is anor try:

    I expect that when I select a row in the JList, the indexnumber of the selected record in the jlist is used as a argument for the setMenuID method of the class MenuKaart. If that works, I can select a row in the JList, hit the delete button from the GUI class that runs a sqldelete statement in the menukaart class, so that the selected record is deleted from the connected sql database.

    The actual result is that I can select a record in the JList, hit the delete button, but the record stays in the database.

    The connection with the database works fine, everything runs fine really, I just can't get the indexnumber by selecting a row in the jlist.

    Thanks again for your precious time and effort, I hope that this time you understand what I mean, I really wouldn't know how to describe my question in an other way.
    Patrick de Kruijf
    Ranch Hand

    Joined: Mar 02, 2010
    Posts: 63
    Please don't focus on my posts above, I think I know what's going wrong;

    I have a JList that displays the contents of a sql database.
    I want to be able to delete a single table by selecting the table in the JList and hit a delete button.
    To get this done I need the JList to give me the indexnumber of the selected entry, this all works fine, I tested this. When I select the first row, a system.out.println replys 0 and the second row does 1.

    I thought I could translate the indexnumber straight to the id number of the database, so the table would be deleted;

    Here is the JList listener class in the class gui

    Here is the 'delete table' method in the class MenuCard


    The problem is that the id that the sql database gives to a table, doesn't sync with the jlist. The first row in the JList always has 0 as the indexnumber, but the corresponding sql table does not always have 0 or 1.

    So when I say, delete the table, my code takes the indexnumber of the jlist and searches for a sql table with a indexnumber of 0, but that one doesn't exist. Because the sql database just keeps on incrementing the primary key: menu_id.

    How can I delete a sql table by selecting the corresponding JList entry? Any tips would come in very handy!

    Thanks again for your time and effort,

    Patrick
    Patrick de Kruijf
    Ranch Hand

    Joined: Mar 02, 2010
    Posts: 63
    I've come a little further again, my questions are getting better I hope:

    How can I sync the indexnumber of a jlist entry to the primary key number of the database row the the jlist is supposed to display?

    I'm looking for something like this:

    jlist.setIndexNumber(sqltable.primarykey);
    Paul Clapham
    Bartender

    Joined: Oct 14, 2005
    Posts: 18541
        
        8

    Patrick de Kruijf wrote:I've come a little further again, my questions are getting better I hope:

    How can I sync the indexnumber of a jlist entry to the primary key number of the database row the the jlist is supposed to display?


    Yes, much better. That's a good question in that it illustrates a not-so-good design choice. Namely, why are you using the index number of the selected JList entry? Why don't you store objects in the JList which can be used to identify the corresponding database key? (Maybe you already are.) And then actually get the selected JList entry, and not just its index number?
    Patrick de Kruijf
    Ranch Hand

    Joined: Mar 02, 2010
    Posts: 63
    Hello Paul, thank you so much for your reply, I'm learning a lot here . . .

    I was planning on making an extra collumn in the database that stores a copy of the getSelectedIndex(); variable of each JList entry. So when I want to delete a table from the database, I can search for a table with exactly the indexnumber in that collumn.

    Is that what you mean?

    --update--

    Well after reading your reply again, I guess my idea is exactly the opposite of what you mean. Can you help me a bit on my way please? Do you have an example please?

    Thanks again,

    Patrick
    Paul Clapham
    Bartender

    Joined: Oct 14, 2005
    Posts: 18541
        
        8

    No, don't make it more complicated. Make it less complicated.

    You have menu items, right? And you want to display a list of them for people to choose from? And given a menu item, can you get from it the key to use to delete it from that database?

    I guess that is all true. In that case you should put the menu items into the JList. Then when one is selected, you get the menu item itself from the JList, not just its index in the list. Pass that menu item to the method which is going to do the database deletion, not just an integer which fails to identify it nicely.
    Patrick de Kruijf
    Ranch Hand

    Joined: Mar 02, 2010
    Posts: 63
    You mean I should use "Patrick's Fried Porkheads" as the identifier instead of indexnumber like "0" or "3"?
    Paul Clapham
    Bartender

    Joined: Oct 14, 2005
    Posts: 18541
        
        8

    Don't you have a MenuItem object?
    Patrick de Kruijf
    Ranch Hand

    Joined: Mar 02, 2010
    Posts: 63
    I use JButtons to add, update and delete records from the database, can I use them the same as MenuItems? How would I use them within a JList?
     
     
    subject: JList selection to int not working