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");
private JList list;
private DefaultListModel m_teams;
private Statement st;
private JComboBox teams;
//private AddPlayer newPlayer;
public PlayerFunctions()
{
super("Add/View/Remove Players");
m_teams = new DefaultListModel();
try
{
// Load the driver class
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//defines the data source for the driver
String sourceURL = "jdbc

dbc:Intramural";
// create connection through the driver manager
Connection conn = DriverManager.getConnection(sourceURL);
st = conn.createStatement();
ResultSet rs = st.executeQuery("Select * from Team");
teams = new JComboBox();
while (rs.next())
{
teams.addItem(rs.getString("TeamName"));
}
String teamName = (String)teams.getSelectedItem();
updateViewPlayers(teamName);
}
catch(ClassNotFoundException cnfe)
{
System.err.println(cnfe);
}
catch(SQLException sqle)
{
System.err.println(sqle);
}
//Main layout will have three panels
JPanel main = new JPanel();
main.setLayout(new BorderLayout());
getContentPane().add(main);
main.add(createPanelOne(),BorderLayout.NORTH);
main.add(createPanelTwo(), BorderLayout.CENTER);
main.add(createPanelThree(), BorderLayout.SOUTH);
add.addActionListener(this);
remove.addActionListener(this);
quit.addActionListener(this);
teams.addActionListener(this);
setSize(240,300);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private JPanel createPanelOne()
{
JPanel one = new JPanel();
one.setLayout(new BorderLayout());
one.setBorder(new TitledBorder(new EtchedBorder(), "Select a Team"));
one.add(teams);
return one;
}
private JPanel createPanelTwo()
{
JPanel two = new JPanel();
two.setLayout(new BorderLayout());
two.setBorder(new TitledBorder(new EtchedBorder(), "View Players"));
JScrollPane ps = new JScrollPane(list);
two.add(ps, BorderLayout.CENTER);
return two;
}
private JPanel createPanelThree()
{
JPanel three = new JPanel();
three.add(add);
three.add(remove);
three.add(quit);
return three;
}
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);
}
}
public static void main(String[] args)
{
PlayerFunctions app = new PlayerFunctions();
}
/* (non-Javadoc)
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
public void actionPerformed(ActionEvent ae)
{
if (ae.getSource() == add)
{
String teamName = (String)teams.getSelectedItem();
AddPlayer _add = new AddPlayer(teamName);
}
else if (ae.getSource() == remove)
{
try
{
String fname;
String lname;
String _temp = (String) list.getSelectedValue();
StringTokenizer temp = new StringTokenizer(_temp," ");
fname = temp.nextToken();
lname = temp.nextToken();
System.out.println(_temp);
st.execute("delete from Player where PlayerFirstName ='" + fname + "' and PlayerLastName ='" + lname + "'" );
m_teams.removeElement(list.getSelectedValue());
}
catch(SQLException sqle)
{
System.err.println(sqle);
}
}
else if (ae.getSource() == teams)
{
m_teams.clear();
String teamName = (String)teams.getSelectedItem();
updateViewPlayers(teamName);
}
}
}