This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Different buttons have the same name, what happened?  RSS feed

 
Forman Smith
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Several days ago, i saw the code like below, so i write it myself and find it works.
i want to know, why the five different buttons have the same name?
When the new button bn is created, what happened to the old button bn?
 
Campbell Ritchie
Sheriff
Posts: 55348
157
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a button is an object and an object doesn't have a “name”. What you have is a reference within the loop, but that reference goes out of scope when the loop terminates. So you do not have 5 references with the same name. You have no references at all after the end of the loop. If you display that GUI you will see five buttons. But they have no references pointing to them, so you cannot find them.

Actually you probably can find them by going through the components added to the frame. You might find it easier if you have a button array.
You do not need to write getContentPane(); there were so many complaints about that about twelve years ago they overloaded add() to default to the content pane.
 
Piet Souris
Rancher
Posts: 1943
66
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Campbell

careful!
There are some bartenders & sheriffs around here who take huge
offense if you let OP be exposed to all the dangers modern internet
has to offer, by not talking about EDT and the likes!
They could easilly give you a dead cow or two, if you're really
unlucky.

More to the point: usually you add some ActionListeners to these
buttons, so that you do not need to retain any reference to them.
 
Forman Smith
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Campbell
Thank you! Now I see I was wrong and know what was wrong. Thank you. Usually I use array, but this time I learn something more.
@Piet Souris
Thank you that you taught me a good way to use these buttons.And the bartender ......
 
Brian Cole
Author
Ranch Hand
Posts: 947
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:a button is an object and an object doesn't have a “name”.


I realize that I'm being pedantic here, but technically JButton does have a name property. It inherits getName() and setName() from java.awt.Component, though setting it usually doesn't do much of anything. It's certainly irrelevant to the original question.

Allow me to mention that few (if any) here would approve of this example's calls to fr.setLayout(null), bn.setLocation(x, y+30*i), and bn.setSize(80,20). The "null layout" approach is occasionally appropriate, maybe, but probably not here and IMHO definitely not for newcomers to Swing. Mr. Souris already mentioned EDT issues.

Forman Smith wrote:Usually I use array, but


You could certainly create an array for your buttons, but usually there is no point. If you add ActionListeners to them (optional, but that lets them do things when pushed) and add them to a container, the container takes care of maintaining references to them.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!