Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By StormyWaters

Thread: Building a simple gui using arrays and loops.

  1. #1
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default Building a simple gui using arrays and loops.

    Ok, so I'm a little stumped here. So I'm trying to build a simple gui to give me some really basic controls over my program that I'm working on, and I would like to build it using arrays and loops so avoid a lot of repeat code, such as 25 someVariable.setColumns(x); etc. However when I put it in the array nothing is drawn into my JFrame and I can't figure out why. As soon as I remove the loops and arrays from the drawing and go back to my crude code of 25x someVariable.setColumns(x) it works as expected.

    I even made sure to print the variables going in and then coming out at the end of the loop to make sure everything was going in and coming out as expected, and they are but still nothing is being drawn. Here is a SSCCE of my code focusing on my issue. Where am I going wrong? I would assume that this is possible as it doesn't seem like that complex of a concept but everything I seem to have tried is resulting in the same output.

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    
    public class JavaSSCCE
    {
    	//Define Variables
    	public JLabel settingLabels[] = new JLabel[8];
    	public JTextField settingFields[] = new JTextField[8];
    	public String[] settingList = new String[9];
    	JFrame settingFrame;
    	JButton settingSaveBtn;
    	JPanel settingPanelCenter, settingPanelSouth;
    	
    	public static void main(String args[])
    	{
    		JavaSSCCE gui = new JavaSSCCE();
    		gui.go();
    	}
    	
    	public void go()
    	{
    		//Build Settings GUI
    		settingFrame = new JFrame("Settings");
    		settingFrame.setSize(600,400);
    		settingFrame.setResizable(false);
    		
    		//Build Labels
    		String[] labelText = {"Username: ", "Password: ","Stat Priority(%): Str:",
    				"Def: ", "Spd: ", "Server Url:", "Time: Start: ", "End: "};
    		for (int i=0; i<8; i++)
    		{
    			System.out.println(labelText[i]);
    			settingLabels[i] = new JLabel(labelText[i]);
    			System.out.println(settingLabels[i].getText());
    			System.out.println("");
    		}
    		
    		//Build Text Fields
    		int[] columns = {25,25,40,4,4,2,2,2,2};
    		for (int i=0; i<8; i++)
    		{
    			System.out.println(columns[i]);
    			settingFields[i] = new JTextField();
    			//settingFields[i].setText(settingList[i]); Different section of code, pulls info from a config file
    			settingFields[i].setColumns(columns[i]);
    			System.out.println(settingFields[i].getColumns());
    			System.out.println("");
    		}
    		
    		//Build Button
    		settingSaveBtn = new JButton("Save");
    		Dimension btnDim = new Dimension(100,25);
    		settingSaveBtn.setPreferredSize(btnDim);
    		settingSaveBtn.setMargin(new Insets(1,1,1,1));
    		//settingSaveBtn.addActionListener(new settingSaveListener());
    		
    		//Build Center Panel
    		settingPanelCenter = new JPanel();
    		for (int i=0; i>9; i++)
    		{
    			settingPanelCenter.add(settingLabels[i]);
    			settingPanelCenter.add(settingFields[i]);
    		}
    		
    		//Build South Panel
    		settingPanelSouth = new JPanel();
    		settingPanelSouth.add(settingSaveBtn);
    		
    		//Add Panels to the frame
    		settingFrame.getContentPane().add(BorderLayout.CENTER, settingPanelCenter);
    		settingFrame.getContentPane().add(BorderLayout.SOUTH, settingPanelSouth);
    		
    		//Make it visible
    		settingFrame.setVisible(true);
    	}
    }
    Thanks in advance,
    -Dark
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Building a simple gui using arrays and loops.

    Where are any of your fields or labels being added to a panel on the frame?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default Re: Building a simple gui using arrays and loops.

    Java Code:
            //Build Center Panel
            settingPanelCenter = new JPanel();
            for (int i=0; i>9; i++)
            {
                settingPanelCenter.add(settingLabels[i]);
                settingPanelCenter.add(settingFields[i]);
            }
    
            //Add Panels to the frame
            settingFrame.getContentPane().add(BorderLayout.CENTER, settingPanelCenter);
    That is my code for adding the labels and text fields is it not? If I'm not missing something simple here then I believe that loop should take all of my defined labels and fields and add them to the panel in the loop. Taking the first label, adding it, then taking the first text field, adding it and then looping to the next.

    Am I mistaken here?
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Building a simple gui using arrays and loops.

    Ah, I didn't spot that.
    You don't seem to have any layout manager for that panel.
    And I think you should do a frame.pack() call (or has that changed?) before the frame.setVisible() call.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default Re: Building a simple gui using arrays and loops.

    Well here is where I am confused as to why its not working. My loops don't add anything to the frame but if I manually set each variable, which is incredibly tedious, it displays properly. This isn't supposed to be anywhere near my final gui layout, I was just trying to get a very simple control panel up to work with the rest of my code.

    Example:
    Java Code:
    settingLabels[0] = new JLabel("Username: ");
    settingPanelCenter.add(settingLabels[0]);
    settingFrame.getContentPane().add(BorderLayout.CENTER, settingPanelCenter);
    That example will add the "Username: " string to the label and it will display properly. I will take a look at frame.pack() but I've never had to use it before when just drawing up a simple gui like this.


    EDIT: Ok after doing some real quick research and experimentation I don't believe frame.pack() is required as it sets all the components to their preferred size, which I am not defining here yet. This just makes the window the size of the button, which does display. However frame.pack() did not do anything to get the rest of the components to display.
    Last edited by Dark; 02-04-2013 at 06:30 PM.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  6. #6
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default Re: Building a simple gui using arrays and loops.

    If your loops aren't doing anything, then maybe there is something wrong with your loops? That for loop you posted in Comment#3 has a problem...
    Tolls likes this.

  7. #7
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default Re: Building a simple gui using arrays and loops.

    Holy @%$& derp moment. Wow, thanks Stormy. Just flipping the comparing symbol gave me what I needed.

    I guess I made a typo and because it was one character just made it that much easier for me to pass over and over in my frustration.

    +Rep
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

Similar Threads

  1. Loops and arrays
    By Katniss in forum New To Java
    Replies: 1
    Last Post: 10-19-2012, 05:21 PM
  2. Arrays and for-loops??
    By esined93 in forum New To Java
    Replies: 2
    Last Post: 10-17-2012, 03:24 PM
  3. Java Arrays & Loops
    By AdamG in forum New To Java
    Replies: 2
    Last Post: 10-14-2011, 05:14 PM
  4. How to use arrays and loops?
    By asadzarrar in forum New To Java
    Replies: 1
    Last Post: 10-31-2010, 11:04 PM
  5. A few questions about arrays and loops
    By Jamison5213 in forum New To Java
    Replies: 1
    Last Post: 12-22-2009, 05:59 AM

Tags for this Thread

Posting Permissions

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