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.
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
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: