问题描述
首先我应该告诉它,这不是关于渲染表格单元格。
I should tell this first, this is NOT about Rendering a Table cell.
这是我在我的数据库中使用基于用户
对象的2D数组构建的TableModel。
Here is the TableModel that i'm building using a 2D array based on a User
object in my DB.
List<User> userList = userManagerService.getAllUsers();
/* String[] col_user = {"Username", "Name", "Phone", .... } */
String[][] data = new String[userList.size()][col_user.length];
int i = 0;
for (User user : userList) {
String[] userdata = new String[col_user.length];
userdata[0] = user.getUserUsername();
userdata[1] = user.getUserName();
userdata[2] = user.getUserPhone();
userdata[3] = user.getUserNic();
userdata[4] = user.getUserAddress();
userdata[5] = user.getUserEmail();
data[i++] = userdata;
}
VstTableItemModel tiModel = new VstTableItemModel(data, col_user);
dataTable.setModel(tiModel);
我的问题是如何获得用户
使用表中的选定行返回对象。请注意,我无法创建新的用户
对象,并使用行数据填充它。我必须得到查询的用户
对象( userList
中的对象)。那么,他们以任何方式设置带有表格行的对象吗?
My problem is how can i get a User
object back, using the selected row in the Table. Note that i can't make a new User
object and populate it with the row data. I must get the queried User
object(objects in userList
). So, is their any way to set a Object with a table row ?
这是我的 VstTableItemModel
类。
public class VstTableItemModel extends AbstractTableModel {
ArrayList<Object[]> data;
String[] header;
public VstTableItemModel(Object[][] obj, String[] header) {
this.header = header;
data = new ArrayList<Object[]>();
for (int i = 0; i < obj.length; ++i) {
data.add(obj[i]);
}
}
@Override
public int getRowCount() {
return data.size();
}
@Override
public int getColumnCount() {
return header.length;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
return data.get(rowIndex)[columnIndex];
}
@Override
public String getColumnName(int index) {
return header[index];
}
}
推荐答案
在创建模型之前,不要将用户
对象拆分,而是将其直接添加到模型中,并允许模型为您完成工作......
Instead of splitting the User
object up before you create the model, add it directly to the model and allow the model to do the work for you...
例如
public class VstTableItemModel extends AbstractTableModel {
private List<User> users;
public VstTableItemModel(List<User> users) {
this.users = new ArrayList<User>(users);
}
@Override
public int getRowCount() {
return users.size();
}
@Override
public int getColumnCount() {
return 6;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
Object value = "??";
User user = users.get(rowIndex);
switch (columnIndex) {
case 0:
value = user.getUserUsername();
break;
case 1:
value = user.getUserName();
break;
case 2:
value = user.getUserPhone();
break;
case 3:
value = user.getUserNic();
break;
case 4:
value = user.getUserAddress();
break;
case 5:
value = user.getUserEmail();
break;
}
return value;
}
@Override
public Class<?> getColumnClass(int columnIndex) {
return // Return the class that best represents the column...
}
/* Override this if you want the values to be editable...
@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
//....
}
*/
/**
* This will return the user at the specified row...
* @param row
* @return
*/
public User getUserAt(int row) {
return users.get(row);
}
}
这样,你应该能够做类似的事情......
This way, you should be able to do something like...
List<User> userList = userManagerService.getAllUsers();
VstTableItemModel tiModel = new VstTableItemModel(userList);
现在需要...时,您可以获取代表特定行的用户...
Now when you need to...you can grab a the user that is represent at a specific row...
User user = tiModel.getUserAt(rowIndex);
这篇关于如何在JTable行中设置自定义对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!