Carey Brown wrote:I think the trick would be not to have created Z1 (etc) in the first place, but to create Z.
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.
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 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.
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 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. . . .
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.