0
votes

I created a custom JDialog that has some in internal state that I want to query after in the parent JFrame after the JDialog closes. I set the modality of the JDialog to "true" which blocks the JFrame until the JDialog is disposed.

In the JDialog I update the state and dispose is called using user interaction. I verify that the state is updated. However, when the parent frame resumes after the JDialog is closed, the state I query is not what I set the JDialog to. I am not sure what I am doing wrong here. Any help is appreciated.

Here is a snapshot of my code:

public class IndexFrame extends JFrame {

private static final long serialVersionUID = 9084040250695639818L;

private JMenu _filemenu;

private JMenuBar _menubar;

public IndexFrame() {
    super("Index");
    init();
}

private boolean init() {

    this._new_filemenu_item = new JMenuItem("New");
    this._open_filemenu_item = new JMenuItem("Open");
    FileMenuOpenActionListener open_filemenu_actionlistener = new FileMenuOpenActionListener(this);
    this._open_filemenu_item.addActionListener(open_filemenu_actionlistener);
    this._close_filemenu_item = new JMenuItem("Close");
    FileMenuCloseActionListener close_filemenu_actionlistener = new FileMenuCloseActionListener(this);
    this._close_filemenu_item.addActionListener(close_filemenu_actionlistener);
    this._exit_filemenu_item = new JMenuItem("Exit");
    CloseButtonActionListener exit_filemenu_actionlistener = new CloseButtonActionListener(this);
    this._exit_filemenu_item.addActionListener(exit_filemenu_actionlistener);

    this._filemenu = new JMenu("File");
    this._filemenu.add(this._new_filemenu_item);
    this._filemenu.add(this._open_filemenu_item);
    this._filemenu.add(this._close_filemenu_item);
    this._filemenu.add(this._exit_filemenu_item);

    this._menubar = new JMenuBar();
    this._menubar.add(this._filemenu);

    this.setJMenuBar(this._menubar);

    this.pack();
    this.setVisible(true);        

    return true;        
}

}

public class FileMenuOpenActionListener implements ActionListener{

private IndexFrame _frame;

public FileMenuOpenActionListener(IndexFrame frame) {
    this._frame = frame;
}

@Override
public void actionPerformed(ActionEvent arg0) {
    File file;
    JFileChooser fc;
    LogInDialog ld;
    fc = new JFileChooser();
    if (fc.showOpenDialog(this._frame) == JFileChooser.APPROVE_OPTION) {
        file = fc.getSelectedFile();

        ld = new LogInDialog(this._frame);
        if (ld.isSuccess()) {
            this._frame.refresh();
        }
    }
    else {
    }
}

}

public class LogInDialog extends JDialog {

private JFrame _paraentframe;

private JLabel _userlabel;
private JTextField _userfield;

private JPanel _userpanel;

private JLabel _pwdlabel;
private JPasswordField _pwdfield;

private JPanel _pwdpanel;

private JPanel _inputpanel;

private JButton _loginbutton;
private JButton _cancelbutton;

private JPanel _buttonpanel;

private JPanel _dialogpanel;

private boolean _success;

public LogInDialog(JFrame pf) {
    super(pf, "Log In", true);

    this._paraentframe = pf;

    init();
}

private boolean init() {

    this._userlabel = new JLabel("Username");
    this._userfield = new JTextField(20);

    this._userpanel = new JPanel();
    this._userpanel.add(this._userlabel);
    this._userpanel.add(this._userfield);

    this._pwdlabel = new JLabel("Password");
    this._pwdfield = new JPasswordField(20);

    this._pwdpanel = new JPanel();
    this._pwdpanel.add(this._pwdlabel);
    this._pwdpanel.add(this._pwdfield);

    this._inputpanel = new JPanel();
    this._inputpanel.add(this._userpanel);
    this._inputpanel.add(this._pwdpanel);

    this._loginbutton = new JButton("Log In");
    LogInDialogLogInButtonActionListener loginbuttonlistener = new LogInDialogLogInButtonActionListener(this);
    this._loginbutton.addActionListener(loginbuttonlistener);
    this._cancelbutton = new JButton("Cancel");
    CloseButtonActionListener cancelbuttonlistener = new CloseButtonActionListener(this);
    this._cancelbutton.addActionListener(cancelbuttonlistener);

    this._buttonpanel = new JPanel();
    this._buttonpanel.add(this._loginbutton);
    this._buttonpanel.add(this._cancelbutton);

    this._dialogpanel = new JPanel();
    this._dialogpanel.setLayout(new BorderLayout());
    this._dialogpanel.add(this._inputpanel, BorderLayout.CENTER);
    this._dialogpanel.add(this._buttonpanel, BorderLayout.SOUTH);

    this.setContentPane(this._dialogpanel);    

    this.pack();
    this.setVisible(true);  

    this._success = false;

    return true;
}

public boolean isSuccess() {
    return this._success;
}

public void setSuccess(boolean s) {
    this._success = s;
}

public String getUserName() {
    return this._userfield.getText();
}

public String getPassWord() {
    return String.valueOf(this._pwdfield.getPassword());
}

}

public class LogInDialogLogInButtonActionListener implements ActionListener{

private LogInDialog _dialog;

public LogInDialogLogInButtonActionListener(LogInDialog dialog) {
    this._dialog = dialog;
}

@Override
public void actionPerformed(ActionEvent arg0) {
    String username;
    String password;
    username = this._dialog.getUserName();
    password = this._dialog.getPassWord();
    if (true) {
        this._dialog.setSuccess(true);
        this._dialog.dispose();
    }                
}

}

1
I am not sure what I am doing wrong here - neither are we since we have no ideas what your code is actually doing. Post a proper minimal reproducible example that demonstrates the problem. So all you need is a simple JFrame with a button. When you click the button a modal dialog is display. The dialog should contain a single component so you can change its property. Then when you close the dialog, the frame should display the value of the property. So the point is first prove you can change a single property. Once that works you add the ability to change more properties. Keep it simple when testing a new concept. - camickr
The Login button listener changes the state of success from false to true and then calls dispose so that the FileOpen Action listener can read its state. It should be true but it is always coming back false. - Neil Pittman
Please read or re-read the mcve link. Your code neither compiles nor runs for us, meaning we can't test it nor experience directly the problem that you're experiencing. Moreover, you've posted much more code than is necessary to reproduce the problem as well as compile and run, meaning we have to go through a ton of irrelevant code to find out what's going on. Please help us help you by posting a valid MCVE. - Hovercraft Full Of Eels

1 Answers

0
votes

In the init method of your LogInDialog, you set _success to false AFTER calling setVisible...

this.setVisible(true);  

this._success = false;

This means that AFTER the dialog is closed, _success will be set to false :/