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

不羁岁月 提交于 2019-12-11 05:40:36

问题


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.


回答1:


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.




回答2:


I guess that the toString() method of the stub doesn't actually call the toString() method of the remote CORBA object. Try using another method name (like getName()), and use a custom renderer which calls this method.

That said, is it really a good idea to model a User as a remote CORBA object? That will cause a lot or remote method calls just to display the names of the users, and thse method calls are basically out of your control, since the Swing components will make them. Shouldn't you use DTOs instead?



来源:https://stackoverflow.com/questions/12769997/java-swing-corba-objects-how-to-store-corba-objects-in-defaultlistmodel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!