简体   繁体   中英

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

I'm trying to drag and drop cells in my 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

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. That is the responsibility of the 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 . It has an example that will disable drop on the first column.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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