Favolas
Favolas

Reputation: 7243

Java show result on JTextField

Started making a simple GUI using netbeans and now I'm facing some problem.

I have this:

package my.sccsymapp;

public class sccsymapp extends javax.swing.JFrame {
    /*SOME CODE*/                                     
    public static void main(String args[]) {
        /*SOME CODE*/
    }
    // Variables declaration - do not modify
    private javax.swing.JTextField tempmedespCost;
    // End of variables declaration
}

If I run this, it works as expected. testis placed on my JTextField.

But what I want to do is to use tempmedespCost.setText("test"); in some other class of my code.

I have this class:

package my.sccsymapp;

import java.util.*;

public class Servico extends sccsymapp{
    /*SOME CODE*/ 
    public void relat (){
    /*SOME CODE*/ 
    tempmedespCost.setText("test");
    }
    /*SOME CODE*/ 
}

It now says:

tempmedespCost has private access in my.sccsymapp.sccsymapp

So I have changed tempmedespCost to public.

Now no error is shown, runs without errors but testis not placed on my JTextField.

Can you point me in some direction?

Upvotes: 2

Views: 1398

Answers (1)

Estragon
Estragon

Reputation: 1462

tempmedespCost is defined as private in your class

private members can only be acceded by functions that are members of the class. Children of the class (like Servico) can't access to private fields.

You can either change the visibility of tempmedespCost to protected or create a getter that will let you access to tempmedespCost

I suggest you read some documentation about Java visibility in Controlling Access to Members of a Class.

Upvotes: 5

Related Questions