Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: JTextField clearing problem

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default JTextField clearing problem

    Hi all,

    I am having a little problem with clearing JTextFields from my GUI application. The problem is I created a load of JTextFields in an array (shown below)

    int[] txtfieldwidths = { 20, 20, 20, 20 }
    for ( int i = 0; i < txtLbls.length; i++ )
    		{
    			gridbagcons.gridwidth = GridBagConstraints.RELATIVE ;
    			labelNames = new JLabel( txtLbls[i] );
    			formPanel.add( labelNames, gridbagcons );
    			gridbagcons.gridwidth = GridBagConstraints.REMAINDER ;
    			labelTxtFields = new JTextField( txtfieldwidths[i] );
    			formPanel.add( labelTxtFields, gbc );
    		}

    I also have a button with an actionListener attached to it which is supposed to clear the JTextFields (shown below)

     
    if ( e.getSource( ) == btnClear )
    		{
     
    			labelTxtFields.setText("");
     
    		}

    The problem is when I go and click on the clear button, only the last JTextField is being erased and none of the others above it.

    Is there a way I can get all the JTextFields to clear?

    Hope you can help me!


  2. #2
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,235
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: JTextField clearing problem

    Your reference to labelTxtFields is set the last instantiated JTextField in the loop, so clearing that alone will only clear the last JTextField. To clear them all, you could keep an array holding ALL the JTextFields you created, and loop through that array clearing each.

Similar Threads

  1. repaint panel without clearing it
    By enflation in forum Java Theory & Questions
    Replies: 5
    Last Post: June 27th, 2010, 04:00 PM
  2. need help Printing output from a JTextfield
    By juanbond311 in forum Java Theory & Questions
    Replies: 27
    Last Post: June 21st, 2010, 08:26 AM
  3. Get a certain line in a JTextField
    By FlamingDrake in forum Java Theory & Questions
    Replies: 2
    Last Post: May 14th, 2010, 03:21 PM
  4. Issue with JTextField Locking
    By PekinSOFT.Systems in forum AWT / Java Swing
    Replies: 0
    Last Post: October 1st, 2009, 11:12 AM
  5. [SOLVED] JTextField not visible in swing
    By Sterzerkmode in forum AWT / Java Swing
    Replies: 4
    Last Post: May 21st, 2009, 07:37 AM