Results 1 to 5 of 5
  1. #1
    Manfizy's Avatar
    Manfizy is offline Senior Member
    Join Date
    Nov 2008
    Location
    Germany
    Posts
    193
    Rep Power
    6

    Unhappy How to populate a JcomboBox with dynamic data from database

    Hi guyz,
    I want to populate a jcomboBox with data from database everytime i run y program. I am able to get the list from a database but am unable to display it in the combobox.

    Any ideas on how i can achieve this will be highly appreciated.

    Am using netbeans.

    Cheerz!!
    We Learn Through Mistakes..,
    Manfizy:rolleyes:

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

    Default

    How do you try to display it in the JComboBox? Do you use a DefaultComboBoxModel object? Can we see your code?

  3. #3
    Manfizy's Avatar
    Manfizy is offline Senior Member
    Join Date
    Nov 2008
    Location
    Germany
    Posts
    193
    Rep Power
    6

    Post

    Quote Originally Posted by Fubarable View Post
    How do you try to display it in the JComboBox? Do you use a DefaultComboBoxModel object? Can we see your code?
    Well; i have not used the DefaultComboBoxModel object because i do not know how to use it.
    Below is the code that am using
    Java Code:
    public void populateComboBox(){
            String query;
           // Vector fileList = new Vector();
            try {
                statement = getConn.getConnection().createStatement();
                query = "SELECT AcademicYears FROM PathConfig ";
    
                resultSet = statement.executeQuery(query);
    
                ResultSetMetaData rsmdt = resultSet.getMetaData();
                int columns = rsmdt.getColumnCount();
    
                while(resultSet.next()){
                Vector row = new Vector();
    
                for(int i = 1; i <= columns; i++){
                   //row.addElement(resultSet.getString(i));
                   fileList.addElement(resultSet.getString(i));
                 }
                //fileList.addElement(row);
                }
                statement.close();
                getConn.conn.close();
            } catch (SQLException ex) {
                Logger.getLogger(FrmTranscript.class.getName()).log(Level.SEVERE, null, ex);
            }
            System.out.println("FILE LIST :"+fileList);
                   
            jComboBox3 = new JComboBox(fileList);
            
        }
    We Learn Through Mistakes..,
    Manfizy:rolleyes:

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

    Default

    I would create a class to hold each row's worth of data and give the class a meaningful toString() method for this is what the combo box will display. I'd then create a DefaultComboBoxModel object and iterate through the resultset creating a new object of the row class and then placing these objects into the model via its addElement method. I'd then make my combo box's model my filled model object.

  5. #5
    Manfizy's Avatar
    Manfizy is offline Senior Member
    Join Date
    Nov 2008
    Location
    Germany
    Posts
    193
    Rep Power
    6

    Default

    Quote Originally Posted by Fubarable View Post
    I would create a class to hold each row's worth of data and give the class a meaningful toString() method for this is what the combo box will display. I'd then create a DefaultComboBoxModel object and iterate through the resultset creating a new object of the row class and then placing these objects into the model via its addElement method. I'd then make my combo box's model my filled model object.
    Thanks for your reply. Used the DefaultComboBoxModel and got my problem solved.
    We Learn Through Mistakes..,
    Manfizy:rolleyes:

Similar Threads

  1. populate jCombobox with database data
    By joeyxaza in forum JDBC
    Replies: 2
    Last Post: 01-06-2014, 10:09 AM
  2. Dynamic data model in JComboBox
    By agreed in forum AWT / Swing
    Replies: 5
    Last Post: 11-15-2009, 08:30 PM
  3. Help Pls!! Jcombobox populate with mysql
    By kwink in forum AWT / Swing
    Replies: 1
    Last Post: 03-23-2009, 04:11 AM
  4. populate jCombobox with database data
    By joeyxaza in forum JDBC
    Replies: 0
    Last Post: 01-19-2009, 04:30 PM
  5. how to populate html:select with values from database
    By sathya_k_83 in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 11-07-2007, 10:53 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
  •