How to lock swt Table or Jface tableviewer scrollbar

微笑、不失礼 提交于 2019-12-11 12:31:48

问题


I have a Tableviewer in which Objects are added really fast and I need the user to be able to select certain elements. The problem is that I can't find a way to make the scrollbar stop scrolling. I just want a button, which locks the Table at the current position.

Maybe you have an answer.


回答1:


[Edit] : You can use Toggle Button as Eclipse console have. You need to maintain below two states of button event.

  1. table#setSelection(item) will select the last item whichever is added.(Auto Scroll)
  2. tableviewer#refresh(object,true,false) can be used to preserve the selection(Scroll Lock).

Here, I created Snippet which will help you to understand how to use refresh in tableviewer:

public class SamplePart {
private static int i = 1;
private Text txtInput;
private TableViewer tableViewer;

@Inject
private MDirtyable dirty;
private Table table;
private TableColumn column;
private Button btnNewButton;

@PostConstruct
public void createComposite(Composite parent) {
    parent.setLayout(new GridLayout(2, false));
    txtInput = new Text(parent, SWT.BORDER);
    txtInput.setMessage("Enter text to mark part as dirty");
    txtInput.addModifyListener(new ModifyListener() {
        @Override
        public void modifyText(ModifyEvent e) {
            dirty.setDirty(true);
        }
    });
    txtInput.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    btnNewButton = new Button(parent, SWT.TOGGLE);
    btnNewButton.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            if(!btnNewButton.getSelection()){
                //once button is pressed it will preserve the selection
                tableViewer.refresh( table , true, false);
            }
        }
    });
    btnNewButton.setText("scroll");
    tableViewer = new TableViewer(parent);
    table = tableViewer.getTable();
    column = new TableColumn(table, SWT.NONE);
    column.setText("Column " + 0);
    TableItem item = new TableItem(table, SWT.NONE);
    item.setText(0, "sample Item " );
    GridData gridData = new GridData(GridData.FILL_BOTH);
    gridData.horizontalSpan = 2;
    gridData.widthHint = 440;
    tableViewer.getTable().setLayoutData(gridData);
    Job job = new  Job("UI") {
        @Override
        protected IStatus run(IProgressMonitor monitor) {
            for(int j = 1 ; j<10000000; j++){
                Display.getDefault().syncExec(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.currentThread().sleep(100);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        addItem();
                        table.getColumn(0).pack();
                    }
                });
            }
            return ASYNC_FINISH;
        }
    };
    job.schedule();
}

private void addItem() {
    i++;
    TableItem item = new TableItem(table, SWT.NONE);
    item.setText(0, "sample Item "+i );
    if(!btnNewButton.getSelection()){
        table.setSelection(item);
    }
}

@Focus
public void setFocus() {
    tableViewer.getTable().setFocus();
}

@Persist
public void save() {
    dirty.setDirty(false);
}
}


来源:https://stackoverflow.com/questions/33604255/how-to-lock-swt-table-or-jface-tableviewer-scrollbar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!