Solved

Setting text to buttons

Posted on 2004-04-25
14
220 Views
Last Modified: 2010-03-31
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);
}
0
Comment
Question by:Drop_of_Rain
  • 7
  • 6
14 Comments
 
LVL 92

Expert Comment

by:objects
ID: 10914348
b.setText((i+1)+" seconds");
0
 
LVL 92

Accepted Solution

by:
objects earned 200 total points
ID: 10914350
or just do it in the ctor:
JButton b = new JButton((i+1)+" seconds");
0
 
LVL 92

Expert Comment

by:objects
ID: 10914351
that way u don't even need the setText() call.
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 86

Expert Comment

by:CEHJ
ID: 10914357
You need

if (i > 0) {
    b.setText("" + (i+1) "second");
}
else {
    b.setText("" + (i+1) "seconds")
}
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 200 total points
ID: 10914359
Sorry - wrong way around:

if (i > 0) {
   b.setText("" + (i+1) "seconds")
}
else {
   b.setText("" + (i+1) "second");
}


0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10914362
...but you need a space before the 's'


  b.setText("" + (i+1) " seconds")
0
 
LVL 92

Expert Comment

by:objects
ID: 10914378
JButton b = new JButton(i==0?"1 second":(i+1)+" seconds");
0
 
LVL 92

Expert Comment

by:objects
ID: 10914381
and no call to setText needed :)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10914412
>>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 ;-)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10914420
...although i admit that

>>"1 second"

is a small advance ;-)
0
 
LVL 92

Expert Comment

by:objects
ID: 10914446
> 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.
0
 

Author Comment

by:Drop_of_Rain
ID: 10914449
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
0
 
LVL 92

Expert Comment

by:objects
ID: 10914461
if you need to start at 2 then use my original comment form as no need to worry about plural
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10917007
:-)
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Java Timer (static) 9 45
What's wrong with this code? 4 23
printf performancy 11 43
Is there a simpler dropbox system? 10 24
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question