• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cant get JButton to appear in the game

 
Zane Adickes
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Pie
Posts: 5599
55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, what happened when you remove the setIgnoreRepaint() and setContentAreaFilled() method calls?

Welcome to CodeRanch!
 
Zane Adickes
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've tried commenting out both and it didn't help :/.

Thanks You!
 
Devarsh Shah
Greenhorn
Posts: 6
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this :

 
Zane Adickes
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic