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

后端 未结 2 1453
盖世英雄少女心
盖世英雄少女心 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: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,

提交回复
热议问题