Question

I am trying to update a jlabel from another class. I've pasted my code below.

Class A {

public void setNetAmount(String s){
    jLabel51.setText(s);
}
public void setDis_percentage(String s){
    jLabel53.setText(s);
}
public void setDiscount(String s){
    jLabel55.setText(s);
}
public void setAdjustment(String s){
    jLabel57.setText(s);
}

}

Class B{
public void SetData(){
new A.setNetAmount(""+netAmount);
new A.setDis_percentage(""+dis_percentage);
new A.setDiscount(""+discount);
new A.setAdjustment(""+adjustment);    
}
}

I am calling the SetData() method in Class A.

public void getData(){
B b = new b();
b.setData();
}

Is there anything wrong with my code ? It is not working at all. Is there any issue of EDT? Please help.

Was it helpful?

Solution

You can't keep invoking "new A". This creates a new instance of class A.

Not really sure why you have a class B to invoke a few methods from class A, but if you use this approach then you would need to pass a reference of class A the your class B method.

Something like:

public class B
{
    public void setData(A a)
    {
        a.setAmount(...);
        a.setPercentage(...);
        ...
    }
}

Then when you invoke the method in your class A you would use:

B b = new B();
b.setData(this);

Although this is a really strange design.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top