0

So I have a GUI class and a class which will hold everything else. I have an ArrayList in the other class and I would like to populate the content of a Jlist in the GUI to the arraylist

I have this so far:

filmList = new JList(films.toArray());
getContentPane().add(filmList);
filmList.setBounds(27, 21, 638, 165);   

It doesn't like just films.toArray() because it's in another class. What am I missing?

This is my FilmSystem class at the moment.

 import java.util.ArrayList;

public class FilmSystem {

public FilmSystem() {

    ArrayList<String> films = new ArrayList<String>();
    films.add("A");
4
  • See this edit: stackoverflow.com/posts/15056563/revisions That is unnecessary noise. Commented Feb 24, 2013 at 20:57
  • About the question: You need to show more code for us to help. Commented Feb 24, 2013 at 20:58
  • what is name of class which has ArrayList? Commented Feb 24, 2013 at 21:09
  • Alright then, I'm not sure what you need to see exactly though. My ArrayList is in a class called FilmSystem. Commented Feb 24, 2013 at 21:13

2 Answers 2

1

From the question its not clear, but if "films" is name of ArrayList in different class then you need to create the object of that class and then call the getter on it to get films and then use toArray on it. Like below:

FilmSystem filmObj=new FilmSystem();
filmList = new JList(filmObj.getterMethod.toArray(new String[0]));

where getterMethod should be name of the getter method you defined in class. If you have not defined getter method then you can directly call Arraylist on object.

If getter is not defined, then use following :

 FilmSystem filmObj=new FilmSystem();
 filmList = new JList(filmObj.films.toArray(new String[0]));
Sign up to request clarification or add additional context in comments.

5 Comments

Sorry, rereading my question I realise it's a bit vague. Yeah my arraylist is called films in a class FilmSystem. I have created a FilmSystem object in the FilmGUI class. How would I word the getter method? I'm guessing it wouldn't be as simple as films.get(); ?
Sorry that didn't work for me, 'films' is still red for some reason. I think I'm missing something that we haven't accounted for. I could edit my question and show the whole of both classes or start from scratch and show me how you'd do it, if you didn't mind that.
i just noticed, you are defining films in the constructor, is this intentional ? you should define films as instance variable like public class FilmSystem { ArrayList<String> films = new ArrayList<String>(); public FilmSystem() { films.add("A");
No that wasn't intentional, thanks for pointing that out. Nothing goes red now but I still got errors when I compile and try to run it. My JFrame runs with nothing in it. I have no idea why :/
I got it working, I have no idea what was it but I opened another one of my projects and quickly recreated a JList and the lines needed and it worked, eventually I got it working in the project I needed it to. Thanks for the help the few lines of code you guys gave me helped me out.
1

An array has an explicit type, independent of its elements' types. When constructing a JList<String> you must pass an array whose type is String[], not Object[]. The zero-argument toArray() method always returns an array whose type is Object[], even though the array's elements might all be of a particular class such as String.

To obtain an array of a particular type, pass an array of the desired type to the toArray method:

filmList = new JList<String>(films.toArray(new String[0]));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.