1

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:

  1. How I disable or inactive my first frame while second frame is open.
  2. 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?
  3. 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.
Cœur
  • 37,241
  • 25
  • 195
  • 267
Vinit ...
  • 1,409
  • 10
  • 37
  • 66

1 Answers1

3

1) In my application two frame is there

don't use two JFrames, thats isn't possible without using (JNI or JNA), these containers doesn't implements parent, there is hard job move one JFrame toFront() / toBack() / real FocusRecycle is cruel joke

2) use JDialog, with setModal or ModalityType,

3) plain vanilla JOptionPane is best way ho to do it,

4) JFrame / JDialog / JWindow doesn't implements finalize(), then doesn't matter if you call for setVisible(false) or dispose()

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • actually I want to take username, password, IPAddress and port number in the second frame. So is this possible to show JDialog in this way. – Vinit ... Mar 24 '12 at 09:35
  • right JOptionPane can returns that as value or array of values [rest here](http://stackoverflow.com/questions/9798066/gettext-vs-getpassword) – mKorbel Mar 24 '12 at 09:40
  • See also [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) – Andrew Thompson Mar 24 '12 at 10:34
  • @Andrew Thompson thanks I can't found out this great answer, btw this picture corresponding with my Xxx after last night, hmmmm not, noooo this animal have got more smarter look than I.... – mKorbel Mar 24 '12 at 10:48