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

Thread: Refreshing Variables Displayed on a JPanel

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Location
    Norfolk, England
    Posts
    7
    My Mood
    Cheerful
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Refreshing Variables Displayed on a JPanel

    I have a small app used to display a score for two players and area to put in scores that should be taken away from the total, for use in a game of darts.

    I have a button that when pressed, initiates a small piece of code to add up the three scores and take them away from the total, however, the total remains at the starting 501 on the JPanel and the scores are not deducted.

    I have printed the value out to make sure that it is working as it should which works fine.

    Any help would be appreciated and just shout if you would like to see some code.

    Many thanks in advance, Rob.


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Refreshing Variables Displayed on a JPanel

    Can you post the code you are dealing with, only the code which you believe to be relevant though.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Junior Member
    Join Date
    Aug 2011
    Location
    Norfolk, England
    Posts
    7
    My Mood
    Cheerful
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Refreshing Variables Displayed on a JPanel

    Okay, here is the if statement after the click takes place on the "Ok" button, this is what I believe to be relevant:

    @Override
    		public void actionPerformed(ActionEvent e) 
    		{
    			// TODO Auto-generated method stub
    			if(e.getSource () == button1)
    			{
    				String Player1T1 = P1T1.getText();
    				String Player1T2 = P1T2.getText();
    				String Player1T3 = P1T3.getText();
     
    				int P1Throw1 = Integer.parseInt(Player1T1);
    				int P1Throw2 = Integer.parseInt(Player1T2);
    				int P1Throw3 = Integer.parseInt(Player1T3);
     
    				P1Total = P1Throw1 + P1Throw2 + P1Throw3;
    				P1S = 501 - P1Total;
    			}

    I am sure that what I am doing is long winded, P1S is the score variable and in the code above this, it is an int set to 501 and then parsed to a string to be displayed on the JPanel.

  4. #4
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Refreshing Variables Displayed on a JPanel

    You need to re-set the JTextField text.
    Check out the API for JTextField, but the following method in particular:
    JTextField
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  5. #5
    Junior Member
    Join Date
    Aug 2011
    Location
    Norfolk, England
    Posts
    7
    My Mood
    Cheerful
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Refreshing Variables Displayed on a JPanel

    Okay, thank you, I will do some more research of my own before re-posting. I think what I am struggling to find out is how to change items on the JPanel once the button has been clicked, I know how to make a Dialogue Box appear with relevant information but not to change the original Panel.

    Thank you for the link. For now, I will set this as solved.

  6. #6
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Refreshing Variables Displayed on a JPanel

    I thought my link was pretty self explanatory to be honest
    To update the text, all you need to do is invoke setText("New Text Goes Here") on your JTextField object, and It should update the text.

    As for dialogue boxes, refer to this tutorial;
    Java Tutorials
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  7. The Following User Says Thank You to newbie For This Useful Post:

    StandbyExplosion (August 24th, 2011)

  8. #7
    Junior Member
    Join Date
    Aug 2011
    Location
    Norfolk, England
    Posts
    7
    My Mood
    Cheerful
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Wink Re: Refreshing Variables Displayed on a JPanel

    Ahh I see, sorry for the misunderstanding
    I will give it a go in the morning :-)
    Many thanks for your help and links!

Similar Threads

  1. [SOLVED] Refreshing a JTabbedPane?
    By Hallowed in forum Java Theory & Questions
    Replies: 2
    Last Post: May 31st, 2011, 01:02 PM
  2. refreshing a jtable with database
    By kollyisrealisaac in forum JDBC & Databases
    Replies: 0
    Last Post: May 7th, 2011, 01:50 PM
  3. Getting a red Oval to displayed on screen
    By warnexus in forum What's Wrong With My Code?
    Replies: 8
    Last Post: January 13th, 2011, 08:27 PM
  4. refreshing JEditorPane
    By nasi in forum AWT / Java Swing
    Replies: 9
    Last Post: April 9th, 2010, 04:01 AM
  5. Certain Chinese Characters not displayed properly.
    By kerwintang in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: August 20th, 2009, 08:23 AM

Tags for this Thread