Jump to content
xisto Community
Sign in to follow this  
kvarnerexpress

Fill Jcombobox

Recommended Posts

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

Share this post


Link to post
Share on other sites
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

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.