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

Thread: JTextfield

  1. #1
    Member
    Join Date
    Feb 2012
    Posts
    39
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default JTextfield

    Hi just a quick question here.

    im doing an electronic bandit assignment and when you win the payout is added to a JTextfield called winnings.

    i also have a Jbutton that allows you to transfer the winnings to the balance so you can carry on playing.

    The problem im having is that once i have transfered the winnings to the balance, the JTextfield that stores the winnings amount still shows the amount of winnings i previously had instead of reseting back to 0 and starting again.

    How do i go about this?


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: JTextfield

    Call the JTextField.setText() method and send it "0".
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. The Following User Says Thank You to aussiemcgr For This Useful Post:

    usherlad (August 22nd, 2012)

  4. #3
    Member
    Join Date
    Feb 2012
    Posts
    39
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: JTextfield

    sorry for being a pain but i dont have a clue what you mean. This is the code i have so far for that bit

     if (event.getSource()==btnTransfer){
    			balance = balance + winnings;
    			txtBalance.setText(balance +"");
    			JOptionPane.showMessageDialog(null,"You have transferred  " +winnings+"p");
    		}
     
    		if (event.getSource()==btnTransfer){
    			winnings =0;
    		}
     
    		if (event.getSource()==btnTransfer){
    			winnings =0;
    			btnTransfer.setEnabled(false);
    			JOptionPane.showMessageDialog(null,"You now have no winnings to transfer!!!");

  5. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: JTextfield

    Where is the code where you change what the winnings JTextField shows? This is what you must use. Let's presume you call this variable winningsTextField, what aussie is recommending is that you simply call winningsTextField.setText("0"). That's it.

    However a better way to do something like this is to separate all concerns into the model (the logical part of your program, the part concerned with winnings, accounts, and such), the view (the GUI part of your program that is concerned with displaying information and components that allow user interactions) and the control (the part of the program that responds to user interactions with GUI controls and which allows the view and the model to interact.

    If done this way, you could have the model have a winnings variable and allow other classes to listen for changes in winnings value by use of listeners such as a PropertyChangeListener. If done right, any change to the value held by the winnings numeric variable will automatically be reflected in the GUI display of the winnings. It's a pretty neat trick actually.
    Last edited by curmudgeon; August 22nd, 2012 at 11:25 AM.

  6. The Following User Says Thank You to curmudgeon For This Useful Post:

    usherlad (August 22nd, 2012)

  7. #5
    Member
    Join Date
    Feb 2012
    Posts
    39
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: JTextfield

    ahhh thank you ever so much. For some reason is putting a + before and it kept adding a 0. Thats why i was getting confused.

    Again thanks alot

Similar Threads

  1. JTextField
    By Karthik Prabhu in forum AWT / Java Swing
    Replies: 7
    Last Post: June 20th, 2012, 10:54 AM
  2. [SOLVED] how to put changeable value in JTextField?
    By scorpas in forum Java Applets
    Replies: 34
    Last Post: May 21st, 2012, 05:24 PM
  3. Need help int JTextField
    By n00b in forum What's Wrong With My Code?
    Replies: 7
    Last Post: November 1st, 2011, 09:57 AM
  4. Jtextfield Validation
    By nimishalex in forum AWT / Java Swing
    Replies: 8
    Last Post: December 11th, 2010, 02:42 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