Newbie: help with Glass Pane, paintComponent
Hi,
I have a little game that should display a message at the position of the player if a certain condition becomes true. First I thought of using a tooltip, however this is not exactly the thing I would like to have... I would like to display an icon, a message text as well as a button.
Now I read the Sun tutorial about the Glass Pane. There is the example "GlassPaneDemo " but it's too complicated for my Swing knowledge.
How can I simply create a Glass Pane and do some painting respectively placing text and a button (Layout Manager like JPanel?)?
Thanks a lot!
[599 byte] By [
SFLa] at [2007-11-26 22:20:19]

# 1
perhaps all you need is a Popup()
# 2
Can I place an icon (PNG) and a button into a popup?
SFLa at 2007-7-10 11:17:12 >

# 3
You can use PopupFactory to create your own popup. The component of the popup will be a JPanel with buttons, texts and images in it
ProZa at 2007-7-10 11:17:12 >

# 4
> You can use PopupFactory to create your own popup.
> The component of the popup will be a JPanel with
> buttons, texts and images in it
Are there any good and simple examples available? I couldn't find one in the Sun Java tutorial... I always get an infinite loop when using PopupFactory...
SFLa at 2007-7-10 11:17:12 >

# 5
hi sfl,
i hope that helps you.
JPanel bla = new JPanel();
bla.add(new JLabel("Peter und Hans backen Brot"));
bla.add(new JButton("Klick"));
PopupFactory pf = new PopupFactory();
Popup pu = pf.getPopup(
this,
bla,
20,
20);
pu.show();
Message was edited by:
marcel_hecker
# 6
Thanks for this example! Is it correct just to place the JPanel "blah" at certain coordinates or do I have to work with a Glass Pane?Thanks a lot! :)
SFLa at 2007-7-10 11:17:12 >

# 7
You can place the Popup on every coordinate on the screen without using the glass pane(in the sample: x:20 and y:20). This can be an advantage or an disadvantage. Popups can shown out of range, such as: Popup on (0,0) and Frame on (200,200). So you must find out the correct coordinates, keep them actual in relation to your Frame and you must close the popup, such as with an button or after an defined time.
# 8
> You can place the Popup on every coordinate on the> screen without using the glass paneThanks again :) Hmmm... but isn't the glass pane intended for this? How would I have to rewrite the code above to use the glass pane?
SFLa at 2007-7-10 11:17:12 >

# 9
Hi SFL,
it is easier to use the popups. The GlassPane is an Layer over your container (jpanel, jframe, jlabel etc.). if you want to use them you must memorize the "downer" component and then you can make your changes.
in the small example i exchange the labels of the popuppanels. if you want this nice demoeffects you need to overwrite some paint-methods. the next disadvantage is the actionhandling on the glasspane, you must rebuild all things that the layer under your glasspane can. thats why the sun example looks difficult.
greets
JPanel bla = new JPanel();
JLabel peter = new JLabel("Peter und Hans backen Brot");
bla.add(peter);
bla.add(new JButton("Klick"));
PopupFactory pf = new PopupFactory();
Popup pu = pf.getPopup(
this,
bla,
20,
20);
pu.show();
JPanel glass = bla;
glass.remove(peter);
glass.add(new JLabel("vienna calling"));
bla.getRootPane().setGlassPane(glass);
bla.getRootPane().getGlassPane().setVisible(true);
# 10
Ah, very good :)
I just have one more question: I would like to show a popup if a certain condition becomes true. I have written a method "showPopup":
private void showPopup() {
showingPopup = true;
popupPanel = new JPanel();
popupLabel = new JLabel("Message",);
popupPanel.add(popupLabel);
popup = popupFactory.getPopup(this, popupPanel, (int) mouseCoordinate.getX(), (int) mouseCoordinate.getY());
popup.show();
}
Now I am testing whether the condition is true... and I get an infinite loop :(
if (isColliding()) {
showPopup();
System.out.println(count);
}
else {
if (showingPopup) hidePopup();
}
How can I avoid this infinite loop?
SFLa at 2007-7-10 11:17:12 >

# 11
You have a method isColliding which is presumably checking a colliding flag. However nowhere in the code you presented are you resetting that flag. Perhaps that is your problem.
# 12
> You have a method isColliding which is
> presumably checking a colliding flag. However
> nowhere in the code you presented are you resetting
> that flag. Perhaps that is your problem.
I corrected this, setting isColliding to false after the check, however, the infinite loop remains. If I remove the "showPopup();" call the infinite loop is gone - the int variable "count" is just printed out one time.
Did I make a mistake when initialising the Popup?
SFLa at 2007-7-10 11:17:12 >

# 13
can you post the rest of the code pls?
# 14
The code is too long to be posted here. Think I give it up using popups; the loop was caused by my mouse dragging method, I fixed it. However, when I hide the popup a grey rectangle remains, even when calling repaint().
SFLa at 2007-7-10 11:17:12 >

# 15
Sounds like you need to run the popup code in the [url http://java.sun.com/docs/books/tutorial/uiswing/concurrency/dispatch.html]Event Dispatch Thread[/url]. See attached sample code.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
showPopup();
}
});