简体   繁体   中英

Getting a Vector to JList (Java)

I am having trouble with accessing a vector from a different class and displaying into a Jlist.

So for example, (this is one class)

 // I have a vector that contains an object in one class

 private Vector<Person> personList;

 public MakePerson(Vector personList){
 this.personList = personList;

 // I have some GUI formatting here and button action listeners which 
 add a person when a button is pressed.
 }

Action Listner

 public void action(Actionevent event){
 Person people = new Person();

 // The information from the GUI is grabbed and placed into a variable.

 people.setName(name);
 people.setAge(age);
 people.setheight(tall);
 personList.add(people);
 }

 // To my knowledge this is adding the object to the vector, please let me
 know if otherwise any help is greatly appreciated 

Now here is where I am having trouble (this is a different class in the same package)

 public class ChoosePerson extends Jpanel {

 private Vector<Person> personList;
 private Jlist myList;

 public ChoosePerson(Vector personList){

 this.person = personList;
 myList = new JList(personList);

 //This is part of my panel where the Vector should be displayed but doesn't

 Jpanel list = new Jpanel();
 JScrollPane myPane = new JScrollPane(myList);
 list.add(myPane);

//This is where I am having trouble
 }

The purpose of the code is to add a person from the first class GUI and display it on the second class GUI using the vectors. (Note: The MakePerson and ChoosePerson GUI are seperated by tabs)

The general approach is that you have to create a ListModel which wraps the Vector and then you configure the JList to use this model. Then you create a Renderer to display the objects ( Person ) in your list as needed.

I suggest you go through the full example from the official doc .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM