JWindow内にフォーカス可能なコンポーネントを配置する
Total: 2318, Today: 1, Yesterday: 1
Posted by aterai at
Last-modified:
Summary
JWindowや装飾なしのJFrame、JPopupMenuなどにフォーカス可能なコンポーネントを配置するテストを実行します。
Screenshot

Advertisement
Source Code Examples
JButton button4 = new JButton("JWindow(owner)");
button4.addActionListener(e -> {
JButton b = (JButton) e.getSource();
resetEditor(editor, b);
Point p = b.getLocation();
p.y += b.getHeight();
SwingUtilities.convertPointToScreen(p, b.getParent());
Window window = new JWindow(SwingUtilities.getWindowAncestor(b));
window.setFocusableWindowState(true);
window.setModalExclusionType(Dialog.ModalExclusionType.APPLICATION_EXCLUDE);
// window.setAlwaysOnTop(true);
window.add(editor);
window.pack();
window.setLocation(p);
window.setVisible(true);
editor.requestFocusInWindow();
});
View in GitHub: Java, KotlinDescription
JPopupMenuJPopupMenu内にフォーカス可能なコンポーネントとしてJTextAreaを配置- 親
JFrameのタイトルバーなどをクリックすると自動的にJPopupMenuが非表示になる JPopupMenu内に配置した子コンポーネントのJTextAreaからJPopupMenuが開けない- 親
JFrame内にポップアップするLightWeightPopupでJPopupMenu#pack()を実行すると子のJTextAreaからフォーカスが移動してしまうのでJTextArea#requestFocusInWindow()で再設定する必要がある - 親
JFrame外にポップアップするHeavyWeightPopupでJPopupMenu#pack()を実行すると一瞬親JFrameタイトルバーの描画などが乱れる
button1.addActionListener(e -> {
JButton b = (JButton) e.getSource();
resetEditor(editor, b);
JPopupMenu popup = new JPopupMenu();
popup.setBorder(BorderFactory.createEmptyBorder());
popup.add(editor);
popup.pack();
Point p = b.getLocation();
p.y += b.getHeight();
popup.show(this, p.x, p.y);
editor.requestFocusInWindow();
});
JFrame#setUndecorated(true)JFrame#setUndecorated(true)でタイトルバーなどの装飾を非表示にしたJFrameにフォーカス可能なコンポーネントとしてJTextAreaを配置- 親
JFrameがアクティブWindowでなくなるためグローバルフォーカスが外れて親JFrameのタイトルバーの描画などが変化する JTextArea外をクリックして編集終了と合わせてJFrameを閉じる場合、親JFrameにMouseListenerやComponentListenerを追加する必要がある- 親
JFrameのタイトルバーをクリックしてもそのイベントを取得する方法がない?ため、その動作でJTextAreaを閉じることができない
- 親
JButton button2 = new JButton("JFrame#setUndecorated(true)");
button2.addActionListener(e -> {
JButton b = (JButton) e.getSource();
resetEditor(editor, b);
JFrame window = new JFrame();
window.setUndecorated(true);
// window.setAlwaysOnTop(true);
window.setModalExclusionType(Dialog.ModalExclusionType.APPLICATION_EXCLUDE);
window.add(editor);
window.pack();
Point p = b.getLocation();
p.y += b.getHeight();
SwingUtilities.convertPointToScreen(p, b.getParent());
window.setLocation(p);
window.setVisible(true);
editor.requestFocusInWindow();
});
JWindow()JWindowにフォーカス可能なコンポーネントとしてJTextAreaを配置- 親
JFrameを指定していないので非表示のJFrameが親フレームになり、JWindowに配置したコンポーネントがフォーカスを取得できない JWindow#setFocusableWindowState(true)を指定しても効果がない?- 非表示の
JFrameが別途表示された後(このサンプルではbutton2やbutton4が実行された後)ならマウスでJTextAreaの文字列を選択することなどが可能になる
JButton button3 = new JButton("JWindow()");
button3.addActionListener(e -> {
JButton b = (JButton) e.getSource();
resetEditor(editor, b);
Window window = new JWindow();
window.setFocusableWindowState(true);
// window.setAlwaysOnTop(true);
window.setModalExclusionType(Dialog.ModalExclusionType.APPLICATION_EXCLUDE);
window.add(editor);
window.pack();
Point p = b.getLocation();
p.y += b.getHeight();
SwingUtilities.convertPointToScreen(p, b.getParent());
window.setLocation(p);
window.setVisible(true);
editor.requestFocusInWindow();
});
JWindow(owner)- 表示中の親
JFrameを所有フレームにしてJWindowを作成し、これにフォーカス可能なコンポーネントとしてJTextAreaを配置 - 親
JFrameがアクティブWindowのままになるのでそのタイトルバーの描画などは変化しない JTextArea外をクリックして編集終了と合わせてJWindowを閉じる場合、親JFrameにMouseListenerやComponentListenerを追加する必要がある- 装飾なしの
JFrameと同様に親JFrameのタイトルバーをクリックしてもそのイベントを取得する方法がない?ため、その動作でJTextAreaを閉じることができない
- 装飾なしの
JWindow#setAlwaysOnTop(true)を設定していると別アプリケーションのウィンドウが重なってもその手前にセルエディタが表示されることになるので、代わりにWindow#setModalExclusionType(Dialog.ModalExclusionType.APPLICATION_EXCLUDE)を設定して回避
- 表示中の親
Reference
- How to Use the Focus Subsystem (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)
- JTextAreaとJFrameで幅固定、文字列の折り返し、親枠外まで高さ拡大可能なセルエディタを作成する