Results 1 to 4 of 4
  1. #1
    SnakeDoc is offline Senior Member
    Join Date
    Apr 2012
    Posts
    129
    Rep Power
    0

    Question 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:

    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);
    	}
    }
    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));
    		
                                // 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);
    	}
    }
    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.

    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!

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: calling variables in a loop?

    pane.add(tf[i])
    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.

    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.

  3. #3
    SnakeDoc is offline Senior Member
    Join Date
    Apr 2012
    Posts
    129
    Rep Power
    0

    Default 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.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: calling variables in a loop?

    You're welcome. Post back if you get stuck.

Similar Threads

  1. Calling Variables in Multiple Methods
    By PrimalScientist in forum New To Java
    Replies: 10
    Last Post: 02-07-2012, 10:26 AM
  2. Calling Variables
    By Soulpole in forum New To Java
    Replies: 7
    Last Post: 01-28-2012, 08:26 PM
  3. Using variables in a loop
    By louiedogg418 in forum New To Java
    Replies: 11
    Last Post: 06-28-2011, 03:52 AM
  4. creating variables in a for loop?
    By sehudson in forum New To Java
    Replies: 7
    Last Post: 03-01-2011, 03:37 AM
  5. Trouble with For loop and variables in a program
    By dablyz in forum New To Java
    Replies: 12
    Last Post: 05-06-2008, 04:25 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •