Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从非UI线程访问UI线程_Java_Swt_Eclipse Rcp_Jface - Fatal编程技术网

Java 从非UI线程访问UI线程

Java 从非UI线程访问UI线程,java,swt,eclipse-rcp,jface,Java,Swt,Eclipse Rcp,Jface,我试图将剪贴板的内容复制到文本框中,但收到一条“无效线程访问”消息 我在剪贴板上使用了一个侦听器,如果有更改,它会通知我 我创建了一个新线程来包装侦听器,但它似乎无法正常工作 代码如下: public class ClipboardDialog extends TitleAreaDialog { // Local attributs private Text mTextClipboardcontent; public ClipboardDialog (She

我试图将剪贴板的内容复制到文本框中,但收到一条“无效线程访问”消息

我在剪贴板上使用了一个侦听器,如果有更改,它会通知我

我创建了一个新线程来包装侦听器,但它似乎无法正常工作

代码如下:

    public class ClipboardDialog extends TitleAreaDialog {

    // Local attributs
    private Text mTextClipboardcontent;


    public ClipboardDialog (Shell parentShell) {

        super(parentShell);

    }


    @Override
    protected Control createDialogArea(Composite parent) {

        // Creating display and controls

        setTitle("ClipBoard content");
        Composite area = (Composite) super.createDialogArea(parent);
        Composite container = new Composite(area, SWT.NONE);
        container.setLayout(new FormLayout());
        GridData gd_container = new GridData(GridData.FILL_HORIZONTAL);
        // gd_container.grabExcessVerticalSpace = true;
        gd_container.horizontalAlignment = SWT.LEFT;
        gd_container.verticalAlignment = SWT.TOP;
        container.setLayoutData(gd_container);

        // Group control
        Group grpInputData = new Group(container, SWT.NONE);
        FormData fd_grpInputData = new FormData();
        fd_grpInputData.bottom = new FormAttachment(0, 303);
        fd_grpInputData.right = new FormAttachment(0, 467);
        fd_grpInputData.top = new FormAttachment(0, 5);
        fd_grpInputData.left = new FormAttachment(0, 5);
        grpInputData.setLayoutData(fd_grpInputData);
        grpInputData.setText("Input Data");
        GridLayout gl_grpInputData = new GridLayout(4, false);
        gl_grpInputData.marginLeft = 4;
        gl_grpInputData.marginHeight = 0;
        gl_grpInputData.marginWidth = 2;
        gl_grpInputData.verticalSpacing = 10;
        gl_grpInputData.horizontalSpacing = 10;
        grpInputData.setLayout(gl_grpInputData);
        new Label(grpInputData, SWT.NONE);
        new Label(grpInputData, SWT.NONE);
        new Label(grpInputData, SWT.NONE);
        new Label(grpInputData, SWT.NONE);
        new Label(grpInputData, SWT.NONE);


        // Input control
        mTextClipboardcontent = new Text(grpInputData, SWT.BORDER);
        GridData gdContent = new GridData(SWT.LEFT, SWT.TOP, false, false, 1,
                1);
        gdContent.heightHint = 18;
        gdContent.widthHint = 250;
        mTextClipboardcontent.setLayoutData(gdContent);
        mTextClipboardcontent.setBounds(0, 0, 76, 21);
        mTextClipboardcontent.setTextLimit(8);



        new Thread(new Runnable() {
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(1000);
                    } catch (Exception e) {
                    }
                    Display.getDefault().syncExec(new Runnable() {
                        public void run() {

                            Toolkit.getDefaultToolkit().getSystemClipboard()
                                    .addFlavorListener(new FlavorListener() {

                                        @Override
                                        public void flavorsChanged(FlavorEvent e) {

                                            String result = "";
                                            Clipboard clipboard = Toolkit
                                                    .getDefaultToolkit()
                                                    .getSystemClipboard();

                                            Transferable contents = clipboard
                                                    .getContents(null);
                                            boolean hasTransferableText = (contents != null)
                                                    && contents
                                                            .isDataFlavorSupported(DataFlavor.stringFlavor);

                                            if (hasTransferableText) {

                                                try {

                                                    // Getting the content of
                                                    // the ClipBoard
                                                    result = (String) contents
                                                            .getTransferData(DataFlavor.stringFlavor);

                                                    mTextContent.settext(result);
                                                } catch (UnsupportedFlavorException ex) {

                                                    // Loggin the exception
                                                    Log.LogError(
                                                            ex.getClass()
                                                                    .getCanonicalName(),
                                                            ex.getMessage());

                                                } catch (IOException ex) {


                                                    // Loggin the exception
                                                    Log.LogError(
                                                            ex.getClass()
                                                                    .getCanonicalName(),
                                                            ex.getMessage());
                                                }

                                            }
                                        }
                                    });

                        }
                    });
                }
            }
        }).start();

        return area;
    }

    /**
     * Create contents of the button bar.
     * 
     * @param parent
     */
    @Override
    protected void createButtonsForButtonBar(Composite parent) {
        Button mBtnOK = createButton(parent, IDialogConstants.OK_ID,
                IDialogConstants.OK_LABEL, true);
        mBtnOK.setEnabled(false);
        createButton(parent, IDialogConstants.CANCEL_ID,
                IDialogConstants.CANCEL_LABEL, false);
    }

    /**
     * Return the initial size of the dialog.
     */
    @Override
    protected Point getInitialSize() {
        return new Point(479, 400);
    }
}

在不是UI线程的线程中调用您的味道侦听器

您需要使用
asyncExec
(或
syncExec
,根据需要)方法在传递给
显示器的
可运行的
中执行其代码:

runnable将在UI线程上执行,从而避免出现错误

请注意,您可能不需要在UI线程中执行此操作:

 Toolkit.getDefaultToolkit().getSystemClipboard()
                                    .addFlavorListener(

在不是UI线程的线程中调用您的味道侦听器

您需要使用
asyncExec
(或
syncExec
,根据需要)方法在传递给
显示器的
可运行的
中执行其代码:

runnable将在UI线程上执行,从而避免出现错误

请注意,您可能不需要在UI线程中执行此操作:

 Toolkit.getDefaultToolkit().getSystemClipboard()
                                    .addFlavorListener(

我试过了,但没用!我使用了Display.getCurrent().asyncExec(new Runnable(){public void run(){});我进行了编辑以使其更清晰。问题是在注册它的线程中没有调用flavorListener。我尝试了它,但不起作用!我使用了Display.getCurrent().asyncExec(new Runnable(){public void run(){});我进行了编辑以使其更清晰。问题是在注册它的线程中没有调用flavorListener。为什么要将[awt]标记这个问题?我可以删除它吗?是的,你可以!我之所以放置它是因为剪贴板侦听器我忘了剪贴板在awt.datatransfer中…但这不是问题的本质,我将让问题不带[awt]标记。你为什么放置[awt]标记此问题?我可以删除它吗?是的,你可以!我之所以放置此问题,是因为剪贴板侦听器我忘记了剪贴板位于awt.datatransfer…但由于这不是问题的本质,我将让问题不带[awt]标记。