Search code examples
javac++swingcorbadefaultlistmodel

Java Swing, Corba Objects - How to store Corba objects in DefaultListModel?


I have such IDL interface:

interface User
{
    string toString();
    //..
};

interface Group
{
    typedef sequence<User> Users;
    Users getUsers();

};

When I translated it to C++ I got sth like this:

// ...
Group::Users* GroupImpl::getUsers()
{
    // ..return sequence of 'User'-objects
}

On client side (written in Java) I want to show my users. I do sth like this:

public void showAllUsers() 
{
    User[] users = interface_obj.getUsers();
    if(users.length != 0)
    {
        DefaultListModel model = new DefaultListModel();
        for(int i=0; i<users.length; i++)
            model.addElement(users[i]);
        this.usersList.setModel(model);
    }
}

this.usersList is a JList.

When I do this like I wrote, I see only IORs of my Users-object:

IOR :0123405948239481293812312903891208320131293812381023
IOR: 0092930912617819919191818173666288810010199181919919

and so on ...

How to make it that way, to see their toString(); representation in DefaultListModel? I dont want to do this:

model.addElement(users[i].toString());

thats not the point. When I use RMI instead of CORBA, model.addElement(users[i]); is exactly what I need cause I see users string representation. But I need to use CORBA and store in DefaultListModel corba-user-objects, not strings. Please, help.


Solution

  • One way to do it would be to make a UserView class whose instances you'd put in the list model:

    public class UserView {
    
        private final User corbaUser;
    
        public UserView(User corbaUser) {
            this.corbaUser = corbaUser
        }
    
        @Override
        public String toString() {
           String ret = null;
           // construct the string as you want here
           return ret;
        }
    }
    

    EDIT:

    as pointed out by JB Nizet be careful with the code you put in toString() since it is called every time the list needs to be shown - or the showing of the freshest data might be exactly what you want.