English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Hi, trying to figure out why my JTextField 'scInput' cannot be seen in another class?

I define it in class 'gui' as

private JTextField scInput;

within the public class

public class Gui extends JFrame
implements ActionListener

But when trying to call it in another class, its not being resolved...

public String setStockCode()
{
new Gui();
stockCode = scInput.getText();
}

even after i make a new instance of Gui()

Also, changing it to public from private does nothing, and theyre in the same package.

anyone got any ideas?

Thanks

2007-03-18 20:01:02 · 7 answers · asked by Dave 2 in Computers & Internet Programming & Design

Nope, like i said, changing it to public does nothing, since ive made an instance of the method its called in, it can read the private ones, but i did change it and it still doesnt work, scInput (textfield) cannot be resolved...

2007-03-18 20:15:23 · update #1

7 answers

define it as static then call it directly from the class name
gui.scInput.getText();

and dont forget to creat the scInput object in ur main class

2007-03-18 23:31:57 · answer #1 · answered by ToT 2 · 1 0

I think from looking at the code above that your trying to declaring an instance wrong. It your going to call it from another class you have to create a variable based upon that class, such as:

Gui example = new Gui();

Declaring it like you did only makes it local to that class. I'm not 100 percent but it's worth a try

2007-03-22 09:11:51 · answer #2 · answered by shaughn h 1 · 0 0

You cant use it in another class because you've declared it private.
Change it to public JTextField scInput in the gui class.

2007-03-18 20:08:39 · answer #3 · answered by thomas p 2 · 0 1

Besides changing the scInput to public or creating the accessor method ....

Don't forget to create your scInput object.

scInput = new JTextField();

2007-03-18 23:18:40 · answer #4 · answered by Liviawarty J 2 · 0 0

it can't be accessed because you set it to private.. it is best that you add a public getter method like

public Gui getScInput(){
return this.scInput;
}

in your class. So you can call this method if you want to get the value of scInput.

2007-03-18 20:15:54 · answer #5 · answered by never.fade 3 · 0 1

i got the same answer as above, public instead of private..
but i wanna be sure of something first, did u create the object using the new operator?

2007-03-18 20:50:56 · answer #6 · answered by abd 5 · 0 1

you are not able to access it because the variable is private. if you make it public, you will be able to access it.

2007-03-18 20:08:43 · answer #7 · answered by jk 1 · 0 1

fedest.com, questions and answers