Results 1 to 5 of 5
  1. #1
    seniorstatus is offline Member
    Join Date
    Apr 2012
    Posts
    16
    Rep Power
    0

    Default Passing dynamic variables during runtime

    I have a program that has a counter in one class, which is incremented as a user clicks on a button. Then i have a label in another class and I'm attempting to pass the counter variable to the new class during runtime so that it updates as the user clicks the button. as of right now, the variable is passed, but it is static. it doesnt change when the user clicks the button. any suggestions?

    the class that has the variable to be called:
    Java Code:
    public class Fetch extends Stage
    
    //some other code
    //....
    
    
    public String toString() {
          String temp;
    
          if (myInstruction.flush == true) {
               temp = "FLUSHED: \n" + myInstruction + "\n";
               return temp;
          }
          if ((PC >= 0) && (PC < instructionSize) )
            temp = Integer.toString(PC) + ":\n" + myInstruction + "\n";
          else
            temp = myInstruction + "\n";
          return temp;
       }


    the class that is trying to get "temp" during runtime
    Java Code:
    public class DPanel extends JPanel
    {
         private JLabel PCCount;
         public Fetch FetchCall = new Fetch();
         DPanel()
         {
            String PCString = FetchCall.toString();
            PCCount = new JLabel();
            PCCount.setText(PCString);
         }
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,700
    Rep Power
    8

    Default Re: Passing dynamic variables during runtime

    Whether or not a variable is static does not affect whether it can change. And you change the text of a JLabel is by calling the setText() method.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    seniorstatus is offline Member
    Join Date
    Apr 2012
    Posts
    16
    Rep Power
    0

    Default Re: Passing dynamic variables during runtime

    hey Kevin, thanks for the response. i used the setText method to change the text by getting the return value "temp" from "public String toString()", temp changes during runtime. however, when i use the setText method, the JLabel doesnt update with the new value, it keeps the value that is stored when the program is first run. any ides?

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,700
    Rep Power
    8

    Default Re: Passing dynamic variables during runtime

    You have to call setText() whenever you want the text to change. Calling it once and then changing the variable you passed into the method will have no effect.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Passing dynamic variables during runtime

    Follow coding conventions. Variable names should start with a lowercase letter.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Passing variables from one class to another
    By nhmllr in forum New To Java
    Replies: 11
    Last Post: 08-09-2011, 12:43 AM
  2. passing arument to runtime.getRuntime() method
    By cool in forum AWT / Swing
    Replies: 1
    Last Post: 11-12-2010, 06:15 AM
  3. Passing dynamic variables between classes
    By Brekk in forum New To Java
    Replies: 1
    Last Post: 03-22-2010, 06:07 AM
  4. passing dynamic data
    By abhiN in forum Web Frameworks
    Replies: 0
    Last Post: 01-17-2008, 01:16 PM
  5. how can I get the names of variables in runtime?
    By tommy in forum New To Java
    Replies: 1
    Last Post: 07-27-2007, 10:37 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
  •