Results 1 to 12 of 12
  1. #1
    dave247's Avatar
    dave247 is offline Member
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Default accessing custom data model values - how?

    I created a custom data model. Now I want to make a loop statement that can go through each item[] and access any or all of the three fields of data (I am trying to output things to a JList). I am not sure how I would write a statement to do this because I don't fully understand the basics of OOP.

    So, if I wanted to access the price value, which is p, would I do something like:

    For each invItem in items[], print items.p or something? How do I do this?

    Here is my custom data model:
    Java Code:
    class invItem
    {
     int itemNum; 
     String desc;
     double price;
     
     public invItem (int i, String d, double p)
      {
       itemNum = i; desc = d; price = p;
       }
    }
      
    invItem items[]= {
     new invItem(1001, "WD Hard Drive", 49.99),
     new invItem(1002, "Sony Head Phones", 34.99)};
    Thanks
    Last edited by dave247; 03-02-2010 at 04:20 PM. Reason: neatness

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    6

    Default

    Well your Item has 3 pieces of information so you wouldn't display them in a JList, you would use a JTable. Read the JTable API and follow the link to the Swing tutoral to understand the basics of using a JTable.

    In order to add your Item to the model you will need to create a custom TableModel.

    For a more advanced approach at using a generic TableModel for your Items you can then check out the Bean Table Model. But you have a lot of reading and experimenting with a basic JTable before you will understand how this solution works.

  3. #3
    dave247's Avatar
    dave247 is offline Member
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Default

    Thanks for your help but this is part of an assignment I have, and we were asked to put only one of the pieces of data (the description) into a JList from the custom data model. The user will be able to click the various entries in the JList.

    We already went over tables in class but we aren't using them on this assignment.

    I am asking how I would word a statement to access values from that custom data model. Someone please help me understand how to do this.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Class invItem should be capitalized -- InvItem -- since it represents a class. Make sure that you give it a public String toString() method that returns the String that you want the JList to display. For instance, if you want the JList to just display that description of the item, then have toString return the desc field's String.

    Other than that, I'm not 100% sure what you're asking for here. Surely you know how to write a for loop, and surely you know how to call the getter methods of your InvItem objects (they do have getXXX() methods, right?). So where are you stuck?

    For instance, this:
    I am asking how I would word a statement to access values from that custom data model.
    is unclear to me.

  5. #5
    dave247's Avatar
    dave247 is offline Member
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Default

    Sorry if I was unclear before.

    What I am trying to do is create a JList that displays only the description of an item. The actual items are stored in a custom data model with fields for the item number, description and price. I have several entries for the custom data model and I need to make a loop statement that will populate a JList with each item's description.

    I have made the invItem class and made an item[] object that is full of entries...

    Now I need to make a loop statement that is going to iterate through each item and each item's fields. I know how to make a loop statement but I dont know how to interact with each individual field, such as the item description, since I want to grab that value and output it to my list and then move on to the next one...

    This is obviously wrong, but thats why I am asking for an example of how to do it:
    Java Code:
    foreach (invItem (items[]))
    {
    list.addItem(String.valueOf(d));
    }
    (For each invItem in the items[] thing, add the description field to the JList)

    Hopefully I am not making this harder than it has to be... it's spring break for God's sake but my instructor gave this assignment to us anyway and he's not replying to my emails asking for help. Also I have to leave for work now so I am crossing my fingers for some good responses when I get back in 9 hours.

    Thank you all for your help
    dave

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Just add the invItem objects to your JList's model (usually a default list model). As long as you've given invItem a proper toString() method as I described above, the JList will show only the description.

  7. #7
    dave247's Avatar
    dave247 is offline Member
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Default

    yeah... that solution doesn't make any sense, nor does it work.

    I was hoping someone with Java experience would be able to help me... apparently there is no one like that here.

  8. #8
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Nobody will do your work for you. Fubarable's solution is simple and easy.

    IgnoreList++
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,309
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by dave247 View Post
    yeah... that solution doesn't make any sense, nor does it work.
    That solution does make sense and it can work if that InvItem class implements a suitable toString() method.

    Jos

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by dave247 View Post
    yeah... that solution doesn't make any sense, nor does it work.
    Then you should show what you tried and the error messages that it caused. Telling us that it doesn't work doesn't tell us what you're doing wrong or why.

    I was hoping someone with Java experience would be able to help me... apparently there is no one like that here.
    If any part of my suggestions were unclear, the smart thing for you to do would be to ask a specific question about just what you don't understand. Insulting someone who is volunteering time to try to help you, who has made no personal attack on you, is not going to get you any closer to a solution, but will be guaranteed to get you placed on my and many other's do-not-help list.

    Good luck in Java and in life my friend.
    Last edited by Fubarable; 03-03-2010 at 04:04 PM.

  11. #11
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    6

    Default

    that solution doesn't make any sense, nor does it work.
    The answer given was correct, simple to understand and easy to implement. If it isn't the solution you are looking for then the problem is the context of your question because everybody else knows exactly what Pete is suggesting.

    but will be guaranteed to get you placed on my and many other's do-not-help list.
    Java Code:
    addToIgnoreList( dave247  );
    Last edited by camickr; 03-03-2010 at 04:42 PM.

  12. #12
    dave247's Avatar
    dave247 is offline Member
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Default

    sorry for being an A-hole, but I was super stressed out with trying to get this assignment done.

    All I needed to do was this:
    Java Code:
    		
    for (int i=0; i<items.length; i++)
    {
        model.addElement(items[i].desc);
    }
    What I was asking was simply how to properly word the statements so that data flowed correctly. For example, I wrote items[i].desc and not items[i].d and I had to type items.length for the size of the model. Also I could not just use a JList. I had to use a DefualtListModel because of certain issues with swing and awt or whatever.

    I wasnt asking people to do my work for me... its just that direct answers, usually with code examples best help me understand what is going on and what i need to do.

Similar Threads

  1. Accessing custom email headers in bounced back emails
    By pmalladi in forum Advanced Java
    Replies: 0
    Last Post: 10-28-2009, 11:31 AM
  2. Replies: 4
    Last Post: 04-15-2008, 01:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •