• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 219
  • Last Modified:

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



0
jpking72
Asked:
jpking72
  • 4
  • 4
  • 3
  • +1
3 Solutions
 
hoomanvCommented:
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
0
 
hoomanvCommented:
I mean dont create a new frame for each page
have them all in one frame but switch them using CardLayout or just JTabbedPane
0
 
jpking72Author Commented:
isn't there a way to just pass variables from class to class?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
hoomanvCommented:
> isn't there a way to just pass variables from class to class?
use a method in class2 and call it from class1 to pass variables to class2

class B {
   void getVars(vars) {
      this.vars = vars;
   }
}

class A {
   void passVars(vars) {
      B b = new B();
      b.getVars(vars);
   }
}
0
 
d3storCommented:
I suppose you're calling your page-classes from a different class, right?
If so, you can add

publi int[] getAnswers() {
   return this.answers();
}

to your page1 -class
and make a constructor or method for page2 -class which accepts those vars.

-

A bit better approach, however, is to add getAnswers() -method to both page-classes and
do the validating / processing at the calling class or create an observable value object and controller for that object.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> 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.
0
 
jpking72Author Commented:
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;
    }
}
0
 
Mayank SAssociate Director - Product EngineeringCommented:
? What is UserProfile?
0
 
jpking72Author Commented:
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;
    }
}
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> String level = up.getLevel();  

Is correct. You need to write it inside a method where you will define an object of UserProfile called 'up'.
0
 
jpking72Author Commented:
it's still not clear.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
You are writing:

>> String level = up.getLevel();

in the wrong place. It should be inside a method, like:

public test ()
{
  UserProfile up = new UserProfile () ;
  String level = up.getLevel () ;
  System.out.println ( level ) ;

}
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 4
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now