JComboBox Issues
I am attempting to make a combo box that holds both an integer key and a text value for each of its selections, a control native to many other languages GUI libraries. This class populates the combo box with values obtained from a table queried using a SQL statement over a JDBC connection. My ultimate goal is to make this a generic class whose constructor will take a SQL query as a string argument and populate the combo box with the result columns.
The current vesion, class EmpMenu, listed below, is intended to populate the combo box with a list of employees using the initEmps() method. This method works correctly when it is called initially from the EmpMenu() constructor, but not when subsequent calls are made from the instantiating class. The drop down menu appears when you click on the combo box, but the down arrow on the right side of the box is missing.
I developed this code as part of a JFrame subclass form, and moved it into its own class. As a component of in a JFrame I could create my combo box instance with JComboBox(Vector). Using a Vector for my list of values the box almost worked right; the down arrow was in its proper place and the first call to initEmps() repopulated the box correctly but subsequent calls caused the dropdown menu to drop down with blank lines until something else unrelated refreshed the display. I couldn't get the vector version working as a stand-alone class; I just learned the hard way that you cannot Vector v = new Vector; and then in your constructor make the call super(v);
I have read everything I can on JComboBox and have found nothing that addresses these issues. Thanks in advance for any help.
import java.awt.*;
import java.sql.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
publicclass EmpMenuextends JComboBox
{
publicstaticfinal String JDBC_DRIVER ="com.mysql.jdbc.Driver";
publicstaticfinal String DATABASE_URL ="jdbc:mysql://localhost/corp";
private Connection con;
private Statement stmt;
private ResultSet rs;
publicclass MenuItem
{
int id;
String name;
public MenuItem(int theId, String theName)
{
id = theId;
name = theName;
}
public String toString()
{
return id +": " + name;
}
}
public EmpMenu()
{
super();
setMaximumRowCount( 5 );
try
{
Class.forName(JDBC_DRIVER);
}
catch( ClassNotFoundException e )
{
JOptionPane.showMessageDialog( null, e.getMessage(),
"Driver Not Found", JOptionPane.ERROR_MESSAGE );
System.exit(1);
}
initEmps();
}
publicvoid initEmps()
{
int id = 0;
StringBuffer name =new StringBuffer(100);
try
{
con = DriverManager.getConnection(
DATABASE_URL,"","");
stmt = con.createStatement();
removeAll();
rs = stmt.executeQuery(
"SELECT emp_id, CONCAT(last, ', ', first) name "+
"FROM emp ORDER BY name");
addItem(new MenuItem(0,"<new employee>"));
while( rs.next() )
{
id = rs.getInt("emp_id");
name.delete(0,100);
name.append(rs.getString("name"));
addItem(new MenuItem(id, name.toString()));
}
con.close();
setSelectedIndex(0);
validate();
repaint();
}
catch( SQLException e )
{
JOptionPane.showMessageDialog( null, e.getMessage(),
"SQL Error", JOptionPane.ERROR_MESSAGE );
System.exit(1);
}
}
publicint getSelectedEmp()
{
MenuItem theItem = (MenuItem) getSelectedItem();
int theId = theItem.id;
return theId;
}
}

