javaswingnetbeansjtextfieldjsr296

Change text of JTextField from different Class


How can i change the text of a jTextField from other class

Suppose i have Class A in which if i select an item and click create account. I added a tab with same name to my jTabbedPane. This tab is class B. the code for this is:

onclick on "Create Account" this function addclass(mainCB.getSelectedIndex()) has been called

 public void addclass(int a) {
    String s=(String) mainCB.getItemAt(a); //mainCB is variable name of combobox
    JComponent subpanel2=new B(); //added the class
    jTabbedPane1.add(s,subpanel2); //added new tab which is the new class
    B ob=new B(); //object of new class B
    ob.heading(s); //heading is the function in Class B
}

Now how can i Change the jTextField1 text from class A.

heading() function in class B is as follows:

public void heading(String s){
    head.setText(s); //head is the variable name of jTextField1 of class B
}

I have posted the image of both the classes A and B.

This is Class A class A


The new panel which is added in jTabbedPane is class B. This is being called in class A.

Class B


Solution

  • You create two instances of the class B in your addClass method. I think it would solve your problem to call heading on subpanel2, which is of type B. This would go something like:

    public void addclass(int a) {
        String s=(String) mainCB.getItemAt(a); //mainCB is variable name of combobox
        B subpanel2=new B(); //added the class
        jTabbedPane1.add(s,subpanel2); //added new tab which is the new class
        subpanel2.heading(s); //heading is the function in Class B
    }
    

    Is this what you wanted?