Forums Register Login

Cant get JButton to appear in the game

+Pie Number of slices to send: Send
Im working on a turn based strategy game. When a unit is selected on the field I need its available actions to be displayed. Im trying to use JButtons to display the actions so if you were to click the "Move" button you would be enabled to select a destination for that unit. The code I have currently refuses to display and JButtons. I hope you guys can help me out.

Here are a few points that might help you help me:
In the Games constructor it creates a JFrame and JPanel
it sets the JPanel equal to the frames content pane.
The game creates a level.
In the levels "setUp" method it is creating a JButton and passing it to the Games "addComponent" method where it is adding the component to the content pane. (this in itself seems like it may be a little messy to me).

The code compiles and runs fine but I get no JButton.

Thank You!


+Pie Number of slices to send: Send
So, what happened when you remove the setIgnoreRepaint() and setContentAreaFilled() method calls?

Welcome to CodeRanch!
+Pie Number of slices to send: Send
I've tried commenting out both and it didn't help :/.

Thanks You!
+Pie Number of slices to send: Send
Try this :

+Pie Number of slices to send: Send
Ok. Thanks, Devarsh. I'll give this a try first thing after I get off work. I'm wondering though, the content pane is "repainted" ever time render() is run. Would this mean I need to repaint the button every time through the loop as well because that I have tried a few different ways but still with no results
+Pie Number of slices to send: Send
so many things just plain wrong

1) *always* start a swing GUI in the Swing thread (EDT), via SwingUtilities.invokeLater(...)
2) *never* *ever*, have Thread.sleep() in the same thread where repainting or event listening occurs,
separate thread or (usually) better to use a Swing Timer.
3) not sure if you're using 'extends Canvas', but don't use it in Swing, use a JPanel and its paintComponent()

that should do for starters, but to answer your initial question/problem, in Swing, any time you add/remove
components from a visible container, this is the sequence:


Won't you be my neighbor? - Fred Rogers. tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1151 times.
Similar Threads
Turn Based Strategy Games
JFrame
JButton on multiple instances of applet
Is this a good approach?
A problem about full-screen exclusive mode
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 05:03:14.