Results 1 to 2 of 2
  1. #1
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default Drag and Drop within JTable

    Hi, I am searching for JTable Drag and Drop I have seen some example but its not what I am looking for. What I am trying to do is to re-arrage or re-order the rows of JTable using drag and drop.

    Example, I have 5 rows, I want to put 5th row to the first row using drog and drop.

    What I have tried so far.
    Java Code:
        int selRow = -1;
        int dropOutRow = -1;
    
        private void tbl_LaborMouseClicked(java.awt.event.MouseEvent evt) {
            System.out.println("CLICKED");
        }
    
        private void tbl_LaborMouseMoved(java.awt.event.MouseEvent evt) {
            dropOutRow = tbl_Labor.rowAtPoint(evt.getPoint());
            selRow = tbl_Labor.getSelectedRow();
            System.out.println(selRow + ":m" + dropOutRow);
        }
        private void tbl_LaborMouseDragged(java.awt.event.MouseEvent evt) {
            DefaultTableModel model = (DefaultTableModel)tbl_Labor.getModel();
    
    
    
            if(dropOutRow > -1 && selRow >-1)
            {
            Object[] newRowData = new Object[3];
            newRowData[0] = tbl_Labor.getValueAt(selRow, 0);
            newRowData[1] = tbl_Labor.getValueAt(selRow, 1);
            newRowData[2] = tbl_Labor.getValueAt(selRow, 2);
    
            model.insertRow(dropOutRow, newRowData);
    
            tbl_Labor.setModel(model);
            System.out.println(selRow + ":" + dropOutRow);
            }
        }
    Thanks in advance,
    geje

  2. #2
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    Have done the problem above using moveRow method. Problem now is, it takes 5 to 6 seconds before
    the selected row move to the assigned row.
    Java Code:
        int selRow = -1;
        int dropOutRow = -1;
        private void tbl_LaborMouseMoved(java.awt.event.MouseEvent evt) {
            dropOutRow = tbl_Labor.rowAtPoint(evt.getPoint());
    
            DefaultTableModel model = (DefaultTableModel)tbl_Labor.getModel();
    
            if(dropOutRow > -1 && selRow >-1)
            {   Object[] newRowData = new Object[2];
                newRowData[0] = tbl_Labor.getValueAt(selRow, 0);
                newRowData[1] = tbl_Labor.getValueAt(selRow, 1);
    
                model.moveRow(selRow, selRow, dropOutRow);
                tbl_Labor.setModel(model);
                dropOutRow = -1;
                selRow = -1;
            }
        }
    
        private void tbl_LaborMousePressed(java.awt.event.MouseEvent evt) {
                selRow = tbl_Labor.getSelectedRow();
        }
    Is there any way in java code to make it more faster?

    Thanks,
    geje

Similar Threads

  1. drag and drop - disable intra component drop
    By tomba in forum AWT / Swing
    Replies: 4
    Last Post: 12-09-2009, 02:01 PM
  2. Drag and Drop in JTable
    By mahen.1981 in forum Advanced Java
    Replies: 0
    Last Post: 11-27-2009, 12:37 PM
  3. Drag and Drop
    By carderne in forum New To Java
    Replies: 0
    Last Post: 08-31-2009, 10:18 AM
  4. Drag and drop
    By thayalan in forum AWT / Swing
    Replies: 1
    Last Post: 02-16-2009, 04:04 PM
  5. Drag and drop
    By abhivenugopal in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 01-30-2008, 03:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •