public class MyWindow extends JFrame {
JButton botonCreate = new JButton("Open son windows");
public void doSomething(){
botonCreate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JDialog vce = new JDialog(this);
}
});
}
}
the idea is to block the JFrame parent while the child is visible (JDialog).
the parameter of the constructor of the JDialog must be the containing class “MyWindow” but a problem of scope, is the ActionListener, and this generates the following error.
how I can fix this?
error message:
no suitable constructor found for JDialog()
constructor JDialog(java.awt.Frame) is not applicable
(actual argument cannot be converted to java.awt.Frame by method invocation conversion)
constructor JDialog(java.awt.Dialog) is not applicable
(actual argument cannot be converted to java.awt.Dialog by method invocation
conversion)
Your issue is simple. When you’re using
thisyou’re actually usingActionListener.this. So to correct this error you must explain to the compiler you want to actually use the inclosing classthisby specifying it withMyWindow.this.