1

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

Gaurav
  • 55
  • 10
  • Basically i have to add panels if something is selected in combobox and "Create Account" is click. Class B is jPanel containing the second image. So i am adding component in my tabbedPane in first class. But i am unable to change any property of class B form from class A – Gaurav Jul 10 '11 at 02:17

1 Answers1

2

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?

101100
  • 2,666
  • 23
  • 28
  • subpanel2.heading(s) is showing error. it says cannot find method heading() – Gaurav Jul 10 '11 at 02:24
  • Ah, you need to declare `subpanel2` as a `B`, not a `JComponent`, so the compiler knows it is capable of performing the `heading` method. I've edited my post to reflect this. – 101100 Jul 10 '11 at 02:32
  • Ahh... it works... Thank u very much.. i googled my ass off for this – Gaurav Jul 10 '11 at 02:38
  • Can you help in this ?? Suppose if i click "Main" from the new panel. What will be the code to go to main panel which is the class A – Gaurav Jul 10 '11 at 02:42
  • In that case, you'll need to give `B` a reference to `A` (perhaps by passing it into the constructor) and then create a method in `A` to call `setSelectedIndex` on the `JTabbedPane`. In these GUIs, you will often need to give various objects references to each other so they can call each others' methods. – 101100 Jul 10 '11 at 02:54
  • Can you give the code. First i was using jPanel's instead of classes. Just hiding and unhiding them on clicking "Create Account". But that was quite hard coding. So i chose this class thing. this code was working properly that time `public void mainwindow(){ jTabbedPane1.setSelectedIndex(0); mainCB.setSelectedIndex(0); }` – Gaurav Jul 10 '11 at 02:58
  • Can i create a object of class A in class B and call the same function mainwindow() I tried but this is not working??? – Gaurav Jul 10 '11 at 02:59
  • I think you are confusing classes and objects. If you create a new `A` in `B`, it will be a different `A`, not the original. So, in `B`, create a field: `private A mainTab;` and add an `A` parameter to `B`s constructor: `public B (A newMainTab) { mainTab = newMainTab; }`, then pass that into the new `B` when it is created in the code above: `B subpanel2=new B(this);`. Then when you click the main button, you can call `mainTab.mainwindow ();`. – 101100 Jul 10 '11 at 03:10
  • Thanks a lot... u r a genious...:) – Gaurav Jul 10 '11 at 03:21