This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

unexpected selection changes in JTable  RSS feed

 
Ron Katz
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,
I have a single selected JTable which it's elements support drag & drop.
When clicking on an item in the table and starting to drag it, sometimes, if I drag the mouse cursor over the next element in the JTable, the selection changes (while the mouse left button is still pressed!) to the other elemnt. Anyone have an idea what is wrong?

Thanks for your advise...

Ron
 
Ron Katz
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

If you read this, probably you suffered the same problem I did.

Here is the solution I found:

It appears that JList have a SelectionModel, which one may set by using the setSelectionModel method.

If you have a problem with selection that is being change while draging, you may try the following code:

class DragSelectionModel extends DefaultListSelectionModel implements MouseListener{



public void setSelectionInterval(int index0, int index1) {
if (buttonDown) {
return;
}
else {
super.setSelectionInterval(index0, index1);
}
}
public void mouseClicked(MouseEvent e){

}

public void mousePressed(MouseEvent e){
buttonDown = true;
}

public void mouseReleased(MouseEvent e){
buttonDown = false;
}

public void mouseEntered(MouseEvent e){

}

public void mouseExited(MouseEvent e){

}

}

you then set both: the selection model of the list and the mouse listener by calling the following methods:

DragSelectionModel dragSelectionListener = new DragSelectionModel();
setSelectionModel(dragSelectionListener);
addMouseListener(dragSelectionListener);

if you also implements drag and drop you should add the following as well (in case you use jdk1.4.x):

public void dragDropEnd(DragSourceDropEvent dsde) {
...
buttonDown = false;
}

* note that if you do not need dnd support the buttonDown may be declared inside the private class I suggested, if you do need dnd, you should declare it in your main class.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!