Swing/DeleteKeyBeep のバックアップの現在との差分(No.7)
- バックアップ一覧
- 差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/DeleteKeyBeep へ行く。
- 1 (2012-10-01 (月) 14:42:41)
- 2 (2012-10-02 (火) 14:48:03)
- 3 (2012-12-06 (木) 12:18:43)
- 4 (2012-12-07 (金) 16:43:35)
- 5 (2013-07-26 (金) 01:42:46)
- 6 (2014-02-06 (木) 20:31:52)
- 7 (2014-09-17 (水) 13:12:31)
- 8 (2014-10-22 (水) 00:56:08)
- 9 (2015-11-15 (日) 02:59:32)
- 10 (2017-04-04 (火) 14:17:08)
- 11 (2017-04-28 (金) 16:49:39)
- 12 (2018-04-18 (水) 16:23:40)
- 13 (2020-04-10 (金) 21:01:42)
- 14 (2021-05-12 (水) 07:33:45)
- 追加された行はこの色です。
- 削除された行はこの色です。
TITLE:JTextFieldでのBeep音の設定を変更する #tags(JTextField, Beep, ActionMap, DocumentFilter) #author(aterai) #pubdate(2012-10-01T14:42:41+09:00) * 概要 [#y6fbffa9] --- category: swing folder: DeleteKeyBeep title: JTextFieldでのBeep音の設定を変更する tags: [JTextField, Sound, ActionMap, DocumentFilter] author: aterai pubdate: 2012-10-01T14:42:41+09:00 description: JTextFieldなどで、KBD{Delete}、KBD{Back Space}キーを押した時に鳴らすBeep音の設定を変更します。 image: https://lh6.googleusercontent.com/-zIUmkF2C9FA/UGkqdcWDLVI/AAAAAAAABTk/F4nun0GDLZc/s800/DeleteKeyBeep.png --- * 概要 [#summary] `JTextField`などで、KBD{Delete}、KBD{Back Space}キーを押した時に鳴らす`Beep`音の設定を変更します。 #download(https://lh6.googleusercontent.com/-zIUmkF2C9FA/UGkqdcWDLVI/AAAAAAAABTk/F4nun0GDLZc/s800/DeleteKeyBeep.png) * サンプルコード [#j457740c] * サンプルコード [#sourcecode] #code(link){{ String key = DefaultEditorKit.deletePrevCharAction; //"delete-previous"; final Action deletePreviousAction = am.get(key); String key = DefaultEditorKit.deletePrevCharAction; // delete-previous Action deletePreviousAction = am.get(key); am.put(key, new TextAction(key) { //@see javax/swing/text/DefaultEditorKit.java DeletePrevCharAction // @see javax/swing/text/DefaultEditorKit.java DeletePrevCharAction @Override public void actionPerformed(ActionEvent e) { JTextComponent target = getTextComponent(e); if(target != null && target.isEditable()) { if (target != null && target.isEditable()) { Caret caret = target.getCaret(); int dot = caret.getDot(); int mark = caret.getMark(); if(dot==0 && mark==0) { if (dot == 0 && mark == 0) { return; } } deletePreviousAction.actionPerformed(e); } }); key = DefaultEditorKit.deleteNextCharAction; //"delete-next"; final Action deleteNextAction = am.get(key); key = DefaultEditorKit.deleteNextCharAction; // delete-next Action deleteNextAction = am.get(key); am.put(key, new TextAction(key) { //@see javax/swing/text/DefaultEditorKit.java DeleteNextCharAction // @see javax/swing/text/DefaultEditorKit.java DeleteNextCharAction @Override public void actionPerformed(ActionEvent e) { JTextComponent target = getTextComponent(e); if(target != null && target.isEditable()) { if (target != null && target.isEditable()) { Document doc = target.getDocument(); Caret caret = target.getCaret(); int dot = caret.getDot(); int mark = caret.getMark(); if(dot==mark && doc.getLength()==dot) { if (dot == mark && doc.getLength() == dot) { return; } } deleteNextAction.actionPerformed(e); } }); }} * 解説 [#j7ac5e77] 上記のサンプルでは、以下の二点を変更して、`Beep`音の設定を変更しています。 * 解説 [#explanation] 上記のサンプルでは、`JTextField`の`Beep`音の設定を以下`2`点変更して動作テストを行っています。 - `TextAction(DefaultEditorKit.deleteNextCharAction)#actionPerformed(ActionEvent)`などをオーバーライドして、KBD{Delete}キーやKBD{Back Space}キーで文字の削除がなくても、`Beep`音を鳴らさないように変更したアクションを`ActionMap`に設定 - 5文字以上入力できないように制限し、超える場合は`Beep`音を鳴らす`DocumentFilter`を作成して、`AbstractDocument#setDocumentFilter(DocumentFilter)`で設定 - `TextAction(DefaultEditorKit.deleteNextCharAction)#actionPerformed(ActionEvent)`メソッドなどをオーバーライドしてKBD{Delete}キーやKBD{Back Space}キーで文字の削除がなくても`Beep`音を鳴らさないように変更したアクションを`ActionMap`に設定 - `5`文字以上入力できないように制限し、超える場合は`Beep`音を鳴らす`DocumentFilter`を作成して`AbstractDocument#setDocumentFilter(DocumentFilter)`で設定 * 参考リンク [#sd8b7a8e] - [http://docs.oracle.com/javase/tutorial/displayCode.html?code=http://docs.oracle.com/javase/tutorial/uiswing/examples/components/TextComponentDemoProject/src/components/DocumentSizeFilter.java DocumentSizeFilter.java] -- via: [http://docs.oracle.com/javase/tutorial/uiswing/components/generaltext.html Text Component Features (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)] * 参考リンク [#reference] - [https://docs.oracle.com/javase/tutorial/displayCode.html?code=https://docs.oracle.com/javase/tutorial/uiswing/examples/components/TextComponentDemoProject/src/components/DocumentSizeFilter.java DocumentSizeFilter.java] -- via: [https://docs.oracle.com/javase/tutorial/uiswing/components/generaltext.html Text Component Features (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)] * コメント [#bb0d1397] * コメント [#comment] #comment #comment