Java Swing,Corba对象 - 如何在DefaultListModel中存储Corba对象? [英] Java Swing, Corba Objects - How to store Corba objects in DefaultListModel?
问题描述
我有这样的IDL接口:
interface User
{
string toString
// ..
};
interface Group
{
typedef sequence< User>用户;
用户getUsers();
};
当我将它翻译成C ++时,我得到了这样的结果:
// ...
Group :: Users * GroupImpl :: getUsers()
{
// of'User'-objects
}
在客户端显示我的用户。我这样做:
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是 JList
。
当我这样写时,我只看到我的Users对象的IOR:
IOR:0123405948239481293812312903891208320131293812381023
IOR:0092930912617819919191818173666288810010199181919919
等等...
如何做到这一点,看到他们的toString在DefaultListModel中的表示?我不想这样做:
model.addElement(users [i] .toString());
这不是重点。当我使用RMI而不是CORBA时, model.addElement(users [i]);
正是我需要的,因为我看到用户字符串表示。
但是我需要使用CORBA并存储在DefaultListModel corba-user- 对象中,而不是字符串。请帮助。
一种方法是创建一个UserView类,模型:
public class UserView {
private final User corbaUser;
public UserView(User corbaUser){
this.corbaUser = corbaUser
}
@Override
public String toString(){
String ret = null;
//根据需要构造字符串
return ret;
}
}
编辑:
如JB Nizet指出的那样,要小心你放在toString()中的代码,因为每次需要显示列表时都会调用它 - 或者最新数据的显示可能正是你想要的。
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.
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.
这篇关于Java Swing,Corba对象 - 如何在DefaultListModel中存储Corba对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!