Java: way to completely disable any Swing unwanted beeps?

£可爱£侵袭症+ 提交于 2019-12-29 08:49:20

问题


I am working on a fairly complex Java application using Swing.

On some occasions there are unwanted beeps without any user intervention. No crash, application keeps working fine, I'm respecting the EDT rules etc.

Yet in some cases a beep can be heard: I may be doing something silly triggering that beep but in any case it s not a user action for it can happen upon importing data, when the user is away.

Is it possible, for a Java application that should never ever emit any sound to configure it, say by setting a property for the whole application that says: "dont' ever emit a beep"?

I ve been Googling for that issue and I ve found message by people having the same issue but no answer: all I found was some hack saying that there was a known issue with JEditorPane and that using a putProperty("IgnoreCharsetDirective", Boolean.TRUE) was helpful to make unwanted beeps happen less often. Yet information is very scarce on the subject.

It s a real issue because the application is used in an environment where the sound is needed on the computer, but this Java application emitting noise is unacceptable.


回答1:


Your problem is discussed on the Java Forum:

// Write a custom toolkit
public class MyToolkit extends sun.awt.windows.WToolkit 
{
  public void beep() {
  }
}

// Set this property
System.setProperty("awt.toolkit", "MyPackage.MyToolkit");

NOTE: The use of this workaround is discouraged. You should still try to find the root of the problem.


Edit: Removed a link, since the thread on Java Forum is now offline.




回答2:


In Swing you need to override the LookAndFeel as follows:

UIManager.setLookAndFeel(new NimbusLookAndFeel() {

  @Override
  public void provideErrorFeedback(Component component) {

    // Your beep decision goes here

    // You want error feedback 
    super.provideErrorFeedback(component);

  }
});

Typically your beep decision would reference some kind of external configuration/preferences flag for your application.



来源:https://stackoverflow.com/questions/2266549/java-way-to-completely-disable-any-swing-unwanted-beeps

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!