Solved

Setting text to buttons

Posted on 2004-04-25
14
239 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

691 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