简体   繁体   English

如何在 jTable 顶部显示最后添加的行

[英]how to display the last added row in the top of the jTable

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您好我有一个问题我想知道是否有办法在运行时显示 jTable 顶部的最后添加行我更新了 select 语句“SELECT * FROM table ORDER BY ID DESC”,它填充了我的表行显示在表格底部,直到我关闭程序并再次打开它然后它显示在顶部

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: 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);
        }
   } 
}

First of all why are you calling your custom object Items .首先,您为什么要调用自定义 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.因此,更好的名称应该是Item甚至Product ,因为您在所有方法名称中都使用了“product”。 Be consistent.始终如一。

can you tell me how to include the method insertRow into my code please你能告诉我如何在我的代码中包含方法 insertRow

You are using a List to hold the data.您正在使用List来保存数据。 Read the List API and you will see that there are two add(...) methods.阅读List API,您会看到有两个add(...)方法。 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的末尾,另一种方法将元素插入到List的指定索引处。

So to add an Item at the beginning of the List you would specify 0 as the index.因此,要在List的开头添加一个项目,您需要指定 0 作为索引。

To do this you copy the logic of your addRow(...) method except you want two parameters:为此,您需要复制 addRow(...) 方法的逻辑,但需要两个参数:

public void insertRow(int index, Item item)

Then you change the logic in the method to:然后将方法中的逻辑更改为:

  1. insert the Item at the specified index of the List.在 List 的指定索引处插入 Item。
  2. invoke the fireTableRowsInserted(..) method at the specified index.在指定索引处调用 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).您可以在开头或中间插入一个项目(如果需要)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM