Skip to main content

Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » NatTable » Get index of currently selected column from within custom selection binding
Get index of currently selected column from within custom selection binding [message #1751255] Fri, 06 January 2017 12:02 Go to next message
Stephan Druskat is currently offline Stephan DruskatFriend
Messages: 102
Registered: October 2011
Location: Berlin, Germany
Senior Member

I've added a custom selection configuration to my NatTable's selectionLayer.

In this custom selection configuration I override addSelectionUIBindings() to add a new custom selection binding via addConfiguration(binding).

This custom binding extends DefaultSelectionBindings#configureUIBindings(UIBindingRegistry uiBindingRegistry) by adding a new key binding as follows.

public void configureUiBindings(UiBindingRegistry uiBindingRegistry) {
	uiBindingRegistry.registerKeyBinding(new IKeyEventMatcher() {
		public boolean matches(KeyEvent event) {
			if (event.keyCode == 'n') {
				return true;
			return false;
	}, new IKeyAction() {
		public void run(NatTable natTable, KeyEvent event) {
			System.out.println("N gives us the item " + natTable.getData("selectedItem"));

I've also added an ILayerListener to my NatTable, which listens to selection changes, and sets the selected object accordingly:

natTable.setData("selectedItem", item)

In the listener the actual Item is got via the absolute column index (using LayerUtil.convertColumnPosition(natTable, colPos, bodyDataLayer)), and getting an object from a list (my data provider extends ListDataProvider) at the specified column index. (FYI: My list objects aren't beans and don't have the properties I want to present so I have to hack my way around presenting them (e.g., by getting properties of connected nodes in the main graph object, while the Items are nodes themselves, but whatever).)

This all works fine, but I'd like to avoid calling NatTable.setData("selectedItem", item) every time the selection changes as I assume this isn't very performant. Hence I'd like to work via the selection itself if possible at all.

As I have a NatTable object in my KeyAction's run() method, it'd be great to get the actual absolute column index of the current selection via this.

I have tried via separate methods (e.g., setting the selectionLayer as data to the NatTable so I can retrieve it later, same hack as above, and doesn't work) but couldn't get it to work. The examples show nothing similar, so I'd like to inquire if there is something like a "global" option for getting the current selection with only a NatTable object in hand.

Re: Get index of currently selected column from within custom selection binding [message #1751269 is a reply to message #1751255] Fri, 06 January 2017 17:22 Go to previous message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2712
Registered: July 2012
Senior Member
No this is not possible. The NatTable is not aware of selection, as a NatTable might not contain a SelectionLayer in the layer composition.

Either create an action that knows the SelectionLayer, or use a command with a command handler on the SelectionLayer like we do with the DeleteSelectionAction or do a workaround like we do in the SearchAction or somewhere on the way and search for the SelectionLayer in the layer stack.

The question is what you are trying to achieve? Typically the command approach is best matching to perform actions in a NatTable
Previous Topic:NatTable a suitable widget for my application?
Next Topic:StyledText in TextPainter
Goto Forum:

Current Time: Sat Sep 21 02:55:24 GMT 2019

Powered by FUDForum. Page generated in 0.02836 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top