如何让一个单元格表现得像它的可编辑但只读它?

sam*_*ell 2 java swing jtable tablecelleditor abstracttablemodel

我有一个JTable,其中我希望单元格在单元格可编辑时的行为方式,但单元格无法编辑,换句话说,只读.因此,如果我双击一个单元格,我应该只能在单元格中选择文本并从该单元格中复制文本.

cam*_*ckr 5

是否可以阻止用户进行任何更改?

您需要使用自定义编辑器:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.text.*;

public class TableCopyEditor extends JPanel
{
    public TableCopyEditor()
    {
        String[] columnNames = {"Editable", "Non Editable"};
        Object[][] data =
        {
            {"1", "one"},
            {"2", "two"},
            {"3", "three"}
        };

        JTable table = new JTable(data, columnNames);
        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        JScrollPane scrollPane = new JScrollPane( table );
        add( scrollPane );

        //  Create a non-editable editor, but still allow text selection

        Caret caret = new DefaultCaret()
        {
            public void focusGained(FocusEvent e)
            {
                setVisible(true);
                setSelectionVisible(true);
            }
        };
        caret.setBlinkRate( UIManager.getInt("TextField.caretBlinkRate") );

        JTextField textField = new JTextField();
        textField.setEditable(false);
        textField.setCaret(caret);
        textField.setBorder(new LineBorder(Color.BLACK));

        DefaultCellEditor dce = new DefaultCellEditor( textField );
        table.getColumnModel().getColumn(1).setCellEditor(dce);
    }

    private static void createAndShowUI()
    {
        JFrame frame = new JFrame("Table Copy Editor");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( new TableCopyEditor() );
        frame.pack();
        frame.setLocationByPlatform( true );
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }

}
Run Code Online (Sandbox Code Playgroud)