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;

}

}

[6057 byte] By [aslan23a] at [2007-10-2 0:28:23]
# 1
I don't understand why so many people automatically extend a GUI class like JComboBox. Why not look into a ComboBoxModel.
walker8a at 2007-7-15 16:42:52 > top of Java-index,Java Essentials,New To Java...
# 2
Because there's no mention in my Java textbook of a ComboBoxModel?
aslan23a at 2007-7-15 16:42:52 > top of Java-index,Java Essentials,New To Java...
# 3
try reading the API's
DarkNomada at 2007-7-15 16:42:52 > top of Java-index,Java Essentials,New To Java...
# 4

Been there, done that. So I know that its a subclass of ListModel and has methods getSelectedItem() and setSelectedItem. That doesn't tell me how to create and use the thing.

If anyone has a code example of how I add, clear, and re-add items to a JComboBox using a ComboBoxModel I'd be grateful to see it. I couldn't find anything useful on the web.

aslan23a at 2007-7-15 16:42:52 > top of Java-index,Java Essentials,New To Java...
# 5
sure, look into this classi'm working on some other links(will have them shortly) http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/DefaultComboBoxModel.html
walker8a at 2007-7-15 16:42:52 > top of Java-index,Java Essentials,New To Java...
# 6

It would be well worth the time to read up on JTables

http://java.sun.com/docs/books/tutorial/uiswing/components/table.html

then apply the model concept to your JComboBox. the reason i mention

this is because there are more detailed examples in the case of JTable

and the concept is extremely similar.

specifically look at description of table models

http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#data

and find examples here

http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#eg

I don't have time right now to come up with anything else but will try later.

walker8a at 2007-7-15 16:42:52 > top of Java-index,Java Essentials,New To Java...
# 7
Thank you very much.I've looked over the links and done some tinkering. I haven't had much success, but I'll keep trying. If you can find a ComboBoxModel example, I'll be very grateful.
aslan23a at 2007-7-15 16:42:52 > top of Java-index,Java Essentials,New To Java...
# 8

Eureka! ( I have found it ).

I went back to my old version, where I am not subclassing the JComboBox.

I declare my combo box like so:

private JComboBox empMenu = new JComboBox(new DefaultComboBoxModel());

Then, in my initEmps() method I simply replace the DefaultComboBoxModel each time before populating the JComboBox:

empMenu.setModel(new DefaultComboBoxModel());

... and pray that java garbage collection really works!

Thanks again!

aslan23a at 2007-7-15 16:42:52 > top of Java-index,Java Essentials,New To Java...