why cant my variables be seen?

icklejez

Member
Jan 12, 2007
50
0
0
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()

anyone got any ideas?

Thanks
 

txrandom

Diamond Member
Aug 15, 2004
3,773
0
71
Try this, make a method that you can call that changes JTextField.

So something like:

public void setscInput(String input) {

JTextField = new JTextField(input);

}

I don't remember exactly how to initialize JTextField
 

txrandom

Diamond Member
Aug 15, 2004
3,773
0
71
I think I remember running into a problem like this when I was making a big GUI program. I remember setting some GUI components to final. I have no idea why I had to make them final, but just try making them final and try the method above too.
 

icklejez

Member
Jan 12, 2007
50
0
0
Hmmm, changing the textfield to final didnt do anything and that method threw up a load of errors when implemented, very odd, it doesnt make sense why it cant see it...
 

daMachine

Senior member
Oct 30, 2001
322
0
76
It's been a while since I've done Java, but I think you need something like this.

// this implementation assumes scInput is public
public String setStockCode()
{
Gui myGui = new Gui();
stockCode = myGui.scInput.getText();
}