Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: problem using vectors and jcomboboxes

  1. #1
    Paul_White is offline Member
    Join Date
    Apr 2011
    Posts
    15
    Rep Power
    0

    Question problem using vectors and jcomboboxes

    I am working on a program that integrates with mysql.

    I am querying the database and using a vector to populate a JcomboBox with the results . This is done as the GUI gets built and is working fine. Based on the item chosen in the first JcomboBox I want to popultate the second JcomboBox. I am using an action listener on the first JcomboBox, calling another class, passing the item selected in the first box, doing the query and returning the result as a vector.

    As I used a vector in the first case I thought I could just call

    box2.addItem(vector2);

    My problem is that I get the result of the query displayed as a single line (one entry) enclosed by []

    I am puzzled because JComboBox Box1 = new JComboBox(vector1); works to set up a JcomboBox at intitialisation but I cannot add a vector in the same way to an empty box after it has been made visible.

    I can input single lines using box2.addItem("new line etc"); but I want to add a list as I have done successfully using a vector like in box 1.

    Could someone give me a hint as to what I am doing wrong?

    Thank you

    Paul

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Box 1 uses a constructor. The addItem(...) is adding the string representation of the vector. You can use a loop to add each item of the vector to the jcombobox.

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

    Default

    Quote Originally Posted by Paul_White View Post
    As I used a vector in the first case I thought I could just call

    box2.addItem(vector2);

    My problem is that I get the result of the query displayed as a single line (one entry) enclosed by []

    I am puzzled because JComboBox Box1 = new JComboBox(vector1); works to set up a JcomboBox at intitialisation but I cannot add a vector in the same way to an empty box after it has been made visible.
    You can call it an inconsistency (or omission) in the API for the JComboBox class but the addItem( ... ) method just adds one single item to the list (as its name implies); you can't add all elements of a Vector to a JComboBox with just one method call. You have to call that method over and over again for each element in the Vector; something like this:

    Java Code:
    for (Object item: yourVector)
       box2.addItem(item);
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default

    I'd consider it better style to construct a new model and set it to the combo.
    Java Code:
    comboBox.setModel(new DefaultComboBoxModel(vector));
    db
    Fubarable likes this.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default

    Quote Originally Posted by sunde887 View Post
    The addItem(...) is adding the string representation of the vector.
    No, it's adding the vector. The default renderer uses the String returned from toString().

    db

  6. #6
    Paul_White is offline Member
    Join Date
    Apr 2011
    Posts
    15
    Rep Power
    0

    Default Thank you

    Guys,

    Lots of good advice as always - Thank you all.

    Darryl from all the answers given I liked the look of your answer (It seemed to me to be the closest to the way I wanted it coded) and it works like a charm!

    Thanks again.

Similar Threads

  1. Problem with Vectors
    By travist120 in forum Threads and Synchronization
    Replies: 4
    Last Post: 04-04-2011, 10:02 AM
  2. Vectors Problem
    By dashwall in forum New To Java
    Replies: 9
    Last Post: 01-04-2010, 11:16 PM
  3. problem with JComboBoxes?
    By swayam07 in forum New To Java
    Replies: 4
    Last Post: 08-27-2008, 05:41 AM
  4. problem with Vectors and getTotal() function
    By java_fun2007 in forum New To Java
    Replies: 2
    Last Post: 11-23-2007, 01:55 PM
  5. Problem with vectors in java
    By toby in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:56 AM

Tags for this Thread

Posting Permissions

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