Drop_of_Rain
asked on
Setting text to buttons
I need to set the text to the 1st button (1 second), and the 2nd-10th buttons 2 seconds - 10 seconds. I can see (seconds 1) but need some help to get the text the other way. Also with (("" + n, Color.green, Color.black);)
for (int i=0; i<10; i++)
{
JButton b = new JButton();
b.setPreferredSize( new Dimension( 40, 20 ) );
b.setText("seconds"+(i+1);
buttons.put("b"+(i+1), b);
b.addActionListener(this);
JButton jcoloredbutton = new JColoredButton("" + n, Color.green, Color.black);
gridPanel.add(b);
}
for (int i=0; i<10; i++)
{
JButton b = new JButton();
b.setPreferredSize( new Dimension( 40, 20 ) );
b.setText("seconds"+(i+1);
buttons.put("b"+(i+1), b);
b.addActionListener(this);
JButton jcoloredbutton = new JColoredButton("" + n, Color.green, Color.black);
gridPanel.add(b);
}
b.setText((i+1)+" seconds");
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
that way u don't even need the setText() call.
You need
if (i > 0) {
b.setText("" + (i+1) "second");
}
else {
b.setText("" + (i+1) "seconds")
}
if (i > 0) {
b.setText("" + (i+1) "second");
}
else {
b.setText("" + (i+1) "seconds")
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
...but you need a space before the 's'
b.setText("" + (i+1) " seconds")
b.setText("" + (i+1) " seconds")
JButton b = new JButton(i==0?"1 second":(i+1)+" seconds");
and no call to setText needed :)
>>JButton b = new JButton(i==0?"1 second":(i+1)+" seconds");
That's really the same code logic as i posted, but in a less readable form
>>and no call to setText needed :)
setText will get called whether you call it explicitly or not ;-)
That's really the same code logic as i posted, but in a less readable form
>>and no call to setText needed :)
setText will get called whether you call it explicitly or not ;-)
...although i admit that
>>"1 second"
is a small advance ;-)
>>"1 second"
is a small advance ;-)
> That's really the same code logic as i posted, but in a less readable form
Thats good considering its answering the same q :P
The readability is questionable though.
Thats good considering its answering the same q :P
The readability is questionable though.
ASKER
is a small advance ;-) you are right I really need to start with 2 seconds up to 12 but I thought I could work that out later.
You both have given me the answer to my question. I will use both ways in different classes. I have raised the points because of that.
Thanks for all the support,
Christopher
You both have given me the answer to my question. I will use both ways in different classes. I have raised the points because of that.
Thanks for all the support,
Christopher
if you need to start at 2 then use my original comment form as no need to worry about plural
:-)