Results 1 to 5 of 5
  1. #1
    skatefreak is offline Member
    Join Date
    Jan 2009
    Posts
    18
    Rep Power
    0

    Default Jcombobox not liking String input for options :|

    Hey there,
    Slight issue here where i have a loop pulling usernames out of a mysql database and creating a string which outputs all the usernames and connects them to form a string such as "name1" , " name2" , "name3" , "name4" , etc.

    This String is then put in a JCombobox ("concatonated" ?!) for example.
    in the GUI class,
    instead of:
    Java Code:
    String[] cmbouser = {" Select username " , " Joe " , " Mike " , " Valsilis " ,};
    I have, in the first class:
    Java Code:
    custusers = Data.getcustusers();
    String[] cmbouser = {" Select username " , custusers};
    Custusers is technically equal to {" Joe " , " Mike " , " Vasilis " ,} so does anyone know why the combobox ends up just displaying first { Select username } and then second on the list there is {" Joe " , " Mike " , " Vasilis " ,} as the only second option :|

    I'm aiming to have all the users pulled out of the sql database on the combobox list.

    Am i going about this in the wrong way?

    Is there any way around this combobox issue?

    Oh and lastly, i dont suppose anyone has any pointers to any material on Jcombobox action listeners and such?

    Am looking forward to any replies and am starting to read posts hoping i can start giving back someday soon if i ever *get there* heh.

    Best regards

    -Jvr

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

    Default

    What is the customers object exactly?

    You say,
    Custusers is technically equal to {" Joe " , " Mike " , " Vasilis " ,}
    but it's likely a single String, and this isn't an array. Even if it were, you're code wouldn't work.

    Remember programming is unforgiving. When the API tells you that if you pass an Array of objects into a combo box constructor, you will see the toString representation of those objects, you have to do exactly that. It appears that you are passing an array of two Strings into this combo box, the first being " Select username ", and the second being a String containing concatenated other Strings. The combo box will then do exactly as you told it to: it will display the two Strings.

    What you want to do instead is create a single array with all the strings, probably starting with " Select username " and then containing all the other Strings each as a separate item in the array. This may require a for loop to create. Then use this array to initialize your combo box.
    Last edited by Fubarable; 05-10-2009 at 11:13 PM.

  3. #3
    skatefreak is offline Member
    Join Date
    Jan 2009
    Posts
    18
    Rep Power
    0

    Default

    Hey there,
    As usual, your blinding with your logic heh.
    As you say, i'm using a while loop which pulls out each user in turn and encloses them in the required character dressing:
    Java Code:
    while (rs.next()) 
                {
                    String b = rs.getString("fname");
                    String c = rs.getString("lname");
              .... etc etc....
                    users = users + " \" "+ b +" \",";
                        }
    Mmm,
    Back to arrays i guess.

    I shall look into storing and initialising from an array :)

    Muchos gracious :)

    -Jvr

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

    Default

    Or another option is to use a Vector<String> as a combo box can use this in its constructor. You could add your Strings from the dataset and then later append your " select..." String to index position one.

    Another option is to use a DefaultComboBoxModel

  5. #5
    skatefreak is offline Member
    Join Date
    Jan 2009
    Posts
    18
    Rep Power
    0

    Default

    Hey,
    Muchos gracious :)
    Am off to learn about vectors now i believe.

    -Jvr

Similar Threads

  1. [SOLVED] Reading an input string?!
    By sfe23 in forum New To Java
    Replies: 6
    Last Post: 02-23-2009, 05:38 AM
  2. How to Mask input string
    By nmc.091 in forum New To Java
    Replies: 1
    Last Post: 02-23-2009, 04:49 AM
  3. Prompting user input of a string.
    By apfroggy0408 in forum New To Java
    Replies: 3
    Last Post: 03-09-2008, 07:23 PM
  4. ComboBox with database options
    By Goldy in forum Advanced Java
    Replies: 0
    Last Post: 12-01-2007, 10:43 PM
  5. problem with Java Vm options
    By DonnieDarko in forum Advanced Java
    Replies: 1
    Last Post: 11-21-2007, 04:22 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
  •