Class UnsupportedFlavorException in java

Class UnsupportedFlavorException in java

An exception of this type signals that a Transferable object could not provide data in the requested format.This exception is thrown from Transferable.getTransferData(DataFlavor) to indicate that the DataFlavor requested is not available.

UnsupportedFlavorException also occurs when we request Transferable.getTransferData() to provide data that is not supported by clipboard object.

Example- If the clipboard holds String data and we want image data or if the clipboard holds the image and we want string data. In either case, the clipboard object can not give us the requested data. In these situations, we get UnsupportedFlavorException.

Implemented interface- Serializable

Hierarchy of UnsupportedFlavorException

Object->Throwable(Serializable)->Exception->UnsupportedFlavorException

  • java.lang.Object
  • –java.lang.Throwable
  • —-java.lang.Exception
  • —-java.awt.datatransfer.UnsupportedFlavorException

Pictorial view:

Class UnsupportedFlavorException in java hierarchy
Class UnsupportedFlavorException in java hierarchy

The structure of the class UnsupportedFlavorException is given by

public class java.awt.datatransfer.UnsupportedFlavorException extends java.lang.Exception{
// Public Constructor
public UnsupportedFlavorException(DataFlavor flavor);
}

The details of the class structure are given as follows:

public UnsupportedFlavorException(DataFlavor flavor);

public UnsupportedFlavorException(DataFlavor flavor) constructs UnsupportedFlavorException with the given data flavor.

Parameter
flavor
The flavor that caused the exception.

Field inheritated from throwable:

  • serialVersionUID

The packages contribute to UnsupportedFlavorException are

java.awt.datatransfer–transfer data between applications

  • Transferable.getTransferData(DataFlavor flavor)
  • StringSelection.getTransferData(DataFlavor flavor)
  • DataFlavor.getReaderForText(Transferable transferable)

java.awt.dnd–Drag and Drop

  • DropTargetContext.TransferableProxy.getTransferData(DataFlavor df)

The class UnsupportedFlavorException also inherits methods from class Object and Throwable.

 

READ  IllegalThreadStateException in Java
From Object classFrom Throwable class
clone()fillInStackTrace()
getClass()getMessage()
notify()printStackTrace()
wait()printStackTrace(PrintWriter)
wait(long, int)getLocalizedMessage()
wait(long)printStackTrace(PrintStream)
equals(Object)toString()
finalize()addSuppressed()
hashCode()getCause()
notifyAll()getStackTrace()
getSuppressed()
initCause()
setStackTrace()

An Example on how to get UnsupportedFlavorException

import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;

public class Main {
  public static void main(String args[]) {
    String toClipboardText = "Hello from My Java!";
    StringSelection selectedText= new StringSelection(toClipboardText);
    Clipboard clipText = Toolkit.getDefaultToolkit().getSystemClipboard();
    clip.setContents(selectedText,selectedText);
    clipText = Toolkit.getDefaultToolkit().getSystemClipboard();
    Transferable contentsText = clipText.getContents(new Main().getClass());
    if (contentsText == null) {
      System.out.println("The clipboard is empty.");
      return;
    }
    if (contentsText.isDataFlavorSupported(DataFlavor.stringFlavor)) {
      try {
        String dataText = (String) contentsText.getTransferData(DataFlavor.stringFlavor);
        System.out.println(dataText);
      } catch (IOException ex) {
        System.out.println("IOException");
      } catch (UnsupportedFlavorException ex) {
        System.out.println("UnsupportedFlavorException");
      }
    } else{
      System.out.println("Wrong flavor.");
    }
  }
}

Why UnsupportedFlavorException can occurs?

In the below written scenarios UnsupportedFlavorException can occur from the programme.

  • ClipboardUtil$ImageSelection.getTransferData(…)
  • FileDrop$1.drop(…)
  • Drag and Drop nodes in JTree
  • RTATextTransferHandler$TextTransferable.getTransferData(…)
  • StyledTextTransferable.getTransferData(…)
  • ExTransferable$Empty.getTransferData(…)
  • CompositeTransferable.getTransferData(…)
  • Do$SXClipboard$TextTransferable.getTransferData(…)
  • CopyToHMTLAction$HtmlSelection.getTransferData(…)
  • DOMDocumentTree$TreeDropTargetListener.dragEnter(…)
  • ClipboardUtil$HtmlTransferable.getTransferData(…)
  • Swing application -> Drag & drop to the desktop / folder
  • TreeNodeRemoteMap.importData(…)
  • Not Making a JEditorPane with html put correctly formatted text in clipboard
  • PropertyTable.pasteFromClipboard()
  • PlainTextTransferable.getTransferData(…)
  • TreeNodeFolder$DropTransferable.doInBackground()
  • ReportSelection.getTransferData(…)
  • ImageUtils.getImageFromTransferable(…)
  • StaticSwingUtils.getFromClipboard()

Here are the methods that contributes to the UnsupportedFlavorException

  • requestLocationUpdates (LocationManager)
  • addToBackStack (FragmentTransaction)
  • findViewById (Activity)
  • Selector (java.nio.channels)
  • SQLException (java.sql)
  • TreeSet (java.util)
  • CountDownLatch (java.util.concurrent)
  • Loader (org.hibernate.loader)
  • Option (scala)

How to resolve UnsupportedFlavorException ?

We can perform the following things to stop UnsupportedFlavorException from occuring:

  • We need to make sure that copied data can support multiple flovors. So we need to put correct conditions with priorities and sequences.
  • Modify the default functionalities of Paste.

Share and Enjoy !

Leave a Reply

Your email address will not be published. Required fields are marked *