Search code examples
swtjfacetableviewer

How to lock swt Table or Jface tableviewer scrollbar


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.


Solution

  • [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);
    }
    }