| | |
Getting values from other class..
Thread Solved |
•
•
Join Date: Jul 2007
Posts: 20
Reputation:
Solved Threads: 0
Hi,
Im having a problem here. I have 3 classes/frames. After arriving at the 3rd frame, i want to get values entered in the 1st first frame. All the variables in the 1st class are declared as Private. In order for me to get this value from other class, i added a public method getName() in the 1st class. Im still unable to get the value... below is the code:
help me....
Im having a problem here. I have 3 classes/frames. After arriving at the 3rd frame, i want to get values entered in the 1st first frame. All the variables in the 1st class are declared as Private. In order for me to get this value from other class, i added a public method getName() in the 1st class. Im still unable to get the value... below is the code:
public class kwMCQ extends JFrame implements ActionListener
{
public static void main(String [] args)
{
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame mcq = new kwMCQ();
}
private static final int WIDTH=400;
private static final int HEIGHT=250;
private JPanel pnlLogo, pnlIntro, pnlInput, pnlButton, pnlInstruct;
private JLabel lblLogo,lblWelcome, lblFillIn, lblName, lblRollno, lblInstruction;
private JTextField txtName;
private JFormattedTextField txtRollno;
private JButton btnStart, btnBeginMcq;
private JLabel instructLabel1, instructLabel2, instructLabel3, instructLabel4, instructLabel5;
private JButton startB;
private McqTest secondFrame = new McqTest();
public kwMCQ()
{
ImageIcon logo = new ImageIcon("itbLogo.png");
....some other statements.. then
//accessor
public String getTxtName()
{
return txtName.getText();
}
public String getTxtRollNo()
{
return txtRollno.getText();
}
...class that calls the 1st class (the action of getting value is implemented in the listener..
String stuRollNo,stuName;
// im not sure if this is the right way
// but i get nulll value...
// may be i shouldnt create the object
// but i duno how to access it with
kwMCQ login = new kwMCQ();
stuRollNo = login.getTxtRollNo()+"\n";
stuName = login.getTxtName() + "\n";
help me....
mjah, you really shouldn't.
since you are creating a new Object of the type, you won't be working with the Object you've allready stored data in, but with a new one, that has only the data in it, which is specified in the constructor.
since you didn't show the entire constructor, I have no clue wether or not those variables contain null or a value, but I guess it's the first one.
in order to use the data that you've registered in your first frame, once you arrive in the next, you need to use that instance of the Object, not creating a new one. One sollution might be putting that instance as a parameter in your constructor for your next class
since you are creating a new Object of the type, you won't be working with the Object you've allready stored data in, but with a new one, that has only the data in it, which is specified in the constructor.
since you didn't show the entire constructor, I have no clue wether or not those variables contain null or a value, but I guess it's the first one.
in order to use the data that you've registered in your first frame, once you arrive in the next, you need to use that instance of the Object, not creating a new one. One sollution might be putting that instance as a parameter in your constructor for your next class
if i where to go by these i'll declear a an object of type JFreame lets call the object form ie
in all the three class then i'll have a method that have a sructure like the one below in each classes
we will also include another method in each class that will hide the curent form and move to the privious JFrame
and since form is the same as the object that set the form to be visible you can manipulate obect on that form by alling setting any property of the objects
Java Syntax (Toggle Plain Text)
JFrame form;
Java Syntax (Toggle Plain Text)
public void showMe(JFrame obj) { this.setVisible(true); form=obj; }
Java Syntax (Toggle Plain Text)
public void hideMe() { this.setVisible(false); form.setvisible(true); }
Last edited by awo; Oct 3rd, 2007 at 9:10 pm.
![]() |
Similar Threads
- I need help on urgent basis (Networking Hardware Configuration)
- very strange string behaviur.. (C)
- Does constructor create the object ? (C++)
- a simple question{dynamically create object} (C++)
- A function which will transfer the data from a file to STL( vector or map) object. (C++)
- A few questions on classes!.. (C++)
- How to get Array type members' value from a class (JSP)
- Dimensioning Strings (C#)
- Query Error (PHP)
- SIMPLE BOX GIVING ME PROBLEMS--this is homeowrk but i have tried for hours (Java)
Other Threads in the Java Forum
- Previous Thread: I need help 4 Dynamic GUI ???
- Next Thread: Help me
| Thread Tools | Search this Thread |
3d 6 @param affinetransform android api applet application arc array arrays automation binary bluetooth bold byte c++ chat class client code color compare component coordinates database detection doctype eclipse educational error file fractal froglogic game givemetehcodez graphics gui guitesting helpwithhomework html ide ideas image ingres input integer internet intersect j2me java java.xls javaexcel javaprojects jni jpanel jtextarea julia keytool keyword linux list loop map method methods mobile netbeans newbie nextline object pong print problem producer program programming project projectideas read recursion recursive replaysolutions rim scanner sell server set size sms sort sql string swing terminal threads tree web websites windows





