Results 1 to 7 of 7
- 02-04-2013, 05:54 PM #1
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.
Thanks in advance,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); } }
-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.
- 02-04-2013, 06:08 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
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.
- 02-04-2013, 06:13 PM #3
Re: Building a simple gui using arrays and loops.
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.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);
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.
- 02-04-2013, 06:22 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
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.
- 02-04-2013, 06:27 PM #5
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:
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.Java Code:settingLabels[0] = new JLabel("Username: "); settingPanelCenter.add(settingLabels[0]); settingFrame.getContentPane().add(BorderLayout.CENTER, settingPanelCenter);
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.
- 02-04-2013, 06:38 PM #6
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
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...
- 02-04-2013, 06:46 PM #7
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
-
Loops and arrays
By Katniss in forum New To JavaReplies: 1Last Post: 10-19-2012, 05:21 PM -
Arrays and for-loops??
By esined93 in forum New To JavaReplies: 2Last Post: 10-17-2012, 03:24 PM -
Java Arrays & Loops
By AdamG in forum New To JavaReplies: 2Last Post: 10-14-2011, 05:14 PM -
How to use arrays and loops?
By asadzarrar in forum New To JavaReplies: 1Last Post: 10-31-2010, 11:04 PM -
A few questions about arrays and loops
By Jamison5213 in forum New To JavaReplies: 1Last Post: 12-22-2009, 05:59 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks