Swing/InputMap のバックアップの現在との差分(No.10)
TITLE:JDialogをESCキーで閉じる
JDialogをESCキーで閉じる
編集者:Terai Atsuhiro~
作成日:2004-03-15
更新日:2021-09-19 (日) 10:55:00
概要
Escキーを押すと閉じるJDialog
を作成します。Swing (Archive) - JDialog says ESC key is already registered--where?からの引用です。
Screenshot
Advertisement
概要
ESCキーを押すと閉じるJDialogを作成します。 大部分をJDialog says ESC key is already registered--where?から、そのまま引用しています。#screenshot
サンプルコード
#spanend
#spandel
final JDialog dialog = new JDialog(frame, "タイトル", true);
#spanend
#spandel
AbstractAction act = new AbstractAction("了解") {
#spanend
public void actionPerformed(ActionEvent evt) {
#spanadd
* サンプルコード [#sourcecode]
#spanend
#spanadd
#code(link){{
#spanend
#spanadd
JDialog dialog = new JDialog(frame, "title", true);
#spanend
#spanadd
AbstractAction act = new AbstractAction() {
#spanend
@Override public void actionPerformed(ActionEvent e) {
dialog.dispose();
}
};
#spandel
InputMap imap = dialog.getRootPane().getInputMap();
#spanend
#spanadd
InputMap imap = dialog.getRootPane().getInputMap(
#spanend
JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
imap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "close-it");
dialog.getRootPane().getActionMap().put("close-it", act);
- &jnlp;
- &jar;
- &zip;
解説
JOptionPane.showMessageDialog()メソッドで作成したダイアログは、ESCキーを押すと閉じますが、JDialogの場合は閉じてくれません。そこでJDialogでもESCキーを押すと閉じるようにInputMapなどを使って設定します。解説
-
JOptionPane.showMessageDialog()
メソッドなどで作成したダイアログ: Escキーを押すと閉じる - デフォルトの
JDialog
: Escキーを押しても閉じない-
JDialog
でEscキーを押すと閉じるには、JDialog
からInputMap
を取得してAction
を設定する必要がある
-
参考リンク
参考リンク
- Swing (Archive) - JDialog says ESC key is already registered--where?
- Swing (Archive) - ESC Key to Close a JFrame
- JComponentのKeyBinding一覧を取得する
コメント
- fun search test!http://fun-things.info/christmas-cards/wildlife-christmas-cards.htmlhttp://fun-things.info/christmas-gifts/gift-ideas-christmas-boyfriend.htmlhttp://fun-things.info/christmas-cards/snowmobile-christmas-cards.html<a href="http://fun-things.info/christmas-cards/free-musical-animated-christmas-cards.html">free musical animated christmas cards</a>http://fun-things.info/gifts-and-christmas-gift-ideas.html<a href="http://fun-things.info/christmas-cards/boxed-christmas-greeting-cards.html">boxed christmas greeting cards</a>http://fun-things.info/christmas-gifts/personalized-pillow-christmas-gifts.htmlhttp://fun-things.info/christmas-gifts/siberian-husky-christmas-gifts.htmlhttp://fun-things.info/christmas-gifts/top-christmas-gifts-for-her.html<a href="http://fun-things.info/christmas-cards/vintage-christmas-cards.html">vintage christmas cards</a><a href="http://fun-things.info/christmas-gifts/christmas-gift-for-men2c-75-years-old.html">christmas gift for men2c 75 years old</a><a href="http://fun-things.info/christmas-gifts/christmas-song-lyric-gift-for-mom.html">christmas song lyric gift for mom</a>http://fun-things.info/christmas-cards/christain-christmas-cards-for-business.htmlhttp://fun-things.info/christmas-cards/christmas-card-holders.htmlhttp://fun-things.info/christmas-gifts/free-craft-ideas-for-christmas-gifts.html -- deckbox?
コメント
- メモ: Javaの道>掲示板(JDialogが最小化されてしまう)。
JDK 1.7.0
だと、JDialog
などのTitleBar
を右クリックすると、「Java Result: -1073741819
」で落ちる?- かなり前から再現しないのに消し忘れていた。
Nvidia
のドライバのせい? Bug ID: JDK-6458746 OGL: mustang b94 doesn't work correctly with JDialog (Nvidia, 91.31, Windows) -- aterai
- かなり前から再現しないのに消し忘れていた。