简体   繁体   English

是否可以禁用JTable中特定单元格的拖放?

[英]Is it possible to disable drag and drop for specific cell in JTable?

I'm trying to drag and drop cells in my Jtable . 我正在尝试将单元格拖放到Jtable It works but now I would like to know is it possible to disable drag and drop only for the first cell for example because I don't want this cell to be modified? 它可以工作,但是现在我想知道是否可以仅对第一个单元格禁用拖放操作,例如,因为我不想修改此单元格?

Here is the code 这是代码

public class Test extends JFrame {
public Test() {
    setSize(500, 200);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel pan = new JPanel();
    pan.setLayout(new GridLayout(1, 1, 5, 5));

    Object[][] data = new Object[][] { { "00", "10", null }, { "01", "11", null }, { "02", "20", null } };
    String[] name = new String[] { "a", "b", "c" };
    DefaultTableModel model = new DefaultTableModel(data, name);
    JTable jt = new JTable(model);
    pan.add(jt);

    jt.setRowHeight(24);
    jt.setRowSelectionAllowed(false);

    jt.setFillsViewportHeight(true);
    jt.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    jt.setDragEnabled(true);
    jt.setDropMode(DropMode.USE_SELECTION);
    jt.setTransferHandler(new MyTransferHandlerT());

    setContentPane(pan);
    setVisible(true);
}

public static void main(String[] args) {
    new Test();
}
}

and the code for TransferHandler TransferHandler的代码

public class MyTransferHandlerT extends TransferHandler {

 private JTable table;
 private DefaultTableModel model;
 private int rowIndex;
 private int colIndex;

@Override
public int getSourceActions(JComponent c) {
    return MOVE;
}

@Override
protected Transferable createTransferable(JComponent source) {

    table= (JTable)source;
    model = (DefaultTableModel) table.getModel();
    rowIndex = table.getSelectedRow();
    colIndex = table.getSelectedColumn();


    model.getValueAt(rowIndex, colIndex);

    String value = (String)model.getValueAt(rowIndex, colIndex);
    Transferable t = new StringSelection(value);
    return t;
}

@Override
protected void exportDone(JComponent source, Transferable data, int action) {

    table= (JTable)source;
    model = (DefaultTableModel) table.getModel();
    rowIndex = table.getSelectedRow();
    colIndex = table.getSelectedColumn();

    model.setValueAt("", rowIndex, colIndex);
}

@Override
public boolean canImport(TransferSupport support) {
    if (!support.isDataFlavorSupported(DataFlavor.stringFlavor)) {
        return false;
    }
    return true;
}

@Override
public boolean importData(TransferSupport support) {

    table = (JTable) support.getComponent();
    Object data= null;
    int row = table.getSelectedRow();
    int col = table.getSelectedColumn();



    try {
        data = (Object) support.getTransferable().getTransferData(DataFlavor.stringFlavor);
    } catch (UnsupportedFlavorException e) {
        System.out.println("unsupported Flavor Exception");
        e.printStackTrace();
    } catch (IOException e) {
        System.out.println("IO Exception");
        e.printStackTrace();
    }

    model.setValueAt(data, row, col);
    model.fireTableStructureChanged();
    return false;
}
}

Thanks. 谢谢。

model.setValueAt(data, row, col);
//model.fireTableStructureChanged();

Do not invoke the fireTableStructureChanged() method directly. 不要直接调用fireTableStructureChanged()方法。 That is the responsibility of the TableModel. 那是TableModel的责任。

is it possible to disable drag and drop only for the first cell 是否可以仅对第一个单元格禁用拖放

See the section from the Swing tutorial on Location Sensitive Drop . 请参阅Swing教程中有关位置敏感下降的部分 It has an example that will disable drop on the first column. 它有一个示例,该示例将禁用在第一列上的放置。

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

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