• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

why cant my variables be seen?

icklejez

Member
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
 
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
 
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.
 
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...
 
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();
}
 
Back
Top