Advertisement Jump to content
Sign in to follow this  

[java] Java Buttons

This topic is 4578 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all, I am working on a small game in Java and would like to know how I can add a quick and dirty button into my app. Right now I have a class that sets up the main window and I am using a Canvas to draw everything on. My question is can I use the JButton class to add buttons and how do I go about doing this? Thanks for any pointers.

Share this post

Link to post
Share on other sites
You must create, then add a JButton to a Container using that Container's add method. Since a Canvas is not a Container, you cannot add a JButton directly to a Canvas. You can, however, add a JButton to the Container which contains your Canvas. It would look something like this.

JFrame f = new JFrame("Game");
Container c = f.getContentPane(); //this grabs the default Container used by the JFrame, so that we can add stuff to it
JButton b = new JButton("Click me!");
Canvas canvas = new Canvas();
c.add(canvas); //here we are adding stuff to the Container
c.add(b); //you add buttons the same way

Share this post

Link to post
Share on other sites
Ok, I tried that and it sorta worked, except I got an exception and my window was only my button. Here is my code that initializes everything:

public MainWindow()
super( "Game" );
// Exit the program when we close the window for god's sake!
setDefaultCloseOperation( EXIT_ON_CLOSE );
// We don't want to repaint when the OS says we need to
setIgnoreRepaint( true );

GraphicsEnvironment ge =
GraphicsDevice gd = ge.getDefaultScreenDevice();

// Setup the Canvas
renderCanvas = new Canvas()
public Dimension getPreferredSize()
return new Dimension( 1024, 768 );

add( "Center", renderCanvas );
btn = new JButton( "CLICK!" );
btn.setLocation( 10, 10 );
add( btn );

// Put the window in the center of the screen
Point cp = ge.getCenterPoint();
cp.translate( -getWidth()/2, -getHeight()/2 );
setLocation( cp.x, cp.y );

setResizable( false );
setVisible( true );
renderCanvas.createBufferStrategy( 2 );

The exception I get is: Exception in thread "main" java.lang.IllegalArgumentException: Width (0) and height (0) cannot be <= 0

Thanks for any help, I can't seem to see what this is talking about.

Share this post

Link to post
Share on other sites

Which line does your exception occur on?

Anyways, a shot in the dark:

If you are not using a layout manager, you should use setBounds to setup the buttons size and location. This sets up absolute positioning. On another note, it is not always trivial to add lightweight components (JButton) in combination with heavy weight components (Canvas). If you get layout problems you cannot explain, try replacing your Canvas with a JPanel (example), and see if it solves the problem. (I know you will have to comment out things like BufferStrategy, - I only mean this as a means for you to test your component combination works).
You can also try substituting your JButton with a awt Button.

Good luck ;)

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!