Skip to main content

Instantiating a JFrame inside another JFrame?

Please note these forums are being decommissioned and use the new and improved forums at
2 replies [Last post]
Joined: 2011-06-28

i m making an encryption application in swing.

i m in a JFrame and then for asking the password, i instantiate another JFrame from this JFrame (based on an event in the JMenu).

The new PasswordAsking JFrame presents the GUI. It is designed so that user then enters passsword and clicks finish and the control then returns to the calling frame.

But what is happening is that the control immediately returns to the calling frame after the PasswordAsking Frame is displayed,even before the user enters password and presses the finish button. this causes the value of password in the calling frame to be null.(this is my problem)

its something like

class Welcome extends JFrame implements ActionListener

{ ...........

Welcome() {

....... setVisible(true);

} //end of constructor

public void actionPerformed(ActionEvent ae) //throws Exception

{ if((source.getText()).equals("Encrypt Files"))


System.out.println("encryption chosen");

//choose which file to encrypt

passwd=new ArrayList();

AskPassword askPass=new AskPassword(passwd,1);

passwdChar=new char[passwd.size()];//passwd in char[] form

for(int i=0; i<passwd.size(); i++)


Character passElement=(Character)passwd.get(i);




System.out.println("here passwd size is"+passwd.size());


}//end of ActionPerformed

} //end of Welcome


public class AskPassword extends JFrame implements ActionListener

{ .....

AskPassword(ArrayList pass12,int mode) //mode=1 for encryption and 2 for decryption

{ passForeign=pass12; //this is just a reference assignment ,hmm??

..... setVisible(true);


public void actionPerformed(ActionEvent ae)


String s=ae.getActionCommand();

if(s.equals("CANCEL")) {dispose(); }


{.............. for(int i=0;i<passText.length;i++){

passForeign.add(new Character(passText[i]));

} ......



Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2011-07-02

What you really want is "modal" behaviour of child window.

I think that in your case it would be easier for you if you just use something based on JOptionPane instead of second JFrame, or even just call JOptionPane.showInputDialog(), if it is sufficient for your task.

Joined: 2011-06-28

Thanks Rodian...u said exactly wat must be the answer. i was a busy hence a late reply