sending parameter to a JDialog
Hello everyone, from my main window, I am opening a new JDialog window and I want to send a parameter from the main
Opening the new window
ExtensionUI extensionUI = new ExtensionUI(this, true);
extensionUI.setVisible(true);
and from the main of this new class,
ExtensionUI dialog = new ExtensionUI(new javax.swing.JFrame(), true);
and then the constructor
public ExtensionUI(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
parent.printSomething();
}
in the main window I have a method println something, but I cant access thru parent.print();
isn't that parent the main itself or is it like an instance of the JFrame of the main instead? don't know if I explained myself
thanks!
Re: sending parameter to a JDialog
Sorry but I don't understand your question.
Code:
ExtensionUI dialog = new ExtensionUI(new javax.swing.JFrame(), true);
I am assuming ExtensionUI extends JDialog in which case this makes no sense. You are constructing a JDialog with a parent that isn't visible and isn't used anywhere else, why?
Code:
parent.printSomething();
'parent' is type Frame which doesn't have a printSomething method. If you want to call a method declared in the calling class you need to pass in an reference to the calling class to a parameter with the calling classes type. If the calling class extends JFrame you can also use it as the parent for the JDialog if not you need to pass in a reference to the parent Frame as a separate parameter.
Re: sending parameter to a JDialog
the parent is being sent from the main window, which is a JFrame
here it is:
ExtensionUI extensionUI = new ExtensionUI(this, true);
extensionUI.setVisible(true);
the printMethod is in the main class/window, I want to call this method from the JDialog window and I thought the parent was the same thing as the main class, because I am sending main and the JDialog is receiving it as parent, but I cant access to main thru "parent"
Re: sending parameter to a JDialog
Your ExtensionUI classes constructor take a Frame type as a parameter which is a super class of your main class and so you can legally pass in your main class. The problem is the reference to your main class is now in the parent variable which is a Frame type ie it doesn't know anything about your main class type and so you can't call any methods in your main class type. There are 2 options open to you:
1. Cast the 'parent' variable to the type of your main class. The problem here is if you create another instance of ExtensionUI and pass in a class that sub classes Frame but isn't your main class (or a sub class of it) then your cast will fail at runtime which is not good.
2. Change the ExtensionUI constructor so the parent parameter is the type of your main class. This means you can't construct an ExtensionUI object with the wrong class type because it would give a compile time error which is much better than a runtime one.