views:

26

answers:

2

OK so I'm doing an ArrayList into a (awt) List and I'm getting the following errors when compiling.

C:\Users\Dan\Documents\DanJavaGen\ArrayList_INV.java:29: cannot find symbol
symbol  : constructor List(java.lang.Object[])
location: class java.awt.List
        list = new java.awt.List(arr.toArray());
               ^
C:\Users\Dan\Documents\DanJavaGen\ArrayList_INV.java:50: cannot find symbol
symbol  : method getSelectedValue()
location: class java.awt.List
       System.out.println("You have selected: " + list.getSelectedValue());
                                                      ^
2 errors

code:

import java.applet.Applet;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.io.*;
import java.util.*;

public class ArrayList_INV extends JApplet implements MouseListener {

public static String newline;
public static java.awt.List list;
int gold = 123;

    public void init() {



ArrayList<String> arr = new ArrayList<String>();
arr.add("Hatchet");
arr.add("Sword");
arr.add("Shield");
arr.add(gold + " Gold");
System.out.println("You have " + arr.size() + " items in your inventory.");
showInventory(arr);



        list = new java.awt.List(arr.toArray());

        add(list);

        list.addMouseListener(this);

        list.setVisible(true);

    }

public static void showInventory (ArrayList<String> theList) {
for (int i = 0; i < theList.size(); i++) {
System.out.println(theList.get(i));
}
}


    public void mousePressed(MouseEvent e) { }

    public void mouseReleased(MouseEvent e) {

       System.out.println("You have selected: " + list.getSelectedValue());
    }

    public void mouseEntered(MouseEvent e) { }

    public void mouseExited(MouseEvent e) { }

    public void mouseClicked(MouseEvent e) { }

/**
    public void paint(Graphics g) {

    }**/
}

What is wrong? Thanks.

+2  A: 

Same as http://stackoverflow.com/questions/3269599/java-cannot-find-symbol-in-list/3269798#3269798

There are two issues at play here:

java.awt.List does not have a constructor that takes Object[]:

list = new List();
for (String item : arr) list.add(item);

java.awt.List has getSelectedItem() not getSelectedValue():

You could your ArrayList with List as follows:

public void mouseReleased(MouseEvent e) {
    Object index = list.getSelectedItem();
    System.out.println("You have selected: " + index);
}
Alain O'Dea
Oh, I see my error now. By the way, do AWT Lists take kind to detect double clicks? I looked and it needs a ListModule which does not take well with Lists. :\
Dan
if(e.getClickCount() == 2) :-D
Dan
+2  A: 

According to the API docs for java.awt.List, there is no public constructor that accepts an array (line 29 in your code). You can use the no argument constructor and add your items one by one using the add() method.

Additionally, you have a typo in a method name on line 50: It's getSelectedItem(), not getSelectedValue().

Asaph