Page 2 of 2 FirstFirst 12
Results 21 to 28 of 28
  1. #21
    aborgeld is offline Senior Member
    Join Date
    Dec 2010
    Location
    The Hague
    Posts
    114
    Rep Power
    0

    Post

    Your right, i've changed it.

    with this code i get what i want only it gives me always the first index back.
    Only AT (from Austria) and not

    AT
    or the second time of loop
    BE

    Java Code:
        List <String> results= Database.DatabaseConnectie.getLocation(); // only hit the db once
    
            StringBuilder sb = new StringBuilder();
            String s=null;
            for (int i= 0; i < results.size(); i++){
                 sb.append(results.get(i));
                 s=sb.substring(0,3);
                 this.jComboBoxLocation.addItem(results.get(i));
            }
            System.out.println(s);
    
        }
    Last edited by aborgeld; 05-08-2011 at 01:49 PM.

  2. #22
    aborgeld is offline Senior Member
    Join Date
    Dec 2010
    Location
    The Hague
    Posts
    114
    Rep Power
    0

    Default

    I only seem to get the first location and i'm doing the same with filling the combobox and there it works ok

  3. #23
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,778
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by aborgeld View Post
    Your right, i've changed it.

    with this code i get what i want only it gives me always the first index back.
    Only AT (from Austria) and not

    AT
    or the second time of loop
    BE

    Java Code:
        List <String> results= Database.DatabaseConnectie.getLocation(); // only hit the db once
    
            StringBuilder sb = new StringBuilder();
            String s=null;
            for (int i= 0; i < results.size(); i++){
                 sb.append(results.get(i));
                 s=sb.substring(0,3);
                 this.jComboBoxLocation.addItem(results.get(i));
            }
            System.out.println(s);
    
        }
    You only print String s once after the loop has finished so it only prints the last item added to your JComboBox ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #24
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    Looks to me like you didn't read and digest what Jos wrote.

    Quote Originally Posted by JosAH View Post
    You populate your JComboBox with Airport objects; those objects have a toString() method (you must've written it yourself) that show <code> : <country> as their String representation. When you fetch a selected element from your JComboBox you still get an Airport object. It's up to you how you want to handle that.
    getSelectedItem returns an Object which you can cast to Airport.

    The Airport class should have a getID() or similarly named method that returns the two-letter ID.

    So stop making things more complicated by going through Strings where you needn't.

    db

  5. #25
    aborgeld is offline Senior Member
    Join Date
    Dec 2010
    Location
    The Hague
    Posts
    114
    Rep Power
    0

    Default

    Hi Darryl and Burke,

    For me the OO is somethimes still a bit difficult. But i will learn it.
    It works now.

    Thanks for explaining.

    Kind regards,

    André
    PS. Enjoy the weather

  6. #26
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    But has anybody have a good method to get the first 2 or 3 characters out of my arraylist.
    I gave you an example long ago. You should NOT be storing a String with two pieces of data. You should be storing an Object that contains two properties. Then you can format the properties however you want for display purposes and you can access each individual property by adding getter methods.

  7. #27
    aborgeld is offline Senior Member
    Join Date
    Dec 2010
    Location
    The Hague
    Posts
    114
    Rep Power
    0

    Default

    Hi Camickr,

    A good tip in OO programming, thanks. Sorry but that long time ago was the beginning for me.
    Then you learn these things, you start with a string ArrayList.

    But now i'm more advanced and this i had to know, now i never forget ;-).

    Kind regards,

    André

  8. #28
    aborgeld is offline Senior Member
    Join Date
    Dec 2010
    Location
    The Hague
    Posts
    114
    Rep Power
    0

    Default

    Thanks guys i get it to work properly like you said.

    Kind regards,

    André

Page 2 of 2 FirstFirst 12

Similar Threads

  1. populating combo box
    By newto in forum New To Java
    Replies: 11
    Last Post: 11-25-2010, 09:53 AM
  2. Need help: Populating data
    By rahul202 in forum Java Servlet
    Replies: 0
    Last Post: 10-25-2010, 08:16 PM
  3. populating a JList
    By hariza in forum AWT / Swing
    Replies: 2
    Last Post: 09-30-2010, 04:53 AM
  4. Populating a JTable
    By toymachiner62 in forum New To Java
    Replies: 2
    Last Post: 10-13-2009, 06:56 AM
  5. Error when populating the data
    By srinivas_k543 in forum Java Servlet
    Replies: 2
    Last Post: 07-10-2009, 09:29 AM

Posting Permissions

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