views:

61

answers:

2

OK so I switched from JList to List because

1.) It doesn't overlap my drawn images 2.) It can have focus disabled yet track what's selected

Anyway, here's the error I get when I try to compile:

C:\Users\Dan\Documents\DanJavaGen\inventory.java:30: cannot find symbol
symbol  : constructor List(java.lang.Object[])
location: class java.awt.List
        list = new List(arr.toArray());
               ^
C:\Users\Dan\Documents\DanJavaGen\inventory.java:50: cannot find symbol
symbol  : method getSelectedValue()
location: class java.awt.List
        Object index = list.getSelectedValue();
                           ^

The 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.*;
import java.awt.List;

public class inventory extends JApplet implements MouseListener {

public static String newline;
public static 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 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) {
        Object index = list.getSelectedValue();
       System.out.println("You have selected: " + index);
    }

    public void mouseEntered(MouseEvent e) { }

    public void mouseExited(MouseEvent e) { }

    public void mouseClicked(MouseEvent e) { }




    public void paint(Graphics g) {

    }
}
A: 
  • You're importing both java.util.List and java.awt.List. Now the compiler is confused about which one you want. There's a possibility of confusion.

  • java.awt.List, unlike javax.swing.JList, doesn't have a constructor that takes an array as an argument.

  • Also, you still have an empty paint() method. That's very wrong.

Carl Smotricz
How am I importing both awt and util List? :S
Dan
The more specific import (java.awt.List) wins. http://mindprod.com/jgloss/import.html
Alain O'Dea
@Dan: You have an `import java.util.*` which imports everything in the package `java.util`, including `java.util.List`. But in this case, as Alain points out, the `java.awt.List` will take precedence.
Jesper
+1  A: 

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