Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Setting text to buttons

Posted on 2004-04-25
14
Medium Priority
?
244 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 800 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
Independent Software Vendors: 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!

 
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 800 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

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…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
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 …
Suggested Courses

618 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