Skip to main content

Disabling Drag Selection in JTable

5 replies [Last post]
codeshark84
Offline
Joined: 2004-02-04
Points: 0

I would like to disable drag selection in a JTable (and possibly JList) because a drag and drop operation is is often confused for a drag selection operation. I'm sure this is possible, but I haven't been able to find any references or sample code to disable drag selection.

Any help is very much appreciated,
Jeff

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Mikescher
Offline
Joined: 2012-10-20
Points: 0

Hi Jeff

I have currently the same problem (it is even since a long tiome on the Bug List)

Because i couldn't find a working solution i made my own:

public class SFixTable extends JTable {
private static final long serialVersionUID = 1082882838948078289L;

boolean pressed = false;
int  currSRow = -100;

public SFixTable(TableModel dm) {
super(dm);
}

public SFixTable() {
super();
}

public SFixTable(Vector<?> rowData, Vector<?> columnNames) {
super(rowData, columnNames);
}

@Override
protected void processMouseEvent(MouseEvent e) {
int row = rowAtPoint(e.getPoint());
int col = columnAtPoint(e.getPoint());
if (SwingUtilities.isLeftMouseButton(e) && !e.isShiftDown() && !e.isControlDown()) {
boolean isDragRelease = (e.getID() == MouseEvent.MOUSE_RELEASED) && row != currSRow;
boolean isStartClick = (e.getID() == MouseEvent.MOUSE_PRESSED);

if (row >= 0 && col >= 0) {
if (isStartClick) {
super.changeSelection(row, col, false, false);
} else if (isDragRelease) {
super.changeSelection(currSRow, col, false, false);
}
}
pressed = (e.getID() == MouseEvent.MOUSE_PRESSED);
if (pressed) {
currSRow = row;
} else {
currSRow = -100;
}
}

super.processMouseEvent(e);
}

@Override
public boolean isCellSelected(int row, int col) {
return (pressed)? (row == currSRow) : super.isCellSelected(row, col);
}

}

By,
Mike

shan-man
Offline
Joined: 2006-02-17
Points: 0

Hi Jeff,

If you turn off drag gesture recognition, how do you plan to start a drag? If you want to do it yourself, simply don't call setDragEnabled(true) on the component, and when your code decides to do a drag it can explicitly call exportAsDrag() on the component's TransferHandler.

Thanks!
Shannon

shan-man
Offline
Joined: 2006-02-17
Points: 0

Hi Jeff,

It sounds like you might be writing your own drag recognition using AWT's drag and drop classes directly. It is [b]very[/b] complicated to do this and make it work correctly with respect to Swing components and selection. For this reason, we recommend against doing so.

The preferred approach is to use Swing's built-in drag and drop support. Turning on DnD can be as simple as calling [i]component.setDragEnabled(true)[/i]. See the Swing tutorial for more information:

http://java.sun.com/docs/books/tutorial/uiswing/misc/dnd.html

Regards,
Shannon

codeshark84
Offline
Joined: 2004-02-04
Points: 0

Hi Shannon,

Thanks for your reply. At this point I haven't written any drag and drop code. What I want to do is disable AWT's default behavior of doing a selection when a drag gesture is recognized. Currently when users try to perform a drag move/copy opertation AWT's drag gesture recognizer usually registers their action as a drag select. I just want to turn off the drag selection gesture recognition and leave drag move/copy enabled. Is that possible without having to rewrite all of AWTs the drag and drop code for a JTable or JList?

Thanks,
Jeff

Anonymous

Hi Jeff,

I had the same problem with one of my class...
I've found this post on java forums :

http://forum.java.sun.com/thread.jspa?threadID=539232&tstart=210

It might be interesting for you ;) (at least it solved the problem for me)

Bye,
Rom