Jump to content



Welcome to KnowledgeSutra - Dear Guest , Please Register here to get Your own website. - Ask a Question / Express Opinion / Reply w/o Sign-Up!
- - - - -

Fill Jcombobox


1 reply to this topic

#1 kvarnerexpress

    Super Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 407 posts

Posted 31 October 2005 - 11:08 AM

I am developing a GUI using Netbeans...(Definitely recommended!)...and have a JComboBox which is populated from data from a MySQL database.
I would like to do know two things really...

1. In Netbeans is there somewhere on the properties of the JComboBox where I can point to a method I have that returns a resultset so it automatically knows what I want to be the items within the JComboBox.
I guess I am looking for an option very similar to what exists in Macromedia Dreamweaver and the way you select a textfield for example, point to your recordset/query, and then specify it's default values (possibly a surname from the query)
If someone has come across something like that in Netbeans and could tell me...

2. I have listed my code below, it works so not asking for people to fix it, I just wanted to know if this is the correct way to go about populating the combo box with data. I am new to Java and obviously wery about whether or not I am doing things right.
(Did not include all my GUI stuff)
Code:
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class preadvisorLogon extends javax.swing.JFrame {
    
    /** Creates new form preadvisorLogon */
    public preadvisorLogon() throws  Exception{

        mdbc=new MyDBConnection();
        mdbc.init();
        Connection conn=mdbc.getMyConnection();
        stmt= conn.createStatement();
        ResultSet rs = getAgents();
        
        //THIS BUILDS MY GUI, MUCH CODE TAKEN OUT
        initComponents();
        
        //Fill JComboBox with values from query
        updateJComboBox(rs);
    }
 
    public ResultSet getAgents() {
        ResultSet rs=null;
        try{
            rs=stmt.executeQuery("SELECT `Agent ID`,`Description` FROM `aar");
        }
        catch(SQLException e){}
        return rs;
    }

    public void updateJComboBox(ResultSet rs) throws SQLException {
        while(rs.next())
        {
            jcmbAgents.addItem(rs.getString("Description"));
        }
    }


    
}

thanks,kvarnerexpress

#2 iGuest

    Hail Caesar!

  • Kontributors
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 5,876 posts
  • Interests:Trap17 Free Web Hosting, No Ads

Posted 04 February 2009 - 07:40 AM

populate combobox and run queryFill Jcombobox

The posts here are very helpful.Am new to programming and I need to generate some reports by selecting the items to query for from comboboxes and run using a button.Can someone help me with code to start me off.

Regards-reply by Edna

 






Reply to this topic


This post will need approval from a moderator before this post is shown.

  


1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users