MILIND_JOG
asked on
getConstructor in JAVA
Dear Experts,
I have a JDialog and want to call the JDialog using getConstructor. I am unable to pass String array to the constructor and system is giving error like
"java.lang.NoSuchMethodExc eption: guestName.<init>(java.lang .Integer, java.lang.String, boolean, java.awt.Frame)
at java.lang.Class.getConstru ctor0"
I want to pass Integer,String Array, Boolean and Frame. I do require your help know what I should class should I declare to pass an array of String[][]. The sintex which I used is as under
String guestName2[][] = {{"A","0","B"},{"C","1","D "}}; //,{"E","2","F"},{"G","3", "H"}};
int gc = Integer.parseInt(Guest_tex t.getText( ));
String retv = "guestName";
try {
Class cl = Class.forName(retv);
Constructor constructor = cl.getConstructor(new Class[] {Integer.class, String.class, boolean.class, Frame.class});
Object invoker = constructor.newInstance(ne w Object[]{gc, guestName2, true, (new javax.swing.JFrame())});
}
guestName2 is an two dimensional array.
Regards
I have a JDialog and want to call the JDialog using getConstructor. I am unable to pass String array to the constructor and system is giving error like
"java.lang.NoSuchMethodExc
at java.lang.Class.getConstru
I want to pass Integer,String Array, Boolean and Frame. I do require your help know what I should class should I declare to pass an array of String[][]. The sintex which I used is as under
String guestName2[][] = {{"A","0","B"},{"C","1","D
int gc = Integer.parseInt(Guest_tex
String retv = "guestName";
try {
Class cl = Class.forName(retv);
Constructor constructor = cl.getConstructor(new Class[] {Integer.class, String.class, boolean.class, Frame.class});
Object invoker = constructor.newInstance(ne
}
guestName2 is an two dimensional array.
Regards
ASKER
Dear Valery,
Thanks for fast response.
I have two seperate JDialog
1> newGuest and 2> guestName
I want to call guestName from newGuest
guestName constructor is having 4 arguments
public guestName(int gn,String guestName1[][], boolean modal, java.awt.Frame parent)
From newGest, I am calling guestName as below
String guestName2[][] = {{"A","0","B"},{"C","1","D "}}; //,{"E","2","F"},{"G","3", "H"}};
int gc = Integer.parseInt(Guest_tex t.getText( ));
String retv = "guestName";
try {
Class cl = Class.forName(retv);
Constructor constructor = cl.getConstructor(new Class[] {Integer.class, String.class, boolean.class, Frame.class});
Object invoker = constructor.newInstance(ne w Object[]{gc, guestName2, true, (new javax.swing.JFrame())});
}
I am getting error as
java.lang.NoSuchMethodExce ption: guestName.<init>(java.lang .Integer, [[Ljava.lang.String;, boolean, java.awt.Frame)
at java.lang.Class.getConstru ctor0(Clas s.java:270 6)
Please give your inputs.
Regards
Thanks for fast response.
I have two seperate JDialog
1> newGuest and 2> guestName
I want to call guestName from newGuest
guestName constructor is having 4 arguments
public guestName(int gn,String guestName1[][], boolean modal, java.awt.Frame parent)
From newGest, I am calling guestName as below
String guestName2[][] = {{"A","0","B"},{"C","1","D
int gc = Integer.parseInt(Guest_tex
String retv = "guestName";
try {
Class cl = Class.forName(retv);
Constructor constructor = cl.getConstructor(new Class[] {Integer.class, String.class, boolean.class, Frame.class});
Object invoker = constructor.newInstance(ne
}
I am getting error as
java.lang.NoSuchMethodExce
at java.lang.Class.getConstru
Please give your inputs.
Regards
I have a JDialog and want to call the JDialog using getConstructor.Why? Why are you using reflection?
ASKER
Dear CEHJ,
Please suggest the alternate.
Regards
Please suggest the alternate.
Regards
ASKER
Dear CEHJ,
I want to instanciate the second JDialog after some inputs in first JDialog and values need to pass from one to other.
Regards
I want to instanciate the second JDialog after some inputs in first JDialog and values need to pass from one to other.
Regards
Please suggest the alternate.I can't really, since you don't give your GOAL
Gernerally speaking, reflection should only be used for very specialized purposes
I want to instanciate the second JDialog after some inputs in first JDialog and values need to pass from one to other.Then all you need to do is to provide a constructor in your JDialog subclass that accepts the required parameters.
ASKER
Dear CEHJ,
I tried for the same but I am getting error.
java.lang.NoSuchMethodExce ption: guestName.<init>(java.lang .Integer, [[Ljava.lang.String;, boolean, java.awt.Frame)
at java.lang.Class.getConstru ctor0(Clas s.java:270 6)
I have passed exectlty the same which is required in second JDialog.
Regards
I tried for the same but I am getting error.
java.lang.NoSuchMethodExce
at java.lang.Class.getConstru
I have passed exectlty the same which is required in second JDialog.
Regards
ASKER
Deear CHEJ,
I am suspecting error due to [[Ljava.lang.String
Regards
I am suspecting error due to [[Ljava.lang.String
Regards
What you should be doing is something like this:
MySecondDialog d = new MySecondDialog(someStringArray, true, someFrame);
ASKER
Ok CHEJ,
Let me try and will come back to you.
Regards
Let me try and will come back to you.
Regards
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Issue closed and supported well.
Regards.
Regards.
Hei Milind,
CEHJ is right, don't use reflection! Use the classes in this way (I created some for you)
import javax.swing.*;
public class JFrameTester {
public static void main(String[] args) {
JFrame f = new JFrame("your JFrame");
f.setSize(400, 400);
f.setLocation(300,200);
f.setVisible(true);
String guestName2[][] = {{"A","0","B"},{"C","1","D "}}; //,{"E","2","F"},{"G","3", "H"}};
NewGuest newGuest = new NewGuest(f, false, 5, guestName2);
newGuest.setSize(250, 250);
newGuest.setVisible(true);
newGuest.showGuestNname();
}
}
========================== ========== ========== ========== =====
import javax.swing.*;
import java.awt.*;
public class NewGuest extends JDialog {
private Integer yourInt;
private String[][] guestName2;
private Frame owner;
public NewGuest(Frame owner, boolean modal, Integer yourInt, String[][] guestName2) throws HeadlessException {
super(owner, modal);
this.owner = owner;
this.yourInt = yourInt;
this.guestName2 = guestName2;
}
public void showGuestNname() {
GuestName guestName = new GuestName(owner, false, 5, guestName2);
guestName.setSize(150, 150);
guestName.setVisible(true) ;
}
}
========================== ========== ========== =========
import javax.swing.*;
import java.awt.*;
public class GuestName extends JDialog {
private Integer yourInt;
private String[][] guestName2;
public GuestName(Frame owner, boolean modal, Integer yourInt, String[][] guestName2) throws HeadlessException {
super(owner, modal);
this.yourInt = yourInt;
this.guestName2 = guestName2;
}
}
then start the first class! forget about reflection, it's for other purposes.
CEHJ is right, don't use reflection! Use the classes in this way (I created some for you)
import javax.swing.*;
public class JFrameTester {
public static void main(String[] args) {
JFrame f = new JFrame("your JFrame");
f.setSize(400, 400);
f.setLocation(300,200);
f.setVisible(true);
String guestName2[][] = {{"A","0","B"},{"C","1","D
NewGuest newGuest = new NewGuest(f, false, 5, guestName2);
newGuest.setSize(250, 250);
newGuest.setVisible(true);
newGuest.showGuestNname();
}
}
==========================
import javax.swing.*;
import java.awt.*;
public class NewGuest extends JDialog {
private Integer yourInt;
private String[][] guestName2;
private Frame owner;
public NewGuest(Frame owner, boolean modal, Integer yourInt, String[][] guestName2) throws HeadlessException {
super(owner, modal);
this.owner = owner;
this.yourInt = yourInt;
this.guestName2 = guestName2;
}
public void showGuestNname() {
GuestName guestName = new GuestName(owner, false, 5, guestName2);
guestName.setSize(150, 150);
guestName.setVisible(true)
}
}
==========================
import javax.swing.*;
import java.awt.*;
public class GuestName extends JDialog {
private Integer yourInt;
private String[][] guestName2;
public GuestName(Frame owner, boolean modal, Integer yourInt, String[][] guestName2) throws HeadlessException {
super(owner, modal);
this.yourInt = yourInt;
this.guestName2 = guestName2;
}
}
then start the first class! forget about reflection, it's for other purposes.
:)
CEHJ was faster than me ;-)
public class GuestName extends JDialog {
private Integer yourInt;
private String[][] yourArray;
public GuestName(Integer yourInt, String[][] yourArray, Frame owner, boolean modal) throws HeadlessException {
super(owner, modal);
this.yourInt = yourInt;
this.yourArray = yourArray;
}
}