Results 1 to 3 of 3
- 02-16-2011, 08:01 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 2
- Rep Power
- 0
JTextField loop 2x for-loop WEIRD!
Not sure why this is happening, but I am trying to make a simple loop function that will create some JTextFields for some project I have to do.
I am not sure why, but the very LAST textField is not being added to the stage.
I have tested it. It is definitely added, and It should be there, but for some reason, it doesn't show. I even made a "test" textField and put it right where the other one should be by using "getBounds()" and it gets added to where the textField should be. It is almost as if the "add" function gets completely skipped over.
Anyways, can someone help? I'll add the code below:
Java Code:/** * @author Matt */ import javax.swing.JFrame; import javax.swing.JTextField; public class createTextFields extends JFrame { public createTextFields() { textFieldFunction(); // set properties of application's window setTitle( "Simple button function class" ); // set window title setSize( 850, 500 ); // set window size setVisible( true ); } public void textFieldFunction() { int startx = 50; int starty = 50; int changeInX = 80; int changeInY = 40; int boxW = 80; int boxH = 40; int rows = 2; int cols = 7; JTextField textFields[] = new JTextField[rows*cols]; int i = 0; for(int y=0; y < rows; y++){ for(int x=0; x < cols; x++){ textFields[i] = new JTextField(); textFields[i] .setBounds( startx+(changeInX*x) , starty+(changeInY*y), boxW, boxH ); add( textFields[i] ); System.out.println("Just created the "+i+" JTextField."); i++; //System.out.println("X: "+x+"\nY: "+y+"\n\n"); } } // add these lines to make the last box show.... weird... //JTextField test = new JTextField(); //test.setBounds(textFields[13].getBounds()); //add( test ); } public static void main(String[] args) { createTextFields app = new createTextFields(); app.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } }
-
There's nothing weird going on. You're trying to use absolute positioning when adding components to a container that uses BorderLayout, something that you shouldn't be doing. Try resizing your app, and you'll see ALL JTextFields disappear. A quick and wrong solution would be to set the layout of the JFrame's contentPane to null, which would allow for absolute positioning, but why do this when you can and should take advantage of the great layout managers that are available. I suggest that you put your JTextFields into a JPanel that uses a GridLayout, and then place this JPanel into your contentPane via the assistance of other layouts.
- 02-16-2011, 05:46 PM #3
Member
- Join Date
- Feb 2011
- Posts
- 2
- Rep Power
- 0
Thanks a lot for your response. That fixed the problem. Still not exactly sure why, but yes it worked.
I would prefer to use those, but we are in the INTRO to java class, and the teacher wants absolute positioning. I was just making this quick function for handling all those textfields I would need to make.
Similar Threads
-
[Q] Loop issue (while loop)
By iriscience in forum New To JavaReplies: 9Last Post: 01-31-2011, 04:21 PM -
Convert do while loop to for loop
By sandeeptheviper in forum New To JavaReplies: 3Last Post: 01-03-2011, 12:37 PM -
How can I rewrite the following while loop using a for loop?
By gt11990 in forum New To JavaReplies: 5Last Post: 04-30-2010, 05:05 PM -
while-loop stopping on first loop
By davester in forum New To JavaReplies: 6Last Post: 06-26-2009, 08:46 PM -
Need help with While Loop
By mrdestroy in forum New To JavaReplies: 14Last Post: 10-20-2008, 02:29 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks