• category: swing folder: AuditoryCues title: AuditoryCuesでイベント音を設定する tags: [UIManager, AuditoryCues, Sound, JOptionPane] author: aterai pubdate: 2008-05-26T17:13:17+09:00 description: UIManagerにAuditoryCues.playListを設定して、ダイアログが開いた時の警告音などを鳴らします。 image: https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTHzQ8XbXI/AAAAAAAAARw/-TGnQ_tvnnM/s800/AuditoryCues.png

概要

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");

参考リンク

コメント