In my application two frame is there. In main frame “Server” button is there. When I click on “Server” button second frame will open and after enter some detail when i click on “submit” button which is on second frame this frame will close and control again go to the “Server” button where I call this frame so I will do my rest of task.
And one more thing I want in my application when the second frame is open then no activity is on first frame.
My first frame is: JFrame frame;
In this frame one Server button is there: JButton btnSaveOnServer;
In addActionListener of this button I write this code to open second frame:
btnSaveOnServer.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
ServerClass window = new ServerClass();
window.serverFrame.setVisible(true);
}
});
Code for the Second Frame:
public class ServerClass{
public JFrame serverFrame;
private JLabel userName;
private JLabel password;
private JTextField textUsername;
private JPasswordField textPassword;
private JButton btnOk;
protected ServerClass(){
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
serverFrame = new JFrame("Server Entry Token");
serverFrame.setBounds(500, 280, 335, 250);
serverFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
serverFrame.getContentPane().setLayout(null);
font = new Font("Arial", Font.BOLD, 15);
userName = new JLabel("Username");
userName.setBounds(4, 40, 100, 15);
userName.setFont(font);
password = new JLabel("PassKey");
password.setBounds(4, 69, 100, 15);
password.setFont(font);
textUsername = new JTextField();
textUsername.setBounds(110, 35, 200, 25);
textUsername.setFont(font);
textPassword = new JPasswordField(10);
textPassword.setBounds(110, 65, 200, 25);
textPassword.setEchoChar(ch);
btnOk = new JButton("Submit");
btnOk.setBounds(30, 170, 85, 25);
serverFrame.add(userName);
serverFrame.add(password);
serverFrame.add(textUsername);
serverFrame.add(textPassword);
serverFrame.add(btnOk);
btnOk.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
if (!textUsername.getText().equals("")
&& !textPassword.getText().equals("")) {
tArea.append("Username:\t"+textUsername.getText()+"\n");
tArea.append("Pass:\t"+textPassword.getText()+"\n");
//tArea is the area in first frame where I display the entered data.
serverFrame.dispose();
}
}
});
}
}
Here I add the code serverFrame.dispose(); in “Submit” button which close the frame but control not come to the first frame.
So my problem is:
- How I disable or inactive my first frame while second frame is open.
- in my code I am calling second frame in “server” button of first frame. Is this write way to call second frame into first frame?
- how I close the second frame (after clicking on submit button) so frame will close and control will go to the “Server” button of first frame.
1)
In my application two frame is theredon’t use two JFrames, thats isn’t possible without using (
JNI or JNA), these containers doesn’t implementsparent, there is hard job move oneJFrametoFront()/toBack()/ realFocusRecycleis cruel joke2) use JDialog, with
setModalorModalityType,3) plain vanilla JOptionPane is best way ho to do it,
4)
JFrame / JDialog / JWindowdoesn’t implementsfinalize(), then doesn’t matter if you call forsetVisible(false)ordispose()