Display Value in JcomboBox


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 8 of 8

Thread: Display Value in JcomboBox

  1. #1
    Join Date
    Sep 2005
    Posts
    22

    Display Value in JcomboBox

    Hi,
    I am new to java.Can u Plz help me.I have a JcomboBox. I want to display all the ItemId from the database in that JComboBox. I don't know how to do that.Can any one plz help me.Can anybody give some sample code

    The code is:-
    public Iterator selectItemID(){
    Iterator itemNo;
    ArrayList itemNumber = null;
    try {
    statement = dbConnection.prepareStatement("select ItemId from ItemDetails");
    ResultSet itemNumberResult = statement.executeQuery();
    while(itemNumberResult.next())
    {
    itemNo = (Iterator) itemNumberResult.getString(1);
    System.out.println("itemNo " +itemNo);
    return (Iterator) itemNo ;
    }
    } catch (SQLException e) {
    e.printStackTrace();
    }
    return (Iterator) itemNumber;

    }

    itemno=new JComboBox();
    DbActivity dbconnection = new DbActivity();

    Iterator result= dbconnection.selectItemID();
    itemno.addItem(result);


    Regards
    Diana

  2. #2
    Join Date
    Aug 2005
    Posts
    17
    I believe the while loop is running only one time. Comment the return statement out.

    For more help, www.NeedProgrammingHelp.com

  3. #3
    Join Date
    Sep 2005
    Posts
    22
    Hi ,
    I commented the return statement. It is printing all the itemId in the Console.but in the combobox it is printing only the last record id. Can u please tell me what to do.

    The code is:-

    public String selectItemID(){
    String itemNo = null;

    try {
    statement = dbConnection.prepareStatement("select ItemId from ItemDetails");
    ResultSet itemNumberResult = statement.executeQuery();
    while(itemNumberResult.next())
    {
    itemNo = itemNumberResult.getString(1);
    System.out.println("itemNo " +itemNo);
    //return itemNo ;
    }
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return itemNo;

    }
    itemno=new JComboBox();
    DbActivity dbconnection = new DbActivity();

    String result= dbconnection.selectItemID();
    itemno.addItem(result);
    System.out.println("Result "+result);

  4. #4
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    You are creating a new comboBox every time you run through the loop.

    Have you tried to create one comboBox (called something, say "myComboBox"), and then when a new item number is chosen, call myComboBox.addItem(itemNo)? You won't create a new combo box, and you'll be adding the new item numbers to the item list in the box.

  5. #5
    Join Date
    Aug 2005
    Posts
    23
    Quote Originally Posted by daina
    Hi,
    I am new to java.Can u Plz help me.I have a JcomboBox. I want to display all the ItemId from the database in that JComboBox. I don't know how to do that.Can any one plz help me.Can anybody give some sample code
    http://www.codeguru.com/forum/showthread.php?t=356834

    ---------------
    Naveen Vooka
    www.devsquare.com
    DevSquare - Online Application Development

  6. #6
    Join Date
    Sep 2005
    Posts
    22
    Hi

    I used Vector.but the records are not displaying in the jcombobox.It is printing the values in the console. I think the problem is with the line ' itemno=new JComboBox(result);' I am getting the values from the selectItemID() method.but it is not assigning it to the jcombobox.

    the code is:-
    public Vector selectItemID(){
    Vector result = new Vector();

    try {
    statement = dbConnection.prepareStatement("select ItemId from ItemDetails");
    ResultSet itemNumberResult = statement.executeQuery();
    while(itemNumberResult.next())
    {
    String itemNo = itemNumberResult.getString(1);
    System.out.println("itemNo " +itemNo);
    result.add(itemNo); // add all found itemNos
    }
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return result;

    }


    DbActivity dbconnection = new DbActivity();

    Vector result= dbconnection.selectItemID();
    itemno=new JComboBox(result); // construct Combo with Vector
    //System.out.println("Result "+result);

  7. #7
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    You have the line "return result;" in your code. To what are you "returning" the Vector "result"?

    It sounds like you are populating the Vector - (have you tried to print out the values stored in the Vector to make sure that it is populated) - but it looks to me that there is no connection between the Vector "result" you are populating and the "result" you are passing to the JComboBox constructor.

  8. #8
    Join Date
    Oct 2007
    Posts
    1

    can any body help me to Populate JComboBox by Vector() present in different Class

    Actually I am inserting vlaue into Vector() in one class and I want to extract particular item out of it into JCombox()

Similar Threads

  1. Replies: 0
    Last Post: 04-12-2005, 02:27 PM
  2. curved bargraph type display
    By thebluearcher2 in forum VB Classic
    Replies: 0
    Last Post: 03-03-2005, 11:38 AM
  3. Display XML element attributes in CSS or XSL
    By Allen Pitts in forum XML
    Replies: 0
    Last Post: 10-31-2002, 03:02 PM
  4. Replies: 0
    Last Post: 11-02-2000, 08:54 AM
  5. msChart don't display xY chart
    By joni in forum VB Classic
    Replies: 0
    Last Post: 07-13-2000, 05:19 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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center