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);

}

}

}

[4032 byte] By [bronze-starDukes] at [2007-11-26 12:13:08]
# 1

Swing components, being light weight, must be painted entirely within the bounds of its parent frame.

In the first case, the popup doesn't fit into the bounds of the parent so I believe a JWindow is used to represent the popup. A JWindow is a component that can exist on its own and it doesn't have a parent so once its location is set is just remains there.

In the second case the popup does fit in the bounds of its parent, so I believe that some other Swing component, which can't exist on its own is used to represent the popup. Now when the parent move the popup component moves with it.

platinumsta at 2007-7-7 14:13:57 > top of Java-index,Archived Forums,Socket Programming...