?
Solved

Setting text to buttons

Posted on 2004-04-25
14
Medium Priority
?
241 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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month12 days, 19 hours left to enroll

777 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