I used this trick for my new tab-completion system. I got the idea from an example I found somewhere on the internet, but I've lost the bookmark now.
// Initialize the TC popup window
tcPopupWindow = new JDialog();
tcPopupWindow.setUndecorated(true);
tcPopupWindow.addWindowFocusListener(new WindowFocusListener() {
public void windowGainedFocus(WindowEvent e) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// Set location relative to chatTextArea
Point location = chatTextArea.getLocation();
SwingUtilities.convertPointToScreen(location, chatTextArea.getParent());
location.translate(0, chatTextArea.getHeight()
+ (chatTextArea.getBorder() == null ? 0
: chatTextArea.getBorder().getBorderInsets(chatTextArea).bottom));
tcPopupWindow.setLocation(location);
}
});
}
public void windowLostFocus(WindowEvent e) {
tcPopupWindow.setVisible(tabComplete = false);
}
});
tcPopupWindow.getContentPane().setLayout(new BorderLayout());
((JComponent)tcPopupWindow.getContentPane()).setBorder(BorderFactory.createEtchedBorder());
tcPopupWindow.getContentPane().add(tcList);
When you want to use the popup window, set its visibility to true and call its requestFocus method. If you try to set its location before it gains focus, it will move when its components are laid out. It will flash on the screen at its original location, but its still pretty zippy.
Start typing a user's name and press tab:
Use the arrow keys to select the name you want, and press enter or tab to select it: