如何在jTable的顶部显示最后添加的行 [英] how to display the last added row in the top of the jTable
问题描述
你好,我想知道是否有一种方法可以在运行时在jTable的顶部显示最后添加的行.我更新了选择语句"SELECT * FROM table ORDER BY ID DESC".会填满我的表格,但最近添加的行会显示在表格底部,直到我关闭程序并再次打开它,然后它才会显示在顶部
Hello I have a question I want to know if is there a way to display the last added row at the top of jTable in runtime I updated the select statement "SELECT * FROM table ORDER BY ID DESC" which fills my table but the recent added row show up at the bottom of the table till I close the program and I open it again then it shows up at the top
TableItemsModel:
TableItemsModel:
public class TableItemsModel extends AbstractTableModel{
ItemsDao itemDao = new ItemsDao();
private List<Items> items;
public TableItemsModel() throws Exception {
this.items = (ArrayList<Items>)itemDao.getItemsList();
}
private DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
@Override
public int getRowCount() {
return items.size();
}
@Override
public int getColumnCount() {
return 3;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
Items item = items.get(rowIndex);
switch(columnIndex){
case 0: return p.getProductName();
case 1: return p.getProductCategory();
case 2: return p.getProductPrice();
default: return "";
}
}
public String getColumnName(int column){
switch(column){
case 0: return "PRODUCT NAME";
case 1: return "PRODUCT CATEGORY";
case 2: return "PRICE";
default: return "";
}
}
public void addRow(Items item){
items.add(item);
fireTableRowsInserted(items.size()-1, items.size()-1);
}
public void deleteRow(Items item){
items.remove(item);
fireTableRowsInserted(items.size()-1, items.size()-1);
}
}
jFrame:
private final TableItemsModel model;
public Products() throws Exception {
this.model = new TableItemsModel();
private void btnAddItemActionPerformed(java.awt.event.ActionEvent evt) {
String productName = txtProductName.getText();
String productCategory = txtProductCategory.getText();
int productPrice = Integer.valueOf(txtPrice.getText());
try {
int count = ItemsDao.getInstance().insert(itemDao);
if (count == 1) {
model.addRow(Items);
JOptionPane.showMessageDialog(null,"item successfully added");
} else {
JOptionPane.showMessageDialog(null, "Cannot Add Item");
}
} catch (Exception ex) {
Logger.getLogger(AddNewPatient.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
推荐答案
首先,为什么要调用自定义对象Items
.那是复数.对象应使用名称的单数形式.因此,更好的名称是Item
甚至是Product
,因为您使用了"product"在所有方法名称中保持一致.
First of all why are you calling your custom object Items
. That is plural. Objects should be given the singular version of the name. So a better name would be Item
or maybe even Product
, since you use "product" in all you method names. Be consistent.
能否告诉我如何在我的代码中包含insertRow方法
can you tell me how to include the method insertRow into my code please
您正在使用List
来保存数据.阅读List
API,您将看到有两个add(...)
方法.一个方法在List
的末尾添加元素,另一种方法在List
的指定索引处插入元素.
You are using a List
to hold the data. Read the List
API and you will see that there are two add(...)
methods. One method adds the element at the end of the List
and the other method inserts the element at the specified index of the List
.
因此要在List
的开头添加一个项目,您将指定0作为索引.
So to add an Item at the beginning of the List
you would specify 0 as the index.
为此,您需要复制addRow(...)方法的逻辑,但需要两个参数:
To do this you copy the logic of your addRow(...) method except you want two parameters:
public void insertRow(int index, Item item)
然后将方法中的逻辑更改为:
Then you change the logic in the method to:
- 将项目插入列表的指定索引.
- 在指定的索引处调用fireTableRowsInserted(..)方法.
通过传入索引,可以使方法非常灵活.您可以在开头或中间插入项目(如果需要).
By passing in the index you make the method very flexible. You can insert an Item at the start or in the middle (if required).
这篇关于如何在jTable的顶部显示最后添加的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!