How to disable jtable cell selection with mouse and drag

I want to use JTable, but with a different cell selection method, rather than clicking on cells. I use a glass panel so that users can draw a rectangle, and I discover which cells are contained in the rectangle for selection. But I only want to select the cell if it is completely inside the rectangle. For example, imagine a simple 3 x 3 table. My user wants to select the middle cell (1,1). I want them to be able to click on the first cell of 0.0 and drag it to cell 2.2, drawing a rectangle for selection. But I do not want to select cells of 0.0 or 2.2. Or get all 9 selected, for that matter. This is a simplified example. Drawing a rectangle can actually span cells from more than one table on the screen.

I have studied the implementation of the ListSelectionModel interface, but I do not want to rewrite it. During the experiments, I saw that setSelectionInterval () in the model was called like crazy (for every mouse movement) when I click and move in the table. I would really like to find an easy way to disable any listener / mechanism on the table that calls SelectionModel calls, keeping the model in place. I would still like it to report isSelectedIndex (), for example. I would tell the models which intervals are selected.

I realized that somewhere there is a mouse input adapter built into JTables? I would like to disable it if possible.

Thank you in

+4
source share
1 answer

For a general / text question you get a general text answer. :) Start by uninstalling MouseListeners installed in the default table:

MouseListener[] listeners = myTable.getMouseListeners(); for (MouseListener l : listeners) { myTable.removeMouseListener(l); } 

Then add your own MouseListener to the table (use the MouseAdapter if you want) and override the mousePressed / mouseReleased methods to record the points where the drag started and the drag ended. Define a closed rectangle and call:

 setRowSelectionInterval(#, #) setColumnSelectionInterval(#,#) 

to select one (or more) cells.

+3
source

Source: https://habr.com/ru/post/1394324/


All Articles