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

后端 未结 2 1445
盖世英雄少女心
盖世英雄少女心 2020-12-16 07:49

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 clic

相关标签:
2条回答
  • 2020-12-16 08:36

    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.

    0 讨论(0)
  • 2020-12-16 08:39
    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,

    0 讨论(0)
提交回复
热议问题