Win a copy of Classic Computer Science Problems in Swift this week in the iOS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

create an array of JSpinner object names  RSS feed

 
Ranch Hand
Posts: 35
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following code in Netbeans takes the data from 7 UI JSpinner objects (Z1-Z7) and loads it into an array (integer array named Cl.Zones).  I am very new to Java and suspect there is an easier way to do this.  Can I somehow create an array of JSpinner object names (Z1-Z7) and iterate through the array instead of this sequential method?

 
Saloon Keeper
Posts: 4101
48
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If 'Z' were an array of JSpinner objects.
 
Mike Gauthier
Ranch Hand
Posts: 35
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that is exactly the type of thing I am looking for. Now I just need to figure out how to make an array of JSpinner objects.

Will let you know how that comes out.
 
Marshal
Posts: 58829
179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Picky Biker wrote:. . . how to make an array of JSpinner objects. . . .

That's easy enough. For seven objects you can probably write an array initialiser. I would consider a Stream, but that might be too advanced for you.
 
Mike Gauthier
Ranch Hand
Posts: 35
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's what I did and it seems to work fine.  Thank You!

 
Mike Gauthier
Ranch Hand
Posts: 35
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With a little playing around, it was made even shorter and I think, more readable.

 
Carey Brown
Saloon Keeper
Posts: 4101
48
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the trick would be not to have created Z1 (etc) in the first place, but to create Z[0].
 
Mike Gauthier
Ranch Hand
Posts: 35
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:I think the trick would be not to have created Z1 (etc) in the first place, but to create Z[0].



I do not understand this response.

This program will control a Sprinkler system. Z1-Z7 are the 7 sprinkler zones. They are JSpinner elements that hold the number of minutes for each zone.

Since I am brand new at Java, a further explanation would be most helpful.

Thanks
 
Carey Brown
Saloon Keeper
Posts: 4101
48
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Presumably you have some line of code that says
Instead you could have
 
Mike Gauthier
Ranch Hand
Posts: 35
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:Presumably you have some line of code that says
Instead you could have



Okay, now I understand what you are suggesting.  That would work very well, however, in the Netbeans IDE that I am working with, these JSpinners were all coded for me in the UI designer. This is what they look like in the generated code so I don't think this will work in this particular case. The code that is generated by the IDE is not freely edited. But I can certainly use that method in future work when I am not dealing with generated code.


 
Campbell Ritchie
Marshal
Posts: 58829
179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are those spinners marked public or static? Both look wrong
 
Mike Gauthier
Ranch Hand
Posts: 35
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Why are those spinners marked public or static? Both look wrong



This program is organized into 3 separate files. There are about 35 UI elements and all of them are organized into one file. Another file contains most of the functions that access the UI elements to read/write their values. Whenever I tried to access an element from the function file I had to make the element public and then I got errors saying they I couldn't access them because they were not static. 

I would dearly love to not use public and static because it requires me to override the defaults on all of them.

All three files are in the same folder, the same package , and they all have this statement at the top: package Sprinkler;

So my question would be how can I access the UI elements from external files without using public or static?

Remember, I am brand new at java so let's try to keep it relatively simple.

Thanks
 
Campbell Ritchie
Marshal
Posts: 58829
179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mike Gauthier wrote:. . . Whenever I tried to access an element from the function file I had to make the element public and then I got errors saying they I couldn't access them because they were not static.  . . .

That makes me think they were declared in the wrong place. I think they should be declared in the same class as all the other GUI components. If you can get away with declaring them as local variables in the place where you create the GUI (setUpGUI() method or constructor), so much the better .
 
Mike Gauthier
Ranch Hand
Posts: 35
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:That makes me think they were declared in the wrong place. I think they should be declared in the same class as all the other GUI components. If you can get away with declaring them as local variables in the place where you create the GUI (setUpGUI() method or constructor), so much the better .


I'm guessing that means the functions that actually access the UI elements should be in the same class and file as the elements themselves. Then functions that actually use the UI data can be in another file.  Is that correct?

You can probably tell I am having trouble understanding how the program should be structured. I can easily do all this much simpler if I had a single monolithic file, but I am trying to learn how to organize files so each is a reasonable size and has a logical grouping. The Java books I have read don't spend a lot of time on organizing Java programs/files/classes/data.

Would this be a useful structure?

File 1:  All the UI elements and the functions that access them go here (instantiate the data classes here)
File 2:  All the functions that use the UI data (These access the instantiated data inFile 1)
File 3:  Data classes and structures

If there is a good reference for splitting up Java programs and organizing the programs/classes/files/data I would like to see it.
 
Campbell Ritchie
Marshal
Posts: 58829
179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't talk about functions; in Java® we talk about objects because Java® is an object language. I would say those objects which constitute the display go in the same place. Try this sort of thing:-See, all the objects which make up the display are from references declared in the setUpGUI method, except the name. The name is only visible if you enlarge the display.
 
Mike Gauthier
Ranch Hand
Posts: 35
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mike Gauthier wrote:

Campbell Ritchie wrote:Why are those spinners marked public or static? Both look wrong



Here is what they look like after following the advice.  

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!