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 4 of 4

Thread: Create a JText field upon button click

  1. #1
    Member
    Join Date
    May 2012
    Posts
    36
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Create a JText field upon button click

    I have 2 JText fields where I put in a 1st number in textfield1 and a 2nd number in textfield 2. I then have a button for "Total" What I want to do is when the Total button is pressed, create a 3rd JText field and display the total (number1 + number2) in that field. How would I do this?


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Create a JText field upon button click

    Unless you intend the total to be user editable a JLabel would make more sense than a text field. Either way create all the components when you construct the gui. Then, in the event handler for the button, do the addition and set the text in the label or field.

  3. #3
    Member
    Join Date
    May 2012
    Posts
    36
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Create a JText field upon button click

    I have been trying to use JTextField, but set editable to false and here is the line I run:
    	JLabel areaLabel = new JLabel("Answer Equals: ");
    	JTextField areaField = new JTextField(5);
    	areaField.setEditable(false);

    But I get two compile errors:
    illegal start of type:
    areaField.setEditable(false);

    error: (identifier) expected
    areaField.setEditable(false);
    Last edited by jo15765; May 28th, 2012 at 10:34 AM.

  4. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Create a JText field upon button click

    It looks OK to me.

    Post complete code and the full compiler message (together with an indication of which lines of your code it is referring to). This might involve paring down you code to an example that illustrates just the problem you are having.

    ----

    Just noticed that this is duplicated at Hidden JText Field (was going to posta link because of the similarity...)

    This is very irksome.

Similar Threads

  1. add JTextField with a click of button
    By A4Andy in forum AWT / Java Swing
    Replies: 1
    Last Post: August 31st, 2011, 07:34 AM
  2. Replies: 3
    Last Post: April 11th, 2011, 09:51 PM
  3. [SOLVED] Servlet cant perform sql after second button click
    By Stefan_Lam in forum What's Wrong With My Code?
    Replies: 10
    Last Post: March 31st, 2011, 05:18 AM
  4. [SOLVED] Click button to open JDialog
    By susieferrari in forum Java Theory & Questions
    Replies: 6
    Last Post: March 30th, 2011, 09:24 AM
  5. close JDialog on button click
    By Christophe in forum AWT / Java Swing
    Replies: 4
    Last Post: April 4th, 2010, 11:04 PM