Results 1 to 3 of 3
  1. #1
    Streetproject is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Exclamation 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 );
        }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  3. #3
    Streetproject is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    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.
    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

  1. [Q] Loop issue (while loop)
    By iriscience in forum New To Java
    Replies: 9
    Last Post: 01-31-2011, 04:21 PM
  2. Convert do while loop to for loop
    By sandeeptheviper in forum New To Java
    Replies: 3
    Last Post: 01-03-2011, 12:37 PM
  3. Replies: 5
    Last Post: 04-30-2010, 05:05 PM
  4. while-loop stopping on first loop
    By davester in forum New To Java
    Replies: 6
    Last Post: 06-26-2009, 08:46 PM
  5. Need help with While Loop
    By mrdestroy in forum New To Java
    Replies: 14
    Last Post: 10-20-2008, 02:29 PM

Posting Permissions

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