• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

JList Problem

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
}
}
}
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
It's fun to be me, and still legal in 9 states! Wanna see my tiny ad?
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic