java - Is there a way to dynamically change which attribute the toString method displays for custom classes? -


i'm creating jlist in java gui program filled custom "steamfriends" class via arraylist called friendslist:

friendslistjlist.setmodel(new javax.swing.abstractlistmodel() {     steamfriend[] friendlistarr = friendslist.toarray(new steamfriend [0]);     public int getsize() { return friendlistarr.length; }     public steamfriend getelementat(int i) { return friendlistarr[i]; } }); 

right tostring() function returns friendname of steamfriend object, i'm wondering if it's possible change can control attribute returns , best practice be.

if want fill jlist list of steamfriends sorted age (for example), how change tostring() method display age? through static variable (public static int variabletodisplay) in steamfriend object, perhaps?

it's better not use tostring() display data user, rather best used debugging tool. jlists, recommend instead use custom cell renderer, 1 have written display information wish.

e.g. 1 i've used in program,

class labeltextpairlistrenderer extends defaultlistcellrenderer {    @override    public component getlistcellrenderercomponent(jlist<?> list, object value,          int index, boolean isselected, boolean cellhasfocus) {       labeltextpairlist labeltextpairlist = (labeltextpairlist) value;       if (labeltextpairlist == null) {          value = "";       } else {          value = labeltextpairlist.getname();       }       return super.getlistcellrenderercomponent(list, value, index, isselected,             cellhasfocus);    } } 

Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -