Snake
Snake

Reputation: 14678

Setting JTextField value after panel.setInvisible does not update value

I noticed a behavior that I can't explain. In my GUI, on a button click I display a custom Jdialog that has panel and bunch of textfield. I populate these textfields.

Here is the scenario I am seeing using pseduo code.

public void actionPerformed(ActionEvent e) {
    CustomDialog viewDialog = new CustomDialog (Jframe, true);                
    viewDialog.setVisible(true);
    viewDialog.populateInfo();
}

When the code above runs then all textfields are empty. However if I move the setVisible to after the populateInfo method then all the textFields are populated. Basically the JTextField.setText inside the populate info does not seem to have an affect if the setVisible happens before

Why is this!

Upvotes: 1

Views: 34

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285430

Likely your CustomDialog class is a modal JDialog (also as suggested by the true 2nd constructor parameter). If so, then program flow in the calling code is blocked by the setVisible(true) call, and so your populateInfo() method will only be called after the dialog is no longer visible. The solution is as you already know -- call the method before displaying the dialog.

This is not a bug but a feature. :)
Seriously, since now you know for a fact when program code flow will be halted and when it will resume, and so you can safely query the dialog for its state after the setVisible(true) has been called, and feel confident that in the very least the dialog has been presented to the user, and the user has had time to interact with it and dispose of it.

Upvotes: 1

Related Questions