JList Problem

Using a swing interface. The problem I am having is I have a combo box that when selected goes to an access database and populates a JList with the data returned from a result set. Each time a new team is selected a new reslult set populates the window. Presently I can add to the JList in the window. However when I select an item in the window to remove the JList sees the selected value as null. It seems the JList is displaying properly after choosing a different option from the combo however when you try to access the JList elements it returns null. I should also mention I can remove a selected item from the JList the first time the swing interface pops up. But once I select a different option in the combo box I can no longer remove items. JList appears null even though the display does show the items that should be in the list. Here is the code... public class PlayerFunctions extends JFrame implements ActionListener { private JButton add = new JButton("Add"); private JButton remove = new JButton("Remove"); private JButton quit = new JButton("View/Modify");

Only include the relevant code and things will be easier to debug ( you can replace irrelevant chunks of code with '...' ). Anyhow, created a model for the list and use addRow().

Carleto Way
Greenhorn

Joined: Feb 19, 2004
Posts: 2

posted Feb 19, 2004 20:23:00

0

Originally posted by Peyton McCullough: Only include the relevant code and things will be easier to debug ( you can replace irrelevant chunks of code with '...' ). Anyhow, created a model for the list and use addRow().

I have actually done that in my Constructor... public PlayerFunctions() { super("Add/View/Remove Players"); m_teams = new DefaultListModel(); ... In the constructor I then call a method that updates the window String teamName = (String)teams.getSelectedItem(); updateViewPlayers(teamName); .... The method looks like this public void updateViewPlayers(String _teamName) {ResultSet rs;

try { rs = st.executeQuery("Select * from QryTeamPlayers where TeamName ='" + _teamName + "'"); while (rs.next()) { m_teams.addElement(rs.getString("FullName")); } list = new JList(m_teams); list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); } catch(SQLException sqle) { System.err.println(sqle); } } .... This method should see the DefaultListModel() because I have made it private. The JList is populating DefaultListModel when I change values in the comboBox. I just cannot remove the items when I select them. when I execute the following code a null value is always returned. String _temp = (String) list.getSelectedValue(); Except the first time I try to remove an Item before I change the value in the combo box.