概要

UIManagerAuditoryCues.playListを設定して、ダイアログが開いた時の警告音などを鳴らします。

サンプルコード

Object[] optionPaneAuditoryCues = {
  "OptionPane.errorSound", "OptionPane.informationSound",
  "OptionPane.questionSound", "OptionPane.warningSound"
};
// UIManager.put("AuditoryCues.playList", UIManager.get("AuditoryCues.allAuditoryCues"));
// UIManager.put("AuditoryCues.playList", UIManager.get("AuditoryCues.defaultCueList"));
// UIManager.put("AuditoryCues.playList", UIManager.get("AuditoryCues.noAuditoryCues"));
UIManager.put("AuditoryCues.playList", optionPaneAuditoryCues);
View in GitHub: Java, Kotlin

解説

上記のサンプルでは、デフォルトではすべて再生しないように設定されている聴覚フィードバックを、JOptionPaneでダイアログを開いた場合のみ有効になるように変更しています。

  • showMessageDialog1
    • LookAndFeelデフォルトの音が鳴る(LookAndFeelにデフォルトの音が無い場合は鳴らない)
    • WindowsLookAndFeelでは、「コントロールパネル」「サウンドとオーディオデバイスのプロパティ」で、プログラムイベントが設定されている場合は音が鳴る
  • showMessageDialog2
    • 別途用意したwavファイルを再生する
    • UIManager.put("AuditoryCues.playList", UIManager.get("AuditoryCues.noAuditoryCues"))として、二重に鳴らないように制限

  • MetalLookAndFeelMotifLookAndFeelでは以下のようにMessageDialogのイベント音の変更が可能
    UIManager.put("OptionPane.informationSound", "/example/notice2.wav");
    

参考リンク

コメント