Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

arrays and for  RSS feed

 
Roger Wells
Ranch Hand
Posts: 51
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following code which I want to make into a for loop. I want to us i as a index . I can't seem to find out how to make text fields and text labels to use the index i.



 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In what way do you want to use i with the labels and text fields?  What are you trying to accomplish?
 
Roger Wells
Ranch Hand
Posts: 51
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want change the code so that I only have one of this section of code and can add as many text and label fields that are found in the array.

The i would have to index pageAddress and pageLabel so that my add would add unique labels and fields.

 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
add would add unique labels and fields. 

The same names can be used for the references to components that are added to a container.  The container will save the references locally.
There is no need for there to be unique names like: pageLabel0 and pageAddress0, etc

The 4 lines of code you posted should work inside of a loop for creating any number of JLabels and JTextFields that are added to a container.
 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seems pretty simple. there are several ways it might be done, depending on requirements that have not been specified.
For example, iIf you do not need further references to things like pageLabel1 and pageAddress1, here's one way:
 
Campbell Ritchie
Marshal
Posts: 56534
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where does the magic number 4 come from? How you you know that 4 will be the correct length?
 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just copying the OP's use of 4 items. OI course it should probably be fields.length(), assuming that is what is desired.
 
Roger Wells
Ranch Hand
Posts: 51
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well at this time I am trying to figure out 2 more things.

1. How do I get this magical number? This code will generate a variable number of fields. So at this point I don't know
2. How do I click the button and fetch the value that has been entered into each field?


here is the complete code.  the output is pretty ugly at this time but hopefully I will learn how to make it pretty.

 
Roger Wells
Ranch Hand
Posts: 51
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I am at the next step of the learning curve. There will be many more I am sure.

My next step is how to get the button to work. I would like to press the button and have it concat a string of all the data that the user enters and display it into some type of text box.

This is the current state of the program. Remember that I am a greenhorn and this is the first Java program other than some other training tutorials that I have written. The user interface looks horrid and the logic seems to be slow as it takes about 3-4 seconds to display the form. But I will eventually get there.

I appreciate all of the help and suggestions I have gotten.

Thanks Roger

 
Campbell Ritchie
Marshal
Posts: 56534
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roger Wells wrote:. . . How do I get this magical number? This code will generate a variable number of fields. So at this point I don't know . . .
In that case you will have to assume the magic number is a mistake and use something else. Beware of magic numbers, particularly in arrays. It is only a matter of time until you get one wrong and either get an out of bounds exception because the magic number was too big, or get a null exception because the magic number was too small and you have not filled the array.
Where are you getting the array from? I can see one array containing arrays of Strings, and it looks as if you were planning to create a second array containing arrays of buttons, from it. I don't like that idiom for filling arrays, when you can use an array initialiser instead. You can even use initialisers to initialise arrays of arrays:-
Added as edit: if you us initialisers you might not have all the rows the same length, but you will not exceed the length of each row, nor leave any vacant spaces for nulls. Make sure always to use the forms of loop below to iterate those arrays.You usually have to write new JButton[][] in an initialiser, but you can omit that part if you declare and initialise the array on the same like. That sounds like a time‑consuming process, Maybe you can put the words into a text file and populate the array from that. You can probably do that with Streams, but I haven't the time to work that out at present.
You can iterate the arrays and call methods on the individual buttons:-Note that format of action listener will only work in Java8. If you use a conventional for loop, you will have indices and you can use those indices as locations.That assumes you have a constraints object which takes indices. Look for Cay Horstmann's GBG class for an example. If you don't use grid bag, you can use a different technique for adding. Maybe you can use grid layout and then you won't need indices.

Make sure your business logic is in different classes. Don't write a GUI until your business logic is working completely independently.
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A couple of other minor things about your code:
  • You ignore an Exception.  Bad idea.  At least print a stacktace.
  • You create a variable in main() that you don't need.
  • Rather than putting all your code in the constructor, I'd like to see it in a method.
  •  
    Roger Wells
    Ranch Hand
    Posts: 51
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Well I have finally gotten the programmer doing what I started out wanting it to do. It's messy and needs a lot of cleaning up. The UI is really horrid but I have not got to that part of my training yet.

    Feel free to make suggestions. I plan to put some of the code in their own class but I will wait until to I do the UI part. Some parts of the program I don't even know why it's there remember that I am still a greenhorn....

    One comment was where did the magic number 4 now 7 come from. That is the maxium number of parameters for each template variable. If this number is bad then the data entry program for the template is bad as it will check to make sure it is always the same.

    My next part of the training I hope will allow me to get the data from the different textFields and string them together to make the complete citation.

    Thanks to each of you who have commented it has been a real help. I am sure I will be back..


     
    Carey Brown
    Saloon Keeper
    Posts: 3311
    46
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    -- OR --
     
    Knute Snortum
    Sheriff
    Posts: 4276
    127
    Chrome Eclipse IDE Java Postgres Database VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I would write the while loop at line 47 like this:
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!