|
-
Nov 12th, 2002, 10:54 AM
#1
Thread Starter
Banned
Please help me fix the problem.
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 dbc: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
-
Nov 29th, 2002, 09:26 AM
#2
Lively Member
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
}
}
}
...
...
...
}
-
Dec 2nd, 2002, 07:32 AM
#3
You could also implement ListSelectionListener in the main class or use an anonymous class:
Code:
refresh.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
refresh_handler(e);
}
});
refresh is a JButton.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|