File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes JButton Syntax Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "JButton Syntax Question" Watch "JButton Syntax Question" New topic

JButton Syntax Question

Lonnie Wood

Joined: Feb 16, 2011
Posts: 15


I'm trying to build a panel that has nine buttons. Each button will need to show the value of an item in an array. So I built my array and tried to set the value of Button2 with the following code.

When compiling I get the error:
cannot find symbol
symbol : constructor JButton(int)
location: class javax.swing.JButton

If I substitute

It adds the button with the text Button 2.

Is there a different syntax that I should be using to equate the text of the button to the value of the array?

I haven't started building the Listeners and ActionPerformed yet, do I need that before this will work?

Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

You're trying to call the constructor with an int as parameter. That constructor doesn't exist. You want to call it with a String. So the solution is to create a String from that int.

"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Lonnie Wood

Joined: Feb 16, 2011
Posts: 15

int a = array[1][2];
String s = "";
s = Integer.toString(a);

Works Great now.

Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46353
Lonnie Wood wrote: . . ..
The initial assignment to "" is unnecessary. It's eitherorYou might do well to look at the String#valueOf method, too.
I agree. Here's the link:
subject: JButton Syntax Question
It's not a secret anymore!