java /对象的表模型/更新对象”
我有一个 Stock 对象集合,我正在实时更新大约 10/15 个变量。我通过集合中的 ID 访问每只股票。我还尝试在 JTable 中显示它并实现了 AbstractTablemodel。效果不太好。
我有一个 RowMap,当股票添加到 TableModel 时,我将每个 ID 添加到其中。为了更新 TableModel 中所有股票的价格和变量,我想将 Stock 对象发送到 updateModel(Stock s) 方法。我可以通过搜索地图找到相关行,但是如何很好地处理这个问题,这样我就不必开始迭代表列并将单元格的值与对象的变量进行比较以查看是否有任何差异??
基本上,我想将 Stock 对象发送到 TableModel 并在发生更改时更新单元格,如果没有更改则不执行任何操作。
关于如何实现可以做到这一点的 TableModel 有什么想法吗?任何指针都将不胜感激。
谢谢。
编辑:
'导入 javax.swing.table.AbstractTableModel; 导入 ppwebsitemonitor.views.OrderTableModel.*;
公共最终类 QuoteTableModel 扩展 AbstractTableModel {
// Attributes
String[] columnName = new String[]{"Stock", "Quantity", "Price", "Side", "Status"};
Class[] columnClass =
new Class[]{String.class, Integer.class, Double.class, String.class, String.class};
Object[][] rows = null;
public QuoteTableModel() {
}
public QuoteTableModel(Object[][] orders) {
this.rows = orders;
}
public int getColumnCount() {
return columnName.length;
}
public boolean isNew(int row) {
return rows[row][4].equals(OrderStatus.NEW);
}
public boolean isFilled(int row) {
return rows[row][4].equals(OrderStatus.FULLY_FILLED);
}
public boolean isCancelled(int row) {
return rows[row][4].equals(OrderStatus.CANCELLED);
}
public void updateOrderPrice(int row, Double newPrice) {
Object[] order = rows[row];
order[2] = newPrice;
super.fireTableRowsUpdated(row, row);
}
public void updateOrderStatus(int row, int status) {
Object[] order = rows[row];
order[4] = OrderStatus.States[status];
super.fireTableRowsUpdated(row, row);
}
public Object getValueAt(int row, int col) {
return rows[row][col];
}
public int getRowCount() {
return rows.length;
}
public String getColumnName(int col) {
return columnName[col];
}
public Class getColumnClass(int col) {
return columnClass[col];
}
}'
I've a collection of Stock objects that I'm updating about 10/15 variables for in real-time. I'm accessing each Stock by its ID in the collection. I'm also trying to display this in a JTable and have implemented an AbstractTablemodel. It's not working too well.
I've a RowMap that I add each ID to as Stocks are added to the TableModel. To update the prices and variables of all the stocks in the TableModel, I want to send a Stock object to an updateModel(Stock s) method. I can find the relevant row by searching the map, but how do I handle this nicely, so I don't have to start iterating through table columns and comparing the values of the cells to the variables of the object to see whether there are any differences??
Basically, i want to send a Stock object to the TableModel and update cells if there are changes and do nothing if there aren't.
Any ideas about how to implement a TableModel that might do this? Any pointeres at all would be appreciated.
Thanks.
Edit:
'import javax.swing.table.AbstractTableModel;
import ppwebsitemonitor.views.OrderTableModel.*;
public final class QuoteTableModel extends AbstractTableModel {
// Attributes
String[] columnName = new String[]{"Stock", "Quantity", "Price", "Side", "Status"};
Class[] columnClass =
new Class[]{String.class, Integer.class, Double.class, String.class, String.class};
Object[][] rows = null;
public QuoteTableModel() {
}
public QuoteTableModel(Object[][] orders) {
this.rows = orders;
}
public int getColumnCount() {
return columnName.length;
}
public boolean isNew(int row) {
return rows[row][4].equals(OrderStatus.NEW);
}
public boolean isFilled(int row) {
return rows[row][4].equals(OrderStatus.FULLY_FILLED);
}
public boolean isCancelled(int row) {
return rows[row][4].equals(OrderStatus.CANCELLED);
}
public void updateOrderPrice(int row, Double newPrice) {
Object[] order = rows[row];
order[2] = newPrice;
super.fireTableRowsUpdated(row, row);
}
public void updateOrderStatus(int row, int status) {
Object[] order = rows[row];
order[4] = OrderStatus.States[status];
super.fireTableRowsUpdated(row, row);
}
public Object getValueAt(int row, int col) {
return rows[row][col];
}
public int getRowCount() {
return rows.length;
}
public String getColumnName(int col) {
return columnName[col];
}
public Class getColumnClass(int col) {
return columnClass[col];
}
}'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以存储 Stock 对象本身,而不是将股票 ID 存储在表模型中。与您在大师收藏中保留的相同。然后,每次更新股票值时,都会向表 UI 发送刷新(或将其置于定时刷新或程序所需的任何位置)。
编辑
我添加了一些代码来说明我的意思。它仅高于伪代码,您必须根据您的情况进行调整。
这是为了模拟您的股票订单对象:
您的自定义模型看起来像这样:
您可以使用它来测试:
此代码不考虑您的任何排序可能需要做。但基本概念已经有了。希望这有帮助。
Instead of storing the stock ID's in your table model you can store the Stock objects themselves. The same ones that you are holding onto in your master collection. Then send a refresh to the table UI every time you do an update to a stock value (or put it on a timed refresh or whatever your program requires).
Edit
I am including some code to illustrate what I meant. It's barely above pseudo-code, you will have to adapt it to your situation.
This is to emulate your stock order object:
Your custom model would look something like this:
You can use this to test:
This code doesn't take into account any sorting you might need to do. But the basic concepts are there. Hopefully this helps.