• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JList selection to int not working

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
    Posts: 63
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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.

     
    Bartender
    Posts: 5167
    11
    Netbeans IDE Opera Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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?
     
    Patrick de Kruijf
    Ranch Hand
    Posts: 63
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 63
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 63
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Marshal
    Posts: 28193
    95
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
    Posts: 63
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Marshal
    Posts: 28193
    95
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 63
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You mean I should use "Patrick's Fried Porkheads" as the identifier instead of indexnumber like "0" or "3"?
     
    Paul Clapham
    Marshal
    Posts: 28193
    95
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Don't you have a MenuItem object?
     
    Patrick de Kruijf
    Ranch Hand
    Posts: 63
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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?
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic