どのオブジェクトがフォーカスを受けているかチェックし、それに応じてウィンドウを非表示にします。 focusLost
コールバック内
、isTemporary()
フラグをチェック:
これは粗い例
public class MainFrame extends JFrame {
private JWindow popupWindow;
public MainFrame() throws HeadlessException {
super("Main Frame");
createGUI();
}
private void createGUI() {
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setPreferredSize(new Dimension(600, 400));
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
JTextField hintTextField = new JTextField();
popupWindow = new JWindow(this);
popupWindow.getRootPane().setBorder(BorderFactory.createLineBorder(Color.RED));
popupWindow.getRootPane().setLayout(new BorderLayout());
popupWindow.getRootPane().add(hintTextField, BorderLayout.CENTER);
for(int i = 0; i < 10; i++) {
JTextField textField = new JTextField();
textField.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if(e.getClickCount() == 2) {
Point point = textField.getLocationOnScreen();
popupWindow.setBounds(point.x, point.y + textField.getPreferredSize().height, 200, 200);
popupWindow.setVisible(true);
}
}
});
textField.addFocusListener(new FocusAdapter() {
@Override
public void focusLost(FocusEvent e) {
if(e.getOppositeComponent() != hintTextField) {
popupWindow.setVisible(false);
}
}
});
panel.add(textField);
}
add(panel, BorderLayout.PAGE_START);
pack();
setLocationRelativeTo(null);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new MainFrame().setVisible(true));
}
}
フォーカスを受け取ったオブジェクトを確認し、それに応じてウィンドウを非表示にします。 –