views:

275

answers:

1

Hi Everyone,

Sorry if this was asked before, I googled everywhere with no luck. Here's my problem: I'd like to drag and drop rows within a JTable. I cannot get it to work without first selecting the row, which is annoying, I'd like to get a similar behavior the Windows explorer has : if I single click on an item and start moving the mouse while holding the left button, it drags my row, if I single click elsewhere and start moving the mouse while holding the left button, it does a multiple selection of the rows. Any help is greatly appreciated

A: 

You probably need to add a mouse listener that listens for mousePressed events, and then cause the selection of the row to be the row the mouse cursor is currently over.

table.addMouseListener(new MouseAdapter() {
    public void mousePressed(MouseEvent e) {
        Point p = e.getPoint();
        int row = table.rowAtPoint(p);
        table.setSelectedRow(row);
    }
});
aperkins