-
Bug
-
Resolution: Unresolved
-
P4
-
None
-
6
-
x86
-
windows_vista
FULL PRODUCT VERSION :
java version "1.6.0_05"
Java(TM) SE Runtime Environment (build 1.6.0_05-b13)
Java HotSpot(TM) Client VM (build 10.0-b19, mixed mode, sharing)
ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows [Version 6.0.6001]
A DESCRIPTION OF THE PROBLEM :
I'm trying to start a drag from a JTable the "old-fashioned" way with DragSource and DragGestureListener (our application needs to run on non-Java-6 systems like Mac OS X, too). Unfortunately, this corrupts the existing selection and only selects the currently clicked cell.
4.1, 4.2
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Use the left frame (without a work-around):
- select a couple of lines (e.g. by drag-selecting in the second column)
- now try to start a drag in the first column
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
the drag should start without changing the previously made selection
ACTUAL -
the selection is changed so only the line is selected where the drag started
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
import java.awt.*;
import java.awt.datatransfer.*;
import java.awt.dnd.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
public class DragTableExample {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new DragTableExample(false);
new DragTableExample(true);
}
});
}
public DragTableExample(boolean tryToFixSelectionProblem) {
final JTable table = new JTable(new Object[][] {
new Object[] {"1.1", "1.2"},
new Object[] {"2.1", "2.2"},
new Object[] {"3.1", "3.2"},
new Object[] {"4.1", "4.2"},
new Object[] {"5.1", "5.2"},
new Object[] {"6.1", "6.2"},
new Object[] {"7.1", "7.2"},
}, new Object[] {
"A", "B"
});
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
if (tryToFixSelectionProblem) {
// this code tries to fix the default discarding of the selected rows when starting to drag on a selection
table.setTransferHandler(new TransferHandler() {
@Override
public void exportAsDrag(JComponent comp, InputEvent e, int action) {
}
@Override
public int getSourceActions(JComponent c) {
return MOVE;
}
});
table.setDragEnabled(true);
}
// configure drag source
DragSource.getDefaultDragSource().createDefaultDragGestureRecognizer(table, DnDConstants.ACTION_MOVE,
new DragGestureListener() {
public void dragGestureRecognized(DragGestureEvent dge) {
DragTableExample.this.dragGestureRecognized(table, dge);
}
});
final JFrame frame = new JFrame("Drag starts only on first column");
frame.getContentPane().add(BorderLayout.CENTER, new JScrollPane(table));
frame.getContentPane().add(BorderLayout.SOUTH, new JLabel(tryToFixSelectionProblem
? "When trying to drag-select on an already selected line in the second column, nothing happens"
: "When starting to drag on selected rows, only the clicked line will remain selected"));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation(tryToFixSelectionProblem ? screenSize.width / 2 : screenSize.width / 2 - frame.getWidth(),
(screenSize.height - frame.getHeight()) / 2);
frame.setVisible(true);
}
private void dragGestureRecognized(JTable table, DragGestureEvent dge) {
final int[] selectedRows = table.getSelectedRows();
if (selectedRows.length == 0) {
return;
}
// allow to start drag only from 1st (visible) column
final Point dragOrigin = dge.getDragOrigin();
if (table.columnAtPoint(dragOrigin) != 0) {
return;
}
final String draggedText = getText(selectedRows, table.getColumnCount(), table.getModel());
System.out.println("String to drag following text:");
System.out.println(draggedText);
dge.startDrag(null, new StringSelection(draggedText), null);
}
private static String getText(int[] selectedRows, int columnCount, TableModel model) {
final StringBuilder builder = new StringBuilder();
for (int i = 0; i < selectedRows.length; i++) {
if (i > 0) {
builder.append("\n");
}
final int row = selectedRows[i];
for (int column = 0; column < columnCount; column++) {
if (column > 0) {
builder.append(", ");
}
final Object value = model.getValueAt(row, column);
builder.append(value);
}
}
return builder.toString();
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
The work-around is shown in the right frame: you now can start the drag in the left column keeping multiple selected rows selected.
Unfortunately, now the usual drag-select in the second column does not work correctly any more: when starting the drag-select (in the second column) on an unselected row, everything is OK. But when starting the drag-select (in the second column) on an already selected row, nothing happens.
java version "1.6.0_05"
Java(TM) SE Runtime Environment (build 1.6.0_05-b13)
Java HotSpot(TM) Client VM (build 10.0-b19, mixed mode, sharing)
ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows [Version 6.0.6001]
A DESCRIPTION OF THE PROBLEM :
I'm trying to start a drag from a JTable the "old-fashioned" way with DragSource and DragGestureListener (our application needs to run on non-Java-6 systems like Mac OS X, too). Unfortunately, this corrupts the existing selection and only selects the currently clicked cell.
4.1, 4.2
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Use the left frame (without a work-around):
- select a couple of lines (e.g. by drag-selecting in the second column)
- now try to start a drag in the first column
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
the drag should start without changing the previously made selection
ACTUAL -
the selection is changed so only the line is selected where the drag started
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
import java.awt.*;
import java.awt.datatransfer.*;
import java.awt.dnd.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
public class DragTableExample {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new DragTableExample(false);
new DragTableExample(true);
}
});
}
public DragTableExample(boolean tryToFixSelectionProblem) {
final JTable table = new JTable(new Object[][] {
new Object[] {"1.1", "1.2"},
new Object[] {"2.1", "2.2"},
new Object[] {"3.1", "3.2"},
new Object[] {"4.1", "4.2"},
new Object[] {"5.1", "5.2"},
new Object[] {"6.1", "6.2"},
new Object[] {"7.1", "7.2"},
}, new Object[] {
"A", "B"
});
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
if (tryToFixSelectionProblem) {
// this code tries to fix the default discarding of the selected rows when starting to drag on a selection
table.setTransferHandler(new TransferHandler() {
@Override
public void exportAsDrag(JComponent comp, InputEvent e, int action) {
}
@Override
public int getSourceActions(JComponent c) {
return MOVE;
}
});
table.setDragEnabled(true);
}
// configure drag source
DragSource.getDefaultDragSource().createDefaultDragGestureRecognizer(table, DnDConstants.ACTION_MOVE,
new DragGestureListener() {
public void dragGestureRecognized(DragGestureEvent dge) {
DragTableExample.this.dragGestureRecognized(table, dge);
}
});
final JFrame frame = new JFrame("Drag starts only on first column");
frame.getContentPane().add(BorderLayout.CENTER, new JScrollPane(table));
frame.getContentPane().add(BorderLayout.SOUTH, new JLabel(tryToFixSelectionProblem
? "When trying to drag-select on an already selected line in the second column, nothing happens"
: "When starting to drag on selected rows, only the clicked line will remain selected"));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation(tryToFixSelectionProblem ? screenSize.width / 2 : screenSize.width / 2 - frame.getWidth(),
(screenSize.height - frame.getHeight()) / 2);
frame.setVisible(true);
}
private void dragGestureRecognized(JTable table, DragGestureEvent dge) {
final int[] selectedRows = table.getSelectedRows();
if (selectedRows.length == 0) {
return;
}
// allow to start drag only from 1st (visible) column
final Point dragOrigin = dge.getDragOrigin();
if (table.columnAtPoint(dragOrigin) != 0) {
return;
}
final String draggedText = getText(selectedRows, table.getColumnCount(), table.getModel());
System.out.println("String to drag following text:");
System.out.println(draggedText);
dge.startDrag(null, new StringSelection(draggedText), null);
}
private static String getText(int[] selectedRows, int columnCount, TableModel model) {
final StringBuilder builder = new StringBuilder();
for (int i = 0; i < selectedRows.length; i++) {
if (i > 0) {
builder.append("\n");
}
final int row = selectedRows[i];
for (int column = 0; column < columnCount; column++) {
if (column > 0) {
builder.append(", ");
}
final Object value = model.getValueAt(row, column);
builder.append(value);
}
}
return builder.toString();
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
The work-around is shown in the right frame: you now can start the drag in the left column keeping multiple selected rows selected.
Unfortunately, now the usual drag-select in the second column does not work correctly any more: when starting the drag-select (in the second column) on an unselected row, everything is OK. But when starting the drag-select (in the second column) on an already selected row, nothing happens.