判断剪切板Transferable内容是否是String
- 方法1, 从clipboard获得transferable, 再用transferable的
isDataFlavorSupported(DataFlavor flavor)方法判断
boolean 是否剪切板的内容是文本 = transferable.isDataFlavorSupported(DataFlavor.stringFlavor);
- 方法2 直接用clipboard的
isDataFlavorAvailable(DataFlavor flavor)方法判断 , 该方法1.5版才有
boolean 是否剪切板的内容是文本 = systemClipboard.isDataFlavorAvailable(DataFlavor.stringFlavor);
Clipboard的isDataFlavorAvailable(DataFlavor flavor)方法从1.5版本开始才有, 可以简化代码, 其实质也是获得Transferable实例后调用其isDataFlavorSupported(DataFlavor flavor)方法判断
源码如下
/**
* Returns whether or not the current contents of this clipboard can be
* provided in the specified {@code DataFlavor}.
*
* @param flavor the requested {@code DataFlavor} for the contents
* @return {@code true} if the current contents of this clipboard can be
* provided in the specified {@code DataFlavor}; {@code false}
* otherwise
* @throws NullPointerException if {@code flavor} is {@code null}
* @throws IllegalStateException if this clipboard is currently unavailable
* @since 1.5
*/
public boolean isDataFlavorAvailable(DataFlavor flavor) {
if (flavor == null) {
throw new NullPointerException("flavor");
}
Transferable cntnts = getContents(null);
if (cntnts == null) {
return false;
}
return cntnts.isDataFlavorSupported(flavor);
}
这里是引用