Results 1 to 3 of 3

Thread: Please help me fix the problem.

  1. #1

    Thread Starter
    Banned
    Join Date
    Nov 2001
    Location
    USA
    Posts
    46

    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_("jdbcdbc: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

  2. #2
    Lively Member
    Join Date
    Dec 1999
    Posts
    106
    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
    }
    }
    }
    ...
    ...
    ...
    }

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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
  •  



Click Here to Expand Forum to Full Width