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....

Dani AI

Generated

As already pointed out, the problem came from creating a brand‑new kwMCQ when you tried to read the values. A new instance has fresh fields (or nulls) so any getText() calls return nothing. The robust approach is to move the data out of the view and pass the same object (or a small data model) to the next screen instead of re-instantiating the original frame.

A simple, practical pattern: create a plain model object and pass it into the next frame. For example:

public class Student {
    private final String name;
    private final String rollNo;
    public Student(String name, String rollNo){
        this.name = name;
        this.rollNo = rollNo;
    }
    public String getName(){ return name; }
    public String getRollNo(){ return rollNo; }
}

Then populate and pass it when moving to the next window:

Student s = new Student(nameField.getText(), rollField.getText());
McqTest next = new McqTest(s);
next.setVisible(true);

This keeps UI and data separate, makes testing easier, and avoids the “new frame = empty fields” trap.

Other workable approaches: pass the original frame instance (or a getter for its model) into the second frame, or use a single JFrame and swap JPanels with CardLayout so state stays in one place. Use observer/callbacks or property-change listeners for looser coupling. A few troubleshooting notes: ensure text fields are initialized before calling getText(), call commitEdit() or use getValue() on JFormattedTextField when appropriate, perform GUI work on the Event Dispatch Thread (SwingUtilities.invokeLater), and avoid using globals/statics for state unless you really need them. ’s show/hide idea works too, but passing a model or using CardLayout usually leads to cleaner code.

Recommended Answers

All 4 Replies

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

if i where to go by these i'll declear a an object of type JFreame lets call the object form ie

JFrame form;

in all the three class then i'll have a method that have a sructure like the one below in each classes

public void showMe(JFrame obj)
{
    this.setVisible(true);
    form=obj;
}

we will also include another method in each class that will hide the curent form and move to the privious JFrame

public  void hideMe()
{
this.setVisible(false);
form.setvisible(true);
}

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

Thanks for the reply... Got it fixed....

Thanks for the reply... Got it fixed... Didnt have time to check this site...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.