You can for example imagine you were asked to re-invent dropdown menu. The right way to do that seems to make JFrame and display it when certain element is clicked. Such JFrame would be undecorated. I'm doing just this. My program wants to allow user to click on image (which represents something) and select from other images available.
So I made such JFrame:
public class FrameSummonerSpells extends JFrame {
public FrameSummonerSpells() {
settings = set;
// Remove title bar, close buttons...
setUndecorated(true);
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
//Will populate the selection of objects in grid layout
createButtons();
//Will make the frame as large as the content
pack();
//Do not appear by default
setVisible(false);
}
}
And I display it from JButton
:
private void showPopup() {
//Create the popup if needed
if(popup==null)
createPopup();
//Get location of this button
Point location = getLocation();
//Move the popup at the location of this button
//Also move vertically so it appears UNDER the button
popup.setLocation(location.x, location.y+this.getSize().height);
//Show the popup
popup.setVisible(true);
}
As you can see, the position I set isn't set relative to the button or the frame:
It should be noted, however, that the arguments I send to setLocation
are accepted, but just wrongly interpreted.
My question therefore is: How to spawn a new object (JFrame or something else) that can contain JComponent, appear over the window or even extend over the window and keep relative position to the window (or some element)?
This is what I want to display: