简体   繁体   English

如何使 JTable 单元格不可编辑但应该能够选择和复制当前单元格中的值

[英]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?我试图使 JTable 单元格不可编辑,但如果我这样做,当我尝试复制整行被选中时,我无法选择单个单元格值,我只想复制选定的单元格值而不是整行。是有办法吗?

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);
               }

            }

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. Ctrl+C键的默认Action是复制整行。 If you only want the data of the currently selected cell then you need to replace the default Action with a custom Action .如果您只想要当前选定单元格的数据,则需要将默认Action替换为自定义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 .上面的代码将创建自定义Action并在JTableActionMap中替换它。 See Key Bindings .请参阅键绑定 The program provided in the link shows all the default Actions and the keyword for each Action.链接中提供的程序显示了所有默认操作和每个操作的关键字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM