File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Game Development and the fly likes Cant get JButton to appear in the game Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Game Development
Bookmark "Cant get JButton to appear in the game" Watch "Cant get JButton to appear in the game" New topic

Cant get JButton to appear in the game

Zane Adickes

Joined: Mar 03, 2012
Posts: 3
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!

Stephan van Hulst

Joined: Sep 20, 2010
Posts: 4642

So, what happened when you remove the setIgnoreRepaint() and setContentAreaFilled() method calls?

Welcome to CodeRanch!

The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
Zane Adickes

Joined: Mar 03, 2012
Posts: 3
I've tried commenting out both and it didn't help :/.

Thanks You!
Devarsh Shah

Joined: Mar 02, 2012
Posts: 6

Try this :

Zane Adickes

Joined: Mar 03, 2012
Posts: 3
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
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
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:

I agree. Here's the link:
subject: Cant get JButton to appear in the game
It's not a secret anymore!