Reputation: 2052
I have one condition where, the condition passes, I'll show a JDialog. If it fails, I just need to show a JOption pane and dispose the JDialog. EX. I have to show the dialog on a button click,
import javax.swing.JDialog;
import javax.swing.JLabel;
public class MyDialog extends JDialog {
public MyDialog(String name){
JLabel welcomeLable = new JLabel("Hi welcome");
add(welcomeLable);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setSize(100, 100);
if(name.equals("pass")){
//Do nothing
}else{
dispose();
}
}
public static void main(String[] d){
MyDialog dialog = new MyDialog("fail");
dialog.setModal(true);
dialog.show();
}
}
The message box is shown with validation message but still the dialog is being displayed. How to solve this. How to solve this.
Upvotes: 0
Views: 938
Reputation: 300
First off: show()
is now deprecated. Instead use pack()
followed by setVisible(true)
.
As regards your problem: The issue is that the dialog can still be shown/set visible even if it has been disposed. This may not make sense from a logical standpoint, but that is how the implementation behaves. I would accomplish what you are trying to do by having the constructor set a flag that the main method will react to. Something like this:
import javax.swing.JDialog;
import javax.swing.JLabel;
public class MyDialog extends JDialog {
private boolean showMe;
public MyDialog(String name){
JLabel welcomeLable = new JLabel("Hi welcome");
add(welcomeLable);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setSize(100, 100);
showMe = name.equals("pass");
}
public boolean getShowMe(){
return showMe;
}
public static void main(String[] d){
MyDialog dialog = new MyDialog("fail");
dialog.setModal(true);
if (dialog.getShowMe()){
dialog.pack();
dialog.setVisible(true);
}else{
dialog.dispose();
}
}
}
Upvotes: 0
Reputation: 264
Is not possible what you want without changing the logic (and the code).
In your main
method the #show()
method is called after the constructor ends so after the call on #dispose()
method.
And this is only the first problem (calling #dispose()
before #show()
), the second one being that once you call #show()
even if you manage to somehow dispose/close the dialog you will have a flickering because the dialog will be shown and in the next moment hidden.
Regards
Upvotes: 0
Reputation: 1
Hazard a guess your call in there should be super.dispose(); Because also to indicate to have the Window class (by JDialog extension) ready to use its dispose method you need to construct it more assured by calling super(); as the first line of the constructor in your class. Also because it may be a climb through the hierarchy to find it in the Window class that JDialog extends from, it may be a little shielded in scope.
Upvotes: 0
Reputation: 347184
Separate your logic from your dialog. Your dialog shouldn't be making decisions about whether it should be displayed or not, that's not it's job. Let your code do this...
public class MyDialog extends JDialog {
public MyDialog(String name){
JLabel welcomeLable = new JLabel("Hi welcome");
add(welcomeLable);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setSize(100, 100);
}
public static void main(String[] d){
if(name.equals("pass")){
MyDialog dialog = new MyDialog("fail");
dialog.setModal(true);
dialog.show();
}
}
}
I'd also suggest you consider having a look at JOptionPane
which can, greatly, reduce the amount of duplicated code you might need to use.
Have a look at How to Make Dialogs for more details
Upvotes: 1