Hi guys,
When I try to focus and select some cell in
a grid and right after this to press “Arrow Down” key, I have got
NullPointerException.
I have used the first snippet of grid (and
changed it a little bit J) to reproduce the problem. Here is the snippet:
package com.maconomy.widgets.snippets;
import
org.eclipse.nebula.widgets.grid.Grid;
import
org.eclipse.nebula.widgets.grid.GridColumn;
import
org.eclipse.nebula.widgets.grid.GridItem;
import org.eclipse.swt.SWT;
import
org.eclipse.swt.events.SelectionAdapter;
import
org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
/**
* Creates a simple grid and a button
for focusing and selection of the first cell
* of the grid.
*/
public class
MxSnippet001GridCellFocusingAndSelection {
/**
* Main entry.
* @param args command line
arguments
*/
public static void main(final String
[] args) {
final
Display display = new Display ();
final Shell
shell = new Shell (display);
//
shell.setLayout(new FillLayout());
// The
following code line was inserted instead of the line commented out above in
// order to
be able to add a button below the grid.
shell.setLayout(new GridLayout(1, false));
final Grid
grid = new Grid(shell, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
grid.setHeaderVisible(true);
final
GridColumn column = new GridColumn(grid, SWT.NONE);
column.setTree(true);
column.setText("Column 1");
column.setWidth(100);
final
GridItem item1 = new GridItem(grid, SWT.NONE);
item1.setText("Root Item");
// final
GridItem item2 = new GridItem(item1, SWT.NONE);
// The
following line was inserted instead of the line commented out above.
final
GridItem item2 = new GridItem(grid, SWT.NONE);
item2.setText("Second item");
// final
GridItem item3 = new GridItem(item2, SWT.NONE);
// The
following line was inserted instead of the line commented out above.
final
GridItem item3 = new GridItem(grid, SWT.NONE);
item3.setText("Third Item");
// The following
code is added to demonstrate the defect with the grid cell
// focusing
and selection.
grid.setCellSelectionEnabled(true);
// The
following button is used to focus and select the first cell of the grid.
// Try to
press "Arrow Down" key right after clicking on this button.
final Button
button = new Button(shell, SWT.BORDER);
button.addSelectionListener(new SelectionAdapter() {
/** {@inheritDoc} */
@Override
public void widgetSelected(final SelectionEvent e) {
grid.forceFocus();
// We tried to use the following three code lines in order to both focus and
// select some cell of the grid, but as it was found, the third code line leads
// to the NullPointerException. If we do not use the third line, the cell is in
// fact selected, but NOT FOCUSED (and focusing is what we need).
grid.setCellSelection(new Point(0, 0));
grid.setFocusItem(grid.getItem(0));
// When you comment out the following code line the first cell will be selected
// but NOT FOCUSED. You will see that the first cell of the grid being not
// focused is indicated by the fact that the border of the cell is not present.
// If you do not comment the line the first cell will be both focused and
selected,
// but pressing "Arrow Down" key leads to NullPointerException.
grid.setFocusColumn(grid.getColumn(0));
}
});
button.setText("Click this button to select and focus the first cell of
the grid. " +
"Right after clicking this button press \"Arrow Down\" key.");
//shell.setSize(200,200);
// The
following line was inserted instead of the line commented out above.
shell.pack();
shell.open
();
while
(!shell.isDisposed()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
}
Do you know why it might be a problem?
Best regards,
Vadym Yepishov