javaswingjtableawttablecelleditor

how to make JTable cell non editable but should be able to select and copy the value in current cell


I am trying to make JTable cells non-editable but if i do so Iam unable to select a single cell value instead when i try to copy the entire row is getting selected I want to copy only the selected cell value instead of entire row.Is there a way to do it?

public class EmployeeWin extends JFrame {

        DefaultTableModel model = new DefaultTableModel() {
            @Override
            public boolean isCellEditable(int row, int column){
                return false;
            }
        };
        Container cont = this.getContentPane();
        JTable tab = new JTable(model);
        private TableRowSorter<TableModel> rowSorter = new TableRowSorter<>(model);
        private final JTextField searchFilter = new JTextField();

        public EmpDataWin(List<EmployeeDTO> pEmployeeDTO) {
            initialize(pEmployeeDTO);
        }

        public void initialize(List<EmployeeDTO> pEmployeeDTOList) {

            JPanel panelParent = new JPanel(new BorderLayout());

            // Add Header

            model.addColumn("Employee Name");
            model.addColumn("Department");
            model.addColumn("Details");

           // Add data row to table

            for (EmployeeDTO aEmployeeDTO : pEmployeeDTOList) {
                model.addRow(new Object[] { aEmployeeDTO.getEmployee_Name(), aEmployeeDTO.getDepartment(),
                        aEmployeeDTO.getDetails()});
            }

            tab.setRowSorter(rowSorter);
            tab.setAutoCreateRowSorter(true);


            JPanel panel = new JPanel(new BorderLayout());
            panel.add(new JLabel(UIConstants.SEARCH), BorderLayout.WEST);
            JTextField searchFilter = SearchFilter.createRowFilter(tab);
            panel.add(searchFilter, BorderLayout.CENTER);
            panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

            tab.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
            JScrollPane sp = new JScrollPane(tab,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
            panelParent.add(panel,BorderLayout.NORTH);
            panelParent.add(sp,BorderLayout.CENTER);
            panelParent.setBorder(BorderFactory.createEmptyBorder(10 , 10, 10, 10));
            cont.add(panelParent);
            this.pack();

                }

                public static void main(String[] args) {

                    EmployeeDAO dao = new EmployeeDAO();
                    List<EmployeeDTO> dto = dao.getemployeeData();

                    JFrame frame = new EmployeeDataWin(dto);
               }

            }

Solution

  • when i try to copy the entire row is getting selected I want to copy only the selected cell value instead of entire row

    The default Action for the Ctrl+C key is to copy the entire row. If you only want the data of the currently selected cell then you need to replace the default Action with a custom Action.

    The logic would be something like:

    Action copyCell = new AbstractAction()
    {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            JTable table = (JTable)e.getSource();
            int row = table.getSelectedRow();
            int column = table.getSelectedColumn();
            Object value = table.getValueAt(row, column);
    
            // copy the data to the clipboard
    
            Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
            StringSelection testData = new StringSelection( value.toString() );
            c.setContents(testData, testData);
        }
    };
    
    table.getActionMap().put("copy", copyCell);
    

    The above code will create the custom Action and replace it in the ActionMap of the JTable. See Key Bindings. The program provided in the link shows all the default Actions and the keyword for each Action.