Link to home
Start Free TrialLog in
Avatar of jpking72
jpking72

asked on

passing variables between classes

I am writing a pretest that is 2 pages. afterthe first page the user picks his answers and hits the "save" button and then the 2nd page comes up, he picks his answers, and then he hits "submit" to finish.

how do i save the answers from class of the first page and get them to the class on the second page so I can check them when submit is pressed?

public class page1 extends javax.swing.JFrame

if(JRadiobutton1.isSelected())
        answer[1] = 1;

etc.

answer[1] = 3
answer[2]= 4

public class page2 extends javax.swing.JFrame

answer[3] = 4
answer[4] = 1

answer[1] and answer[2] need to be passed to class page2 somehow so i can check the answers



Avatar of hoomanv
hoomanv
Flag of Canada image

you dont need to pass answer array to second page
store all the answers in your current application frame
but instead change the frame that is shown each time to the person
use a CardLayout to change pretest's pages
I mean dont create a new frame for each page
have them all in one frame but switch them using CardLayout or just JTabbedPane
Avatar of jpking72
jpking72

ASKER

isn't there a way to just pass variables from class to class?
ASKER CERTIFIED SOLUTION
Avatar of hoomanv
hoomanv
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> isn't there a way to just pass variables from class to class?

Well, you can maintain the answers at a global level in some static collection.

public class AnswersList // make it a singleton
{
  private static AnswersList instance ;
  private Hashtable answers ;
  private AnswersList ()
  {
    answers = new Hashtable () ;
  }
  public Hashtable getAnswers ()
  {
    return answers ;
  }
  // give a static synchronized getInstance ()
}

You can use this hash-table in all the frames for storing answers (values) along with question-numbers (keys) at a global level.
all you have to do is put it in the constructor:

UserProfile up = new UserProfile(level,start);

================================

public class UserProfile {
   
    /** Creates a new instance of UserProfile */
    private String level;
    private int start;
    public UserProfile(String a,int b) {
            level = a;
            start = b;
    }
    public String getLevel(){
        return level;
    }
    public int getFuntion(){
        return start;
    }
}
? What is UserProfile?
UserProfile is supposed to send sends the "level" and "start" to the "Startpage" class. my question now is how do you call method "getLevel" from outside the class?  how do you "pass" the level and start to a new class (say class "Test")? I'm looking for line /**********/.


public class test {
   
    /** Creates a new instance of test */
    public test() {
    }
   
    String level = up.getLevel();  /**************/
}



================================
public class Startpage
...
String level = "beginner";
int start = 5;
...
UserProfile up = new UserProfile(level,start);

================================

public class UserProfile {
   
    /** Creates a new instance of UserProfile */
    private String level;
    private int start;
    public UserProfile(String a,int b) {
            level = a;
            start = b;
    }
    public String getLevel(){
        return level;
    }
    public int getFuntion(){
        return start;
    }
}
>> String level = up.getLevel();  

Is correct. You need to write it inside a method where you will define an object of UserProfile called 'up'.
it's still not clear.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial