简体   繁体   中英

how to select an object from a jTable by a click

I have made a class called Clients, that it has like some simple attributes like client_id, client_name and client_age. I have programmed a small GUI with NetBeans that after I input the data from a client by pressing a button I get it displayed in a jTable.

The source code for adding this in the jTable is:

for (int i=0;i<customerV.length;i++){
                jTable2.setValueAt(customerV[i].getName(), i, 0);
}

I would like that if I click one element on the jTable to be able to add some orders to that client by making using of a jButton. If I program that in a console it would be like:

Order order1=new Order("1000","41211")

in which the first field is the order id and the second is the order number, so if I want to assign that to Customer 1 it would be like.

c1.assignOrder(order1)

how I can do that by using java swing? I mean to select the whole object from the element that I click in the jTable

  • Set instances of your Clients to the table model instead of client names (like: jTable2.setValueAt(customerV[i], i, 0); )
  • Implement a custom renderer that will render the client class as needed (eg display the client's name) and set it to the table. Another (easier) option would be to just override toString on Clients to return client's name, or whatever you want to be displayed and do not bother about the renderer.
  • call getValueAt() to get Clients instance bound to particular cell in your button click handler.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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