Results 1 to 4 of 4
Thread: calling variables in a loop?
- 06-14-2012, 04:47 AM #1
Senior Member
- Join Date
- Apr 2012
- Posts
- 127
- Rep Power
- 0
calling variables in a loop?
Hello!
I'm attempting to shorten my code and call variables inside a loop. I declare the variables at the begginging of my code, but then have to add many lines to use them each time. I would like to use a simple loop to cycle through them since all variables are being used the same way and I know exactly how many there will be / are in my program.
For example:
I would like to reduce this to something like:Java Code:public class FolderWatcher { static JTextField tf1; static JTextField tf2; static JTextField tf3; static JTextField tf4; static JTextField tf5; static JTextField tf6; static JTextField tf7; static JTextField tf8; static JTextField tf9; static JTextField tf10; static JTextField tf11; static JTextField tf12; public static void makeGUI() { jF.setTitle("FolderWatcher"); jF.setSize(300,200); tf1 = new JTextField(10); tf2 = new JTextField(10); tf3 = new JTextField(10); tf4 = new JTextField(10); tf5 = new JTextField(10); tf6 = new JTextField(10); tf7 = new JTextField(10); tf8 = new JTextField(10); tf9 = new JTextField(10); tf10 = new JTextField(10); tf11 = new JTextField(10); tf12 = new JTextField(10); tf1.setEditable(false); tf2.setEditable(false); tf3.setEditable(false); tf4.setEditable(false); tf5.setEditable(false); tf6.setEditable(false); tf7.setEditable(false); tf8.setEditable(false); tf9.setEditable(false); tf10.setEditable(false); tf11.setEditable(false); tf12.setEditable(false); pane.setLayout(new GridLayout(6, 2)); pane.add(tf1); pane.add(tf2); pane.add(tf3); pane.add(tf4); pane.add(tf5); pane.add(tf6); pane.add(tf7); pane.add(tf8); pane.add(tf9); pane.add(tf10); pane.add(tf11); pane.add(tf12); jF.setDefaultCloseOperation(jF.EXIT_ON_CLOSE); jF.setVisible(true); } }
however i get a compile error. Yes i know this is not complete code, I removed a lot of it so its easier to read in this post.Java Code:public class FolderWatcher { static JTextField tf1; static JTextField tf2; static JTextField tf3; static JTextField tf4; static JTextField tf5; static JTextField tf6; static JTextField tf7; static JTextField tf8; static JTextField tf9; static JTextField tf10; static JTextField tf11; static JTextField tf12; public static void makeGUI() { jF.setTitle("FolderWatcher"); jF.setSize(300,200); tf1 = new JTextField(10); tf2 = new JTextField(10); tf3 = new JTextField(10); tf4 = new JTextField(10); tf5 = new JTextField(10); tf6 = new JTextField(10); tf7 = new JTextField(10); tf8 = new JTextField(10); tf9 = new JTextField(10); tf10 = new JTextField(10); tf11 = new JTextField(10); tf12 = new JTextField(10); tf1.setEditable(false); tf2.setEditable(false); tf3.setEditable(false); tf4.setEditable(false); tf5.setEditable(false); tf6.setEditable(false); tf7.setEditable(false); tf8.setEditable(false); tf9.setEditable(false); tf10.setEditable(false); tf11.setEditable(false); tf12.setEditable(false); pane.setLayout(new GridLayout(6, 2)); // this being the shortened section ----------------------------------- for (int i = 1; i <= 12; i++) { pane.add(tf[i]); } jF.setDefaultCloseOperation(jF.EXIT_ON_CLOSE); jF.setVisible(true); } }
I have seen some things suggested online with relection, however I'm pretty new and don't understand quite in which direction to go in.
Please help point me in the right direction. Thanks guys!
- 06-14-2012, 07:12 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: calling variables in a loop?
Variables in Java are a compile time thing which don't "survive" through to the compiled, running code. So there is no equivalent of PHP's ${tf.$i} where a loop might allow you to deal (at runtime) with each of the variables in turn.pane.add(tf[i])
Instead use an array of JTextField. A for loop can then be used to (a) Create the text fields, (b) Make them uneditable and (c) add them to the container. There's nothing special about using an array here: any sort of collection would do.
-----
Generally speaking you should avoid making things static.
- 06-14-2012, 04:41 PM #3
Senior Member
- Join Date
- Apr 2012
- Posts
- 127
- Rep Power
- 0
Re: calling variables in a loop?
I should have thought of that i guess lol. Thanks Pbrockway2, I'll give that a shot tonight when I get home.
- 06-14-2012, 10:54 PM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Similar Threads
-
Calling Variables in Multiple Methods
By PrimalScientist in forum New To JavaReplies: 10Last Post: 02-07-2012, 10:26 AM -
Calling Variables
By Soulpole in forum New To JavaReplies: 7Last Post: 01-28-2012, 08:26 PM -
Using variables in a loop
By louiedogg418 in forum New To JavaReplies: 11Last Post: 06-28-2011, 03:52 AM -
creating variables in a for loop?
By sehudson in forum New To JavaReplies: 7Last Post: 03-01-2011, 03:37 AM -
Trouble with For loop and variables in a program
By dablyz in forum New To JavaReplies: 12Last Post: 05-06-2008, 04:25 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks