简体   繁体   中英

How to get cell value of jtable depending on which row is clicked

I am trying to use an update method on my jtable that's connected to the database and would like to fill in the textfields on the form depending on which row the users clicks. I understand I will be needing a getValueAt() method however I am uncertain of how to fill in which row depending on which row the user clicks. I am unable to find anything on Google or anything so any information would be helpful!

private final UrTableModel urTableModel;
private JTable urTable;
...

// 1. Create your table model class that should extends from DefaultTableModel, instantiate it
urTableModel=new UrTableModel();

// 2. creates table
table = TableUtils.createStandardSortableTable(urTableModel);

// 3. customize your table
table.setBackground(Color.WHITE);
table.getTableHeader().setReorderingAllowed(false);

// 4. Add the mouse listner to it
table.addMouseListener(new MouseAdapter() {
    @Override
    public void mouseClicked(final MouseEvent e) {
        if (e.getClickCount() == 1) {
            final JTable target = (JTable)e.getSource();
            final int row = target.getSelectedRow();
            final int column = target.getSelectedColumn();
            // Cast to ur Object type
            final UrObjctInCell urObjctInCell = (UrObjctInCell)target.getValueAt(row, column);
            // TODO WHAT U WANT!
        }
    }
});

Cheers,

You will need to call getValueAt() your table's model to get the values you need. You will also need a listener on the table to listen for selections. So that once a user selects a row you call getValueAt() to get the value for the specific column of data in that row.

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