• Post Reply Bookmark Topic Watch Topic
  • New Topic

Assigning different names to array elements  RSS feed

 
Declan Barrett
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm working on a program that will consist of several JLabels and JComboBoxes.

I know it's possible to create these using arrays but is it possible to assign different names to each array element e.g. Jlabel[0] to be known as hours, JLabel[1] to be known as minutes and JLabel[2] to be known as seconds so that I could use them as hours.setBounds, minutes.setBounds etc?
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Creating an array for 3 instances sounds like an overkill. If you can let us know what you are trying to do here, we can offer some tips.
e.g. One of the possible solutions would be to create a composite custom widget which will internally encapsulate a JComboxBox with it's associated JLabel
Please TellTheDetails
 
Declan Barrett
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rather than repeating all the lines for creating a JLabel or JComboBox (e.g. new JLabel, setLayout, setBounds etc) I was hoping to create them using a For loop and an array, and to be able to assign a different name to each element of the array?
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you can. No you shouldn'tYou now have two references to the same object, which I think is called an alias. That is potentially confusing; you can do conflicting things with labels[0] and hoursLabel. You can even forget which is which and do something with labels[1] thinking that is the same as hoursLabel.
Why do you want to set bounds on a label in the first place? you should use a layout manager, which works out all the sizes for you.
Why do you want those labels in an array at all? Do you want to apply the same action to all of them? If so, you can hide the aliases from yourself where the cannot do any harm with a ComponentGroup class
 
Declan Barrett
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The requirement is to create the labels etc without using a layout manager. It's part of a project I've been given having just finished a beginners Java course. I could just as easily use the array index throughout the program but I was curious to know if it was possible to assign names.

Thanks for all the help.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Declan Barrett wrote:Rather than repeating all the lines for creating a JLabel or JComboBox . . .
A factory method?

Of course you can create similar Components in an array, but then you would have three (almost) identical components. If you were writing code for a board game, where you require nine‑plus or sixty‑four squares, then an array is probably a good way to create them. You can have the squares as buttons and clicking each button is regarded as a move to that square.
 
Declan Barrett
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


My hope was to create the labels above as an array in a for loop which I think would be more elegant than regurgitating the code like above. Then I wondered if it was 'best practice' to do it as an array and if it was possible to assign unique meaningful identifiers to each element other than the array index.

 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Declan Barrett wrote:The requirement is to create the labels etc without using a layout manager.

Are you sure this is what the requirement is? If yes, then the person who told you this does not know how to properly use Swing
 
Declan Barrett
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes it definitely is the requirement. We were shown how to use layout managers on the course but the instructor wants us to use the setBounds method etc for the project - He never said why - maybe he's evil :-)
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Declan Barrett wrote:My hope was to create the labels above as an array in a for loop which I think would be more elegant than regurgitating the code like above.

OK, forget about "naming" your Labels just for a moment. How would you re-write that code above to use a for loop and an array?

Then I wondered if it was 'best practice' to do it as an array

As others have said: no. At least not the way you're thinking.

and if it was possible to assign unique meaningful identifiers to each element other than the array index.

Absolutely. And If you look again at that snippet of code you wrote, I think you might realise that you've already got one.

Winston
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Declan

Looking at your code, what is common to the 3 line blocks for creating a JLabel?
 
Declan Barrett
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Absolutely. And If you look again at that snippet of code you wrote, I think you might realise that you've already got one.

Winston


That'll be the text assigned to the label then !!!
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Boswell wrote: . . . Looking at your code, what is common to the 3 line blocks for creating a JLabel?
setLayout(null)??

I see what you mean about knowing how to use Swing properly.
 
Declan Barrett
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here's how I'm looking at doing it (work in progress):
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Declan Barrett wrote: . . . That'll be the text assigned to the label then !!!
No, “restaurantLabel”.
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell

I was thinking more along the lines of pulling the logic out to avoid the repetition.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Declan Barrett wrote:That'll be the text assigned to the label then !!!

Bingo. So, presuming you have an array of them, it should be simple matter to write a search that finds the one that you want, no?

However, you still have the problem of creating that array because all your Labels have different text and bounds. If those bounds are related, then I suspect that your "work in progress" is along the right lines.
Alternatively, you could just do it by brute force:and now you have an array of labels to work with.

Sometimes its best not to overthink a problem until you know there's a need.

HIH

Winston
 
Declan Barrett
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cheers Winston and Thanks everyone for all the help
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are we setting a layout on those labels at all? You set the layout on the panel they are in, not the labels themselves.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Why are we setting a layout on those labels at all? You set the layout on the panel they are in, not the labels themselves.

Have to admit, I wondered about that too; but not being a GUI expert I accepted Declan's code at face value .

I believe he also said specifically that he wasn't allowed to use a layout.

Winston
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It took me a little time to realised the layout or non‑layout was being set in the wrong place, too.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!