Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[native-image] ClassCastException when using SystemTray #3414

Closed
pquiring opened this issue May 17, 2021 · 19 comments
Closed

[native-image] ClassCastException when using SystemTray #3414

pquiring opened this issue May 17, 2021 · 19 comments
Assignees

Comments

@pquiring
Copy link

Describe the issue
Getting ClassCastException when using a SystemTray icon.

Steps to reproduce the issue
Sample code:

/**
 *
 * @author pquiring
 */

import java.awt.*;
import java.awt.image.*;

public class Main extends javax.swing.JFrame {

  SystemTray tray;
  Image image;
  TrayIcon icon;

  /**
   * Creates new form Main
   */
  public Main() {
    initComponents();
    image = new BufferedImage(16,16,BufferedImage.TYPE_INT_ARGB);
    icon = new TrayIcon(image);
    tray = SystemTray.getSystemTray();
    try {
      tray.add(icon);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  /**
   * This method is called from within the constructor to initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is always
   * regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    jButton1 = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jButton1.setText("Test");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jButton1ActionPerformed(evt);
      }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addComponent(jButton1)
        .addContainerGap(337, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addComponent(jButton1)
        .addContainerGap(266, Short.MAX_VALUE))
    );

    pack();
  }// </editor-fold>//GEN-END:initComponents

  private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    // TODO add your handling code here:
  }//GEN-LAST:event_jButton1ActionPerformed

  /**
   * @param args the command line arguments
   */
  public static void main(String args[]) {
    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
      public void run() {
        new Main().setVisible(true);
      }
    });
  }

  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JButton jButton1;
  // End of variables declaration//GEN-END:variables
}

Describe GraalVM and your environment:

  • GraalVM version 21.1.0
  • JDK major version: 11
  • OS: Windows 10
  • Architecture: AMD64

More details
Exception:

Exception in thread "AWT-Windows": java.lang.ClassCastException
java.lang.ClassCastException: java.awt.TrayIcon cannot be cast to java.awt.Component
        at com.oracle.svm.jni.JNIJavaCallWrappers.jniInvoke_VA_LIST:Ljava_awt_event_MouseEvent_2_0002e_0003cinit_0003e_00028Ljava_awt_Component_2IJIIIIIIZI_00029V(JNIJavaCallWrappers.java:0)
        at sun.awt.windows.WToolkit.eventLoop(WToolkit.java)
        at sun.awt.windows.WToolkit.run(WToolkit.java:305)
        at java.lang.Thread.run(Thread.java:829)
        at com.oracle.svm.core.thread.JavaThreads.threadStartRoutine(JavaThreads.java:553)
        at com.oracle.svm.core.windows.WindowsJavaThreads.osThreadStartRoutine(WindowsJavaThreads.java:138)

NOTE : You must copy ${java.home}\lib\*font* to .\lib

Making great progress with AWT!

@pquiring
Copy link
Author

@munishchouhan munishchouhan self-assigned this May 21, 2021
@munishchouhan
Copy link
Contributor

@pquiring AWT support on windows is still in progress
#3084

@munishchouhan
Copy link
Contributor

Closing this as there is no activity in the last 30 days,
Please follow #3084
Once its implemented, please try again

@n1kub
Copy link

n1kub commented Mar 14, 2023

Facing same issue. @pquiring did you solve it? Is there any updates or maybe you've found any workaround?

@pquiring
Copy link
Author

No, problem persists. I've even opened ticket with JDK to get them to fix it there. No change.
The problem is TrayIcon is cast into an AWT Component in native code but it does NOT derive from Component.
Also tried to change TrayIcon to derive from Component in labsjdk but get compiler errors.
If I get it to work I'll post a revised JDK somewhere.

@n1kub
Copy link

n1kub commented Mar 23, 2023

@pquiring Thanks for your answer! Shall we open new issue? Maybe the'll pay attention to it now...

@pquiring
Copy link
Author

@n1kub - I opened several tickets against the JDK itself, one was just last month.

@n1kub
Copy link

n1kub commented Mar 24, 2023

@pquiring I found only this one
labs-openjdk-11 issue №4

@n1kub
Copy link

n1kub commented Apr 4, 2023

@pquiring is this your ticket?

@pquiring
Copy link
Author

Yes.

@pquiring
Copy link
Author

I've created a fix (hack) around this issue. It works!

https://github.com/pquiring/labs-openjdk-20/releases/tag/fix-001

@pquiring
Copy link
Author

pquiring commented May 19, 2023

The tray icon works, but now the popup menu is in another language ?

bug-3414-continues

@MartinHelwig
Copy link

@pquiring, try using JPopupMenu and JMenuItem... this displays correct labels.

@pquiring
Copy link
Author

Unfortunately TrayIcon does not support JPopupMenu. There is a workaround using addMouseListener() but my hack disables mouse events.

@MartinHelwig
Copy link

OK, I didn't use your hack, I only use the dev version graalvm-community-openjdk-17.0.7+4.1 and get bad language, than I changed to JPopupMenu with the workaround and it's working for me.

@pquiring
Copy link
Author

The latest graal 23.1.0 dev builds have fixed this issue. See #6524

@GPopZach
Copy link

GPopZach commented Mar 7, 2024

I've created a fix (hack) around this issue. It works!

https://github.com/pquiring/labs-openjdk-20/releases/tag/fix-001

I still have the issue right now, can you please provide the hack you used to get this working? @pquiring

@pquiring
Copy link
Author

pquiring commented Mar 7, 2024

graal 23.1.0 should work now, no need to 'hack' it.

@n1kub
Copy link

n1kub commented Apr 22, 2024

Language is ok, but for me it displays kinda shrinked
изображение

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants