我已将现有的SWT表转换为excel表但是当光标移动到表的右侧时(即在当前视图中的表外),水平条不会向右移动,但是垂直移动正在发生细
我在某处读到SWT表无法进行水平移动。
public void addTreeNavigationBehavior(final Tree treeComposite){
final TreeCursor cursor = new TreeCursor(treeComposite, SWT.NONE);
final ScrolledComposite scrolledComposite = new ScrolledComposite(treeComposite, SWT.NONE);
final ControlEditor editor = new ControlEditor(cursor);
editor.grabHorizontal = true;
editor.grabVertical = true;
cursor.addSelectionListener(new SelectionAdapter() {
// when the TableEditor is over a cell, select the corresponding row in
// the table
@Override
public void widgetSelected(SelectionEvent e) {
// cursor.scroll(0, 0, 0, 0, 0, 0, true);
//need to attach the a method to this say is selectable
scrolledComposite.setExpandHorizontal(true);
treeComposite.setSelection(cursor.getRow());
// treeComposite.getHorizontalBar().setSelection(cursor.getColumn());
// cursor.getHorizontalBar().setSelection(cursor.getColumn());
}
// when the user hits "ENTER" in the TableCursor, pop up a text editor so that
// they can change the text of the cell
@Override
public void widgetDefaultSelected(SelectionEvent e) {
final Text text = new Text(cursor, SWT.NONE);
TreeItem row = cursor.getRow();
int column = cursor.getColumn();
text.setText(row.getText(column));
text.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
// close the text editor and copy the data over
// when the user hits "ENTER"
if (e.character == SWT.CR) {
TreeItem row = cursor.getRow();
int column = cursor.getColumn();
row.setText(column, text.getText());
text.dispose();
}
// close the text editor when the user hits "ESC"
if (e.character == SWT.ESC) {
text.dispose();
}
}
});
// close the text editor when the user tabs away
text.addFocusListener(new org.eclipse.swt.events.FocusAdapter() {
@Override
public void focusLost(FocusEvent e) {
text.dispose();
}
});
editor.setEditor(text);
text.setFocus();
}
});
// Hide the TableCursor when the user hits the "CTRL" or "SHIFT" key.
// This allows the user to select multiple items in the table.
cursor.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.keyCode == SWT.CTRL
|| e.keyCode == SWT.SHIFT
|| (e.stateMask & SWT.CONTROL) != 0
|| (e.stateMask & SWT.SHIFT) != 0) {
cursor.setVisible(false);
}
}
});
// When the user double clicks in the TableCursor, pop up a text editor so that
// they can change the text of the cell.
cursor.addMouseListener(new MouseAdapter() {
@Override
public void mouseDown(MouseEvent e) {
final Text text = new Text(cursor, SWT.NONE);
TreeItem row = cursor.getRow();
int column = cursor.getColumn();
text.setText(row.getText(column));
text.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
// close the text editor and copy the data over
// when the user hits "ENTER"
if (e.character == SWT.CR) {
TreeItem row = cursor.getRow();
int column = cursor.getColumn();
row.setText(column, text.getText());
text.dispose();
}
// close the text editor when the user hits "ESC"
if (e.character == SWT.ESC) {
text.dispose();
}
}
});
// close the text editor when the user clicks away
text.addFocusListener(new org.eclipse.swt.events.FocusAdapter() {
@Override
public void focusLost(FocusEvent e) {
text.dispose();
}
});
editor.setEditor(text);
text.setFocus();
}
});
// Show the TableCursor when the user releases the "SHIFT" or "CTRL" key.
// This signals the end of the multiple selection task.
treeComposite.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
if (e.keyCode == SWT.CONTROL && (e.stateMask & SWT.SHIFT) != 0) return;
if (e.keyCode == SWT.SHIFT && (e.stateMask & SWT.CONTROL) != 0) return;
if (e.keyCode != SWT.CONTROL && (e.stateMask & SWT.CONTROL) != 0) return;
if (e.keyCode != SWT.SHIFT && (e.stateMask & SWT.SHIFT) != 0) return;
TreeItem[] selection = treeComposite.getSelection();
TreeItem row = (selection.length == 0) ? treeComposite.getTopItem() : selection[0];
treeComposite.showItem(row);
cursor.setSelection(row, cursor.getColumn());
cursor.setVisible(true);
cursor.setFocus();
}
});
}