PDA

Click to See Complete Forum and Search --> : Please help me fix the problem.


Jacob
Nov 12th, 2002, 09:54 AM
Hi.
I use the constructor to get the information from the database, but it doesn't complie. Here is my code for the constructor.

=============================================
public getData() {

Connection c = DriverManager.getConnection_("jdbc:odbc:GradeData");
ResultSet r = c.createStatement().executeQuery("SELECT_ ProductID, ProductName FROM Inventory");
ResultSetMetaData rsmd = r.getMetaData();
int i = 0;
while (r.next()) {
strItemNo_Descript[i] = r.getInt("ProductID") +"\t"+ _r.getString("ProductName");
++i;
}

c.close();

//Set all the elements of JFrame
JPanel content = new JPanel();
labelInstruct = new JLabel("Please select the item no");
content.add(labelInstruct);

itemList = new JList(strItemNo_Descript);
itemList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
itemList.setVisibleRowCount(8);
// ======== Here is the problem.===========
itemList.addListSelectionListener(this);

itemPane = new JScrollPane(itemList);
content.add(itemPane);

addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
} );

}
==============================================
The code above has the following error:
addListSelectionListener(javax.swing.event.ListSelectionListener)_ in javax.swing.JList cannot be applied to (getData)_itemList.addListSelectionListener(this);

Would you tell me how to fix this?

Thank You Very Much.

Jacob

hayessj
Nov 29th, 2002, 08:26 AM
You are adding "this" as the ListSelectionListener and if "this" is not a ListSelectionListener it won't work

Use an inner class to act as a listener
i.e.

// imports here

public class Test extends whatever
{
...
...
private JList m_list = null;
...
...

public Test()
{
...
...

m_list = new JList(new DefaultListModel());
m_list.addListSelectionListener(new ListListener());

// add list to this
...
...
...
}


/**
* Class to act as a list click listener to enable/disable driver jar file buttons
*/
private class ListListener implements ListSelectionListener
{
public void valueChanged(ListSelectionEvent e)
{
Object object = e.getSource();
if (object == m_list)
{
// do whatever here when list is clicked
}
}
}
...
...
...
}

CornedBee
Dec 2nd, 2002, 06:32 AM
You could also implement ListSelectionListener in the main class or use an anonymous class:

refresh.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
refresh_handler(e);
}
});

refresh is a JButton.