在Java中为什么会出现java.lang.IllegalStateException:无法打开系统剪贴板

时间:2018-08-11 07:54:01

标签: java clipboard

在Java中为什么会出现java.lang.IllegalStateException:无法打开系统剪贴板吗?

这是在使用Java 1.8.0_181 25.181-b13的Windows上运行的

java.lang.IllegalStateException: cannot open system clipboard
    at sun.awt.windows.WClipboard.openClipboard(Native Method)
    at sun.awt.datatransfer.SunClipboard.getClipboardFormatsOpenClose(SunClipboard.java:327)
    at sun.awt.datatransfer.SunClipboard.isDataFlavorAvailable(SunClipboard.java:188)
    at com.jthink.songkong.ui.startdialog.editsongs.EditSongsTable$PasteAction$1.flavorsChanged(EditSongsTable.java:170)
    at sun.awt.datatransfer.SunClipboard$1SunFlavorChangeNotifier.run(SunClipboard.java:441)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

PasteAction方法是

public PasteAction(JTable tbl)
        {
            putValue(NAME, TextLabel.PASTEBUTTON.getMsg());
            putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_V, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
            table = tbl;

            final Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();

            cb.addFlavorListener(new FlavorListener()
            {
                @Override
                public void flavorsChanged(FlavorEvent e)
                {
                    try
                    {
                        setEnabled(cb.isDataFlavorAvailable(CellTransferable.CELL_DATA_FLAVOR)
                                || cb.isDataFlavorAvailable(DataFlavor.stringFlavor));
                    }
                    catch(IllegalArgumentException iae)
                    {
                        MainWindow.logger.log(Level.SEVERE, iae.getMessage(), iae);
                    }
                }
            });
            setEnabled(cb.isDataFlavorAvailable(CellTransferable.CELL_DATA_FLAVOR)
                    || cb.isDataFlavorAvailable(DataFlavor.stringFlavor));
        }

1 个答案:

答案 0 :(得分:0)

当通知Java应用程序其所有权已丢失时,就会发生这种情况,因此有时需要花一点时间让剪贴板“就绪”。通过向该方法添加一个非常短的sleep命令,我们将能够避免运行时异常。请转到下面的链接以获取更多信息

https://coderanch.com/t/377833/java/listen-clipboard