-
Problem with constructor
The compiler errors I'm getting:
cannot find symbol constructor ContentPanel(java.util.ArrayList<Order>)
cannot find symbol constructor ContentPanel(java.util.ArrayList<Customer>)
cannot find symbol constructor ContentPanel(java.util.ArrayList<Item>)
However, in the constructor header for ContentPanel, I have a parameter for an array list of objects implementing the ListableItem interface. The classes Order, Customer, and Item all implement the ListableItem interface.
Here's my code. I've cut out the unnecessary bits. The lines where the errors happened are in bold.
Code:
public class FrontEnd extends JFrame implements ActionListener
{
private ArrayList<Customer> customerArray;
private ArrayList<Item> itemArray;
private ArrayList<Order> orderArray;
public FrontEnd(){
}
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand().equals("Order List") ||
e.getActionCommand().equals("Customer List") ||
e.getActionCommand().equals("Item List"))
{
addContentPanel(e.getActionCommand());
}
}
public void addContentPanel(String arg)
{
mainPanel.remove(contentPanel);
if (arg.equals("Order List")){
[B]contentPanel = new ContentPanel(orderArray);[/B]
mainPanel.add(contentPanel);
}
else if (arg.equals("Customer List")){
[B]contentPanel = new ContentPanel(customerArray);[/B]
mainPanel.add(contentPanel);
}
else if (arg.equals("Item List")){
[B]contentPanel = new ContentPanel(itemArray);[/B]
mainPanel.add(contentPanel);
}
}
}
Code:
public class ContentPanel extends JPanel
{
public ContentPanel(ArrayList<ListableItem> listItems){
}
}
Code:
public interface ListableItem
{
public String getTitle();
}
-
You don't actually say what Customer, Item and Order are (and what they implement).
But try and compile this:
Code:
import java.util.ArrayList;
import java.util.List;
interface Foo {}
class Bar implements Foo {}
public class Main {
public static void main(String[] args) {
func(new ArrayList<Bar>());
}
static void func(List<Foo> arg) {}
}
A list of Bar is not a (type of) list of Foo. Read about the butterflies, lions and their animal cages in the Subtyping section of Oracle's Generics Tutorial.
[Edit] And read the next section on wildcards.