File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
A friendly place for programming greenhorns!
Big Moose Saloon
Register / Login
JButton Syntax Question
Joined: Feb 16, 2011
Apr 04, 2011 07:18:46
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.
import javax.swing.*; private static int array = new int ; JButton Button2 = new JButton(array); add(Button2);
When compiling I get the error:
cannot find symbol
symbol : constructor
If I substitute
JButton Button2 = new JButton("Button 2"); add(Button2);
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?
Joined: Oct 25, 2008
Apr 04, 2011 07:26:51
You're trying to call the constructor with an int as parameter. That constructor doesn't exist. You want to call it with a
. 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.
Joined: Feb 16, 2011
Apr 04, 2011 07:40:10
int a = array;
String s = "";
s = Integer.toString(a);
Works Great now.
Joined: Oct 13, 2005
Apr 04, 2011 08:57:59
Lonnie Wood wrote:
int a = array; String s = ""; s = Integer.toString(a);
. . ..
The initial assignment to "" is unnecessary. It's either
int a = array; String s = Integer.toString(a);
You might do well to look at the String#valueOf method, too.
With a little knowledge, a
cast iron skillet
is non-stick and lasts a lifetime.
subject: JButton Syntax Question
How to refresh the JButton text among other things.
What is the best way to create GUI components- in-line, methods, inner classes or separate classes
cannot get stuff to repaint - please help
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter
| Powered by
Copyright © 1998-2015