This site requires JavaScript, please enable it in your browser!
Greenfoot back
Back to discussions
JayWood860
JayWood860 wrote ...

2012/5/17

Lists

JayWood860 JayWood860

2012/5/17

I'm just learning how to work with lists and i've create one perfectly fine. Now i'm just wondering how you can add this list to the world. i know you can't just set the actor's image to the list, so how can i add this to the world. also, is there a difference between "java.awt.List" and "java.util.List"? Thanks for all the help!
JayWood860 JayWood860

2012/5/18

anyone?
davmac davmac

2012/5/18

If you want to display a list of items in your scenario, both java.awt.List and java.util.List are wrong. (What do you mean by "I've created one" - why would you create something if you don't understand what it does?) There are various scenarios which do have examples of how to show lists of items - just do a search for "gui"
JayWood860 JayWood860

2012/5/18

i do understand what a list is. this is what i have so far:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.List;
/**
 * Write a description of class User here.
 * 
 * JUWAN 
 * 
 */
public class User extends Actor
{
    public User()
    {
        createLists();
        addObject(sex, getWorld().getWidth()/2, getWorld().getHeight()/2);
    }
    public List sex = new List(2); 
    public List state = new List(10); 
    /**
     * Act - do whatever the User wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
    }    
    public void createLists()
    {
        sex.add("Male", 0);  sex.add("Female", 1);
        state.add("AL"); state.add("AK"); state.add("AZ"); state.add("AR");
        state.add("CA"); state.add("CO"); state.add("CT"); state.add("DE");
        state.add("FL"); state.add("GA"); state.add("HI");
        state.add("ID"); state.add("IL"); state.add("IN"); state.add("IA");
        state.add("KS"); state.add("KY"); state.add("LA"); 
        state.add("ME"); state.add("MD"); state.add("MA"); state.add("MI"); state.add("MN"); state.add("MS"); state.add("MO"); state.add("MT");
        state.add("NE"); state.add("NV"); state.add("NH"); state.add("NJ"); state.add("NM"); state.add("NY"); state.add("NC"); state.add("ND");
        state.add("OH"); state.add("OK"); state.add("OR"); state.add("PA"); state.add("RI");
        state.add("SC"); state.add("SD"); state.add("TN"); state.add("TX"); state.add("UT");
        state.add("VT"); state.add("VA"); state.add("WA"); state.add("WV"); state.add("WI"); state.add("WY");
    }
}
as you can see i did create a list, whether or not it's in the right format or not i don't know. but how would i display it in the world. i would like the lists to look something like this: (just with my own strings instead of course). but i will now search for "gui"
JayWood860 JayWood860

2012/5/18

*getWorld().addObject(....);
davmac davmac

2012/5/18

As I said, you can't use java.awt.List to display something in the world. Do a serach for "gui" to find scenarios with list components.
danpost danpost

2012/5/19

A list is an object, yes; but, it is an abstract object and cannot be displayed as such. Really all you have are a bunch of String object (which also are abstract). What needs done is to create objects to draw the strings on and display them, which you can accomplish by way of a list component gui or by coding it yourself (I prefer to code things myself, as then I am not restricted in any way as to how to do things and what can be done. The drawback to this, though, is the time factor. On the plus side, however, is the experience of the journey to accomplish this task; the knowledge acquired through the journey; and the satisfaction, when completed).
You need to login to post a reply