I have a quick JList question...

This is mostly for curiosity and simplicity.I have been told that I can populate a Jlist of a custom class with a specific variable from that class without using a model or creating another array for that specific encapsulated variable.

For example:

I have a class called PhoneBook. That class has multiple private variables like name, phone, city, address, etc. The way I would usually populate a JList would be by either creating and adding to a model or making a new array and using that classes accessors to get, for example, the names and then using that array in the constructor.

My question is: Is there a way to just populate a JList without doing a model or creating an array but directly with a constructor call.

So I would just have for example something like this(I know this code doesn't work... just what im thinking)

PhoneBook array =/*data from buffer */

list =new JList(array.getName())

...and it would get those names for me for the JList. Ive searched and looked around and now I have a headache and was hoping someone has some experience with this.

Thank you.

[1202 byte] By [Haldir323a] at [2007-11-26 22:11:53]
# 1

do you mean something like this?

import javax.swing.*;

import javax.swing.event.*;

import java.awt.*;

import java.awt.event.*;

class Testing

{

public void buildGUI()

{

Contact[] contacts = {new Contact("larry","123"),new Contact("mo","123"),new Contact("curly","123")};

JList list = new JList(contacts);

JFrame f = new JFrame();

f.getContentPane().add(new JScrollPane(list));

f.pack();

f.setLocationRelativeTo(null);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setVisible(true);

}

public static void main(String[] args)

{

SwingUtilities.invokeLater(new Runnable(){

public void run(){

new Testing().buildGUI();

}

});

}

}

class Contact

{

String name;

String phoneNumber;

public Contact(String n, String pn)

{

name = n; phoneNumber = pn;

}

public String toString(){return name;}

}

Michael_Dunna at 2007-7-10 11:00:46 > top of Java-index,Java Essentials,New To Java...
# 2
Oh yes thats pretty much what I meant! I thought I had to manipulate something in the constructor call to get that. Well I was led to believe that but this works just as well. I always thought it was a waste to create a new model or array just to populate a list. Thanks alot!
Haldir323a at 2007-7-10 11:00:46 > top of Java-index,Java Essentials,New To Java...