Say we have the following array of instantiated objects:
SomeClass[] items = {new SomeClass("Apples", 1.99f, 0.311f),
new SomeClass("Oranges", 0.99f, 0.396f),
new SomeClass("Bananas",2.99f,2.27f)};
Assume the constructor is String name, float price, float weight.
So now I create a JList and put it in a JScrollPane:
itemsList = new JList(items);
ScrollPane itemsListScrollPane = new JScrollPane(itemsList);
Is there a way to override what’s shows up in the scroll pane, so that only the name attribute shows up (and discard the other attributes) in it like so, without overriding the toString() method in the SomeClass class.
Apples
Oranges
Bananas
I hope it’s clear, if you have any questions I’ll try to clarify.
Thanks.
Edit:
I just want to clarify, is there a way to use for example a method written for SomeClass called getName() that returns the string value of the name attribute and let the JList display only that?
You can set a
ListCellRendereron the JList, you can choose whatever you want to render anything. See also the official doc about JList