aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes TableViewer w/Checkbox Selection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "TableViewer w/Checkbox Selection" Watch "TableViewer w/Checkbox Selection" New topic
Author

TableViewer w/Checkbox Selection

Kevin Myers
Greenhorn

Joined: Aug 17, 2010
Posts: 9
Hi All, we have an app with a TableViewer containing a check box (image) and our users have requested we provide them the ability to just "swipe" select each row, rather than having to individually click on each one. To be specific, the want to just hold down the left mouse button and drag it over the checkbox cells, to quickly select many of them.

Does anyone know of a way to achieve this?
Kevin Workman
Ranch Hand

Joined: Sep 28, 2010
Posts: 151
I'd suggest you provide an SSCCE that demonstrates what you're talking about.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19783
    
  20

I don't think there is an SSCCE yet. But I think I know what Kevin M means.

The old way: you click a check box. You click the one below. You click the one below. Continue until done.

The preferred way: you click on a check box. You drag the mouse down. All check boxes encountered while pressed become checked. Continue until the mouse is released.

If this is done with a JTable then a MouseListener / MouseMotionListener (hooray for MouseInputListener!) could help.
Step 1: mouseClicked. Translate the point to a cell (using JTable's rowAtPoint and columnAtPoint methods). Check the current contents. Flip the boolean, store that, set that for the cell.
Step 2: mouseDragged. Again, translate, then set the stored value for the cell.
Step 3: mouseReleased. Well, you're done.

Keep in mind to only do this for the right columns.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Kevin Workman
Ranch Hand

Joined: Sep 28, 2010
Posts: 151
Rob Prime wrote:I don't think there is an SSCCE yet. But I think I know what Kevin M means.


Your crystal ball is more accurate than mine, then. I thought he was just talking about a click-drag selection of multiple rows in a JTable with a custom renderer. Hm.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19783
    
  20

I've got most of it implemented (no I'm not going to post code; we are NotACodeMill. In essence it's as I described though). First I encountered the issue where the cell where the mouse was pressed was not changed; the others were. I found out this is caused by the cell editor that's still active. After getting rid of that behaviour (either by canceling the editing or by making isCellEditable return false for that column; the listener will handle the toggling, even on single clicks). However, now the dragging also selects the cells. I'm sure someone can help solve that.
Kevin Myers
Greenhorn

Joined: Aug 17, 2010
Posts: 9
Thanks for the responses Kevin & Rob.
What Rob suggested is what I'm looking for...(although I'm not sure how it's different then what Kevin said).

Anyway, now I'm a bit confused, which is probably due to being very new at this stuff....but isn't JTable a swing component?

I was actually trying to implement this using an swt/jface table.
Kevin Workman
Ranch Hand

Joined: Sep 28, 2010
Posts: 151
Kevin Myers wrote:(although I'm not sure how it's different then what Kevin said).

If I understand Rob (and myself) accurately, we're talking about two different things: the selections (highlighted or not highlighted) of the table rows, and the selections (checked or not checked) of the checkboxes in a specific column of the table rows.

Kevin Myers wrote:I was actually trying to implement this using an swt/jface table.

That's another reason I suggested an SSCCE. I couldn't tell what components you were actually working with.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19783
    
  20

I think the same techniques could apply as my Swing example.
Kevin Myers
Greenhorn

Joined: Aug 17, 2010
Posts: 9
Ahh. Now I understand.
I am trying to "check" the check boxes by quickly running the mouse over them (with mouse key down), using an swt/jface table.

I'll try and throw together an SSCCE in a bit, just need to finish up a few things.
Kevin Workman
Ranch Hand

Joined: Sep 28, 2010
Posts: 151
Kevin Myers wrote:Ahh. Now I understand.
I am trying to "check" the check boxes by quickly running the mouse over them (with mouse key down), using an swt/jface table.

I'll try and throw together an SSCCE in a bit, just need to finish up a few things.


Yep, that's what Rob thought. I actually don't know anything about SWT, so I'll leave you to it. Good luck.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19783
    
  20

I think the same technique can be used. Replace java.awt.event.MouseListener with org.eclipse.swt.event.MouseListener and java.awt.event.MouseMotionListener with org.eclipse.swt.event.MouseMoveListener. The major difference is that that one doesn't have a dragged event, so you'll need to use the moved event and store whether or not the mouse is down (if this isn't stored in MouseEvent's stateMask field).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: TableViewer w/Checkbox Selection