• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 221
  • 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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