开发者

Call Method from load class from classForName java

开发者 https://www.devze.com 2023-04-01 22:19 出处:网络
First of all I´m using IBM Java2 SDK, Standard Edition, v 1.2 Lest start I have two Clasess, My main goal is to add a Panel class dynamically into a JTabbed Panel and execute the method from the cl

First of all I´m using IBM Java2 SDK, Standard Edition, v 1.2

Lest start I have two Clasess,

My main goal is to add a Panel class dynamically into a JTabbed Panel and execute the method from the class loaded.

Lets start:

Current Menu that will hold the butons that will fire to add classes.

Main class.

public class WMenu extends javax.swing.JFrame {

JTabbedPane1 = new javax.swing.JTabbedPane();
JBtnexec = new javax.swing.JButton();
JBtnundo = new javax.swing.JButton();

...

public static void main(java.lang.String[] args) {
   ...
}

/*When the user Press the btn should execute this method.*/
public void exec(java.awt.event.MouseEvent e) {
    JPanel content = new JPanel();

    /*Name of my class is unknown at this ponit will be controled by Variable for this 
    case i addit manually, */
    Class qc = Class.forName("Wcontrol");  
    content = (JPanel) qc.newInstance();
    JTabbedPane1.addTab("Control", content );
}

/*when press Undo btn */

public void undo(java.util.EventObject newEvent) {
  Class wMenu = Class.forName("Wcontrol");
  Method m = wMenu.getDeclaredMethod("undo", new Class[] {String.class});
  Object c = wMenu.newInstance();   
  m.invoke(c,new Object[] {new String(this.getClass().getName().toString())});
}

}

Class #2 /*My panel that is the Control screeen.Have some label fields */

public class Wcontrol extends JPanel {
  txt_cve = new javax.swing.JTextField();

 public void undo(String in) {

/*txt_cve.setText("TTT");  Comment first test...*/
System.out.println("Data " + txt_cveind.getText()); 
    JOptionPane.showMessageDialog(this, "Data: " +txt_cveind.getText(), "Info", 1);
 }

}

Now, When I run the Frame came out perfe开发者_运维知识库ct, when i execute to load the new class Panel , came prefect also, display in the JTabed panel , with no issues.

The thing is when I type in the txt filed form class Wcontrol example = "TTT" and press the undo btn (Main Class), will execute the method in the wControl becase it came out with the Show Message , but the Text Data is not there... is empty ??

If i add the comment setText , and I run it again does display the Data : TTT Does enyone will know what am i doing wrong here? or any suggestion


I think the program does what you code for. It's empty because you are creating new instance of Wcontrol in undo() that in turn initialize variable txt_cve with empty text. Check my comments below:




       public void undo(java.util.EventObject newEvent) {
            Class wMenu = Class.forName("Wcontrol");
            Method m = wMenu.getDeclaredMethod("undo", new Class[] {String.class});

            // NOTE #1: This one create a new instance of the class 
            Object c = wMenu.newInstance();   
            m.invoke(c,new Object[] {new String(this.getClass().getName().toString())});
        }

        public class Wcontrol extends JPanel {
            // Note #2: Reinitialize the txt_cve 
            txt_cve = new javax.swing.JTextField();

            public void undo(String in) {
                // Note #3: This is the interesting part, you change txt_cve and the
                // txt_cveind got affected, so you must have assigned txt_cfe as a reference
                // to txt_cveind somewhere in your code. 
                /*txt_cve.setText("TTT");  Comment first test...*/
                System.out.println("Data " + txt_cveind.getText()); 
                JOptionPane.showMessageDialog(this, "Data: " +txt_cveind.getText(), "Info", 1);
            }
        }

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号