user3847788
user3847788

Reputation:

updating jTable row if record is already present

I am working on invoice and have added jTable to my frame for multiple items, like I have 4 fields for user "item, quantity, rate, price" and user fills it all and click on add button then these fields are added to jTable with item id inlcuded.

now the problem I am facing when user enters the same item whereas that item is already added in the list and it duplicates the row in jTable.

All i want to update that row if the item is same, update its quantity field with adding new quantity value and price value.

i.e.

 id  |Item  |qty|   rate|   Price|
 --------------------------------------
 12  |saee  |3  |    300|    900 |

now if user enters the same item with 5 quantity, it should update same item or row with plus quantity and price like.

--------------------------------------
id  |Item   |qty|   rate|   Price|
--------------------------------------
12  |saee   |8  |    300|   24000|

but instead of this, it adds a second row in jTable.

here is my code for adding items in to jTable

 DefaultTableModel model = (DefaultTableModel) tbl_sale.getModel();
 model.addRow(new Object[]{lbl_id.getText(), txt_item.getText(), txt_qty.getText(), txt_rate.getText(), txt_rs.getText()});

can anyone tell me what should i do?

Upvotes: 0

Views: 1200

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347244

Sometimes I think we forget that we're operating in an OO environment and the power that something like that can bring

What I mean is, you data can easily be encapsulated into an object, for example...

public class Order {

    private int id;
    private String item;
    private int quanity;
    private double unitPrice;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getItem() {
        return item;
    }

    public void setItem(String item) {
        this.item = item;
    }

    public int getQuanity() {
        return quanity;
    }

    public void setQuanity(int quanity) {
        this.quanity = quanity;
    }

    public double getUnitPrice() {
        return unitPrice;
    }

    public void setUnitPrice(double unitPrice) {
        this.unitPrice = unitPrice;
    }

    public double getTotalPrice() {
        return getUnitPrice() * getQuanity();
    }
}

This also has the capacity to calculate the total price dynamically, rather than needing us to do it.

DefaultTableModel is good for representing disjointed pieces of information, but not so good at representing an object per row, to this end, I use an AbstractTableModel and customise it for the needs of the object, something like...

public class OrderTabelModel extends AbstractTableModel {

    protected static final String COLUMN_NAMES[] = {"ID", "Item", "Qty", "Rate", "Price"};
    protected static final Class COLUMN_TYPES[] = {int.class, String.class, int.class, double.class, double.class};

    private List<Order> orders;

    public OrderTabelModel() {
        orders = new ArrayList<>(25);
    }

    public void add(Order order) {
        orders.add(order);
        fireTableRowsInserted(orders.size() - 1, orders.size() - 1);
    }

    public void remove(Order order) {
        int row = orders.indexOf(order);
        if (row >= 0) {
            orders.remove(order);
            fireTableRowsDeleted(row, row);
        }
    }

    public void update(Order order) {
        int row = orders.indexOf(order);
        if (row >= 0) {
            fireTableRowsUpdated(row, row);
        }
    }

    public Order getOrderAt(int row) {
        return orders.get(row);
    }

    @Override
    public int getRowCount() {
        return orders.size();
    }

    @Override
    public int getColumnCount() {
        return COLUMN_NAMES.length;
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        return COLUMN_TYPES[columnIndex];
    }

    @Override
    public String getColumnName(int column) {
        return COLUMN_NAMES[column];
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Object value = null;
        Order order = getOrderAt(rowIndex);
        switch (columnIndex) {
            case 0:
                value = order.getId();
                break;
            case 1:
                value = order.getItem();
                break;
            case 2:
                value = order.getQuanity();
                break;
            case 3:
                value = order.getUnitPrice();
                break;
            case 4:
                value = order.getTotalPrice();
                break;
        }
        return value;
    }

}

Now, when you want to add/remove/update a given object, you can just call add/remove/update methods of the OrderTabelModel.

Be Sure though, when you want to update an Order, you're interacting with an instance from the model, otherwise things won't go as you expect...

OrderTableModel model = ...;
//...
Order order = model.getOrderAt(0);
order.setQuanity(10);
model.update(order);

Upvotes: 2

Related Questions