Author Topic: [Java] Borderless, tooltip-style popup windows  (Read 3492 times)

0 Members and 1 Guest are viewing this topic.

Offline Camel

  • Hero Member
  • *****
  • Posts: 1703
    • View Profile
    • BNU Bot
[Java] Borderless, tooltip-style popup windows
« on: November 25, 2007, 04:12:22 pm »
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.

Code: [Select]
// 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:

<Camel> i said what what
<Blaze> in the butt
<Camel> you want to do it in my butt?
<Blaze> in my butt
<Camel> let's do it in the butt
<Blaze> Okay!