PopupFactory.getSharedInstance().getPopup acting strangely
Hi everyone. I noticed a strange behavior from PopupFactory.getSharedInstance().getPopup, I don't know if it's a bug or I'm just missing something.
Try this:
1. run
2. click the "Click me" button
3. reposition the window or maximize it
The popupPane is detached from the main JPanel. Now do the following:
4. hide the popupPane by clicking the button
5. resize to a fair width (2-300px should do it)
6. click the button
The popupPane fits in the main JPanel visually.
7. reposition the window or maximize it
The popupPane now stays inside the main JPanel.
Is there an explanation for this behavior? Or did I just bump into a bug of Swing's?
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.Popup;
import javax.swing.PopupFactory;
publicclass popupTestextends JPanel{
private Popup popup;
private JPanel popupPane;
private JToggleButton testButton;
publicstaticvoid main(String[] args){
JFrame frame =new JFrame();
frame.setLayout(new BorderLayout());
frame.setContentPane(new popupTest());
frame.pack();
frame.setVisible(true);
}
public popupTest(){
testButton =new JToggleButton("Test me");
popupPane =new JPanel();
popupPane.add(new JLabel("label label label"));
testButton.addActionListener(new ActionListener(){
publicvoid actionPerformed(ActionEvent event){
if (popup !=null){
closePopup();
}else{
popup = PopupFactory.getSharedInstance().getPopup(
popupTest.this, popupPane,
popupTest.this.getLocationOnScreen().x,
popupTest.this.getLocationOnScreen().y + 30);
popup.show();
testButton.setSelected(true);
}
}
});
add(testButton);
}
privatevoid closePopup(){
if (popup !=null){
popup.hide();
popup =null;
testButton.setSelected(false);
}
}
}

