943,667 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 2972
  • Java RSS
Sep 29th, 2007
0

Getting values from other class..

Expand Post »
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:
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....
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Caled is offline Offline
22 posts
since Jul 2007
Oct 3rd, 2007
0

Re: Getting values from other class..

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
Reputation Points: 919
Solved Threads: 354
Nearly a Posting Maven
stultuske is offline Offline
2,487 posts
since Jan 2007
Oct 3rd, 2007
0

Re: Getting values from other class..

if i where to go by these i'll declear a an object of type JFreame lets call the object form ie
Java Syntax (Toggle Plain Text)
  1. JFrame form;
in all the three class then i'll have a method that have a sructure like the one below in each classes
Java Syntax (Toggle Plain Text)
  1. public void showMe(JFrame obj)
  2. {
  3. this.setVisible(true);
  4. form=obj;
  5. }
we will also include another method in each class that will hide the curent form and move to the privious JFrame
Java Syntax (Toggle Plain Text)
  1. public void hideMe()
  2. {
  3. this.setVisible(false);
  4. form.setvisible(true);
  5. }
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
Last edited by awo; Oct 3rd, 2007 at 9:10 pm.
awo
Reputation Points: 10
Solved Threads: 1
Newbie Poster
awo is offline Offline
21 posts
since Oct 2007
Oct 6th, 2007
0

Re: Getting values from other class..

Thanks for the reply... Got it fixed....
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Caled is offline Offline
22 posts
since Jul 2007
Oct 6th, 2007
0

Re: Getting values from other class..

Thanks for the reply... Got it fixed... Didnt have time to check this site...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Caled is offline Offline
22 posts
since Jul 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: I need help 4 Dynamic GUI ???
Next Thread in Java Forum Timeline: Help me





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC