本文介绍了如何锁定swt表或Jface tableviewer滚动条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Tableviewer,其中添加对象的速度非常快,我需要用户能够选择某些元素.问题是我找不到使滚动条停止滚动的方法.我只想要一个按钮,它将表锁定在当前位置.

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.

也许你有答案.

推荐答案

:您可以像Eclipse控制台一样使用 Toggle 按钮.您需要将按钮事件保持在以下两种状态.

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

  1. table#setSelection(item)将选择最后一个添加的项目.(自动滚动)
  2. tableviewer#refresh(object,true,false)可用于保留选择(滚动锁定).
  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).

在这里,我创建了代码段,它将帮助您了解如何在tableviewer中使用刷新:

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

这篇关于如何锁定swt表或Jface tableviewer滚动条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 04:41