Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8199498

Custom cursor not supported in SwingNode

    Details

    • Type: Enhancement
    • Status: Closed
    • Priority: P4
    • Resolution: Duplicate
    • Affects Version/s: 8, 10, openjfx11
    • Fix Version/s: None
    • Component/s: javafx
    • Subcomponent:
    • CPU:
      generic
    • OS:
      generic

      Description

      A DESCRIPTION OF THE REQUEST :
      When a custom cursor is created in the swing node it is node correctly converted to JavaFX Cursor.
      The issue is that there is no case for value Cursor.CUSTOM_CURSOR in the switch-case of th method static javafx.scene.Cursor embedCursorToCursor(final Cursor cursor).

      The missing case is:
      case Cursor.CUSTOM_CURSOR:
                      return createImageCursor(cursor);

      The body of the createImageCursor is:
      private static ImageCursor createImageCursor(final Cursor cursor)
          {
              Icon cursorIcon = null;
              try
              {
                  final Field field = cursor.getClass().getSuperclass().getDeclaredField("image");
                  field.setAccessible(true);
                  final Image cursorImage = (Image) field.get(cursor);

                  if(cursorImage != null)
                      cursorIcon = new ImageIcon(cursorImage);
              }
              catch(final NoSuchFieldException | IllegalAccessException e)
              {
                  e.printStackTrace();
              }
              return new ImageCursor(ImageConverter.swingIconToImage(cursorIcon));
          }

      Because field image is private I used reflection to access it but you should be able to do it without reflection.


      JUSTIFICATION :
      Custom cursor are not currently supported in JavaFx SwingNode.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      I would see custom cursor when I display a swing control with a custom cursor.
      ACTUAL -
      The default cursor is displayed instead the custom one.

      ---------- BEGIN SOURCE ----------
      public class SwingNodeSample extends Application
      {
          @Override
          public void start(Stage stage)
          {
              final SwingNode swingNode = new SwingNode();

              createSwingContent(swingNode);

              StackPane pane = new StackPane();
              pane.getChildren().add(swingNode);

              stage.setTitle("Swing in JavaFX");
              stage.setScene(new Scene(pane, 250, 150));
              stage.show();
          }

          private void createSwingContent(final SwingNode swingNode)
          {
              SwingUtilities.invokeLater(() ->
              {
                  JPanel content = new JPanel();
                  content.setBackground(Color.BLUE);
                  createCustomCursor(content);
                  swingNode.setContent(content);
              });
          }

          private void createCustomCursor(JPanel panel)
          {
              Toolkit toolkit = Toolkit.getDefaultToolkit();
              String path = getClass().getResource("/ca/rheinmetall/java_fx/ui/javafx/drag_move_cursor.png").getFile();
              Image image = new ImageIcon(path).getImage();
              Cursor cursor = toolkit.createCustomCursor(image, new Point(0, 0), "img");
              panel.setCursor(cursor);
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      see description field.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                pmangal Priyanka Mangal (Inactive)
                Reporter:
                webbuggrp Webbug Group
              • Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: