hi i had created a Jtable Which Stores Data like below
--------------------------------------------------------
a b c d e
w q 1 2 9
--------------------------------------------------------

i want to get the cell value when i click to the cell... i tried lot but not got hte out put please help to get the output please

Dani AI

Generated

Short, practical summary and a robust pattern to get a JTable cell value on a mouse click (covers clicks, keyboard navigation, sorting/filtering and editing). pointed to the table/model API and suggested ListSelectionListener — both are useful. The approach below combines a mouse listener for direct clicks, selection listeners for keyboard navigation, and model/view index conversion so the right value is read when a RowSorter is active.

Enable cell selection, then for mouse clicks use the table coordinate helpers to find the clicked cell, convert those view indices to model indices, and read the model value. Converting is essential when you use sorting/filtering; otherwise you’ll read the wrong row. Also, if the cell is being edited, stop the editor before reading to get the committed value.

Example (mouse-click path):

table.setCellSelectionEnabled(true);

table.addMouseListener(new MouseAdapter() {
    @Override
    public void mouseClicked(MouseEvent e) {
        Point p = e.getPoint();
        int viewRow = table.rowAtPoint(p);
        int viewCol = table.columnAtPoint(p);
        if (viewRow < 0 || viewCol < 0) return;
        int modelRow = table.convertRowIndexToModel(viewRow);
        int modelCol = table.convertColumnIndexToModel(viewCol);
        Object value = table.getModel().getValueAt(modelRow, modelCol);
        // handle value
    }
});

Troubleshooting tips: ensure setCellSelectionEnabled(true) (or enable row/column selection) so selection APIs return indices; if you get -1 the click hit the header or outside the grid; if edits are involved call table.getCellEditor().stopCellEditing() first; and add ListSelectionListeners on both table.getSelectionModel() and table.getColumnModel().getSelectionModel() to handle keyboard navigation as described.

Recommended Answers

All 6 Replies

use

myTable.getValueAt(selectedRow,selectedColumn);

which returns an Object


hi i had created a Jtable Which Stores Data like below
--------------------------------------------------------
a b c d e
w q 1 2 9
--------------------------------------------------------

i want to get the cell value when i click to the cell... i tried lot but not got hte out put please help to get the output please

thanks for your answer but the selected row and column value is differ for each click

so, your selectedRow would be

myTable.getSelectedRow()

and your selectedColumn would be

myTable.getSelectedColumn()

thanks for your answer but the selected row and column value is differ for each click

Retreiving the data value in cell could be done by creating a class that implements ListSelectionListener and passed it as argument to the ListSelectionModel of the table and to the ListSelectionModel of ColumnModel of the given table.
hope it helps.

this approche will give the value of the cell if you click on or if you use the keyboard button navigaiton.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.