Solved

Setting text to buttons

Posted on 2004-04-25
14
197 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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 learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

706 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now