0

I have a class for my GUI which I created using Swing UI Designer. All the components were automatically made private but now I need output some text in a variable called consoleTextArea from outside of the UI.class When I set consoleTextArea to public static I get an error saying "UI.form: Cannot bind: field is static: indeed.UI.consoleTextArea"

...
public static JTextArea consoleTextArea;
...
...
UI.consoleTextArea.setText("abc");
...

\src\indeed\UI.form: Cannot bind: field is static: indeed.UI.consoleTextArea

tucuxi
  • 17,561
  • 2
  • 43
  • 74
  • you'll have to show the relevant code. but why do you make that area static? You are aware how setters and getters work? – Stultuske Mar 09 '20 at 08:20
  • No I am not aware actually, I'm a novice in programming. First year of apprenticeship. Could you link me to something? When I didn't set it static it said "non-static field cannot be referenced from a static context" –  Mar 09 '20 at 08:27
  • you shouldn't try to contact the field in a static context, but through an instance of your class. Do you know what this means? – Stultuske Mar 09 '20 at 08:30
  • No I'm not sure. I think it would be better if I posted more of my code, the problem is it's alot.. I have to access the JTextArea multiple times and at different points in my code.. –  Mar 09 '20 at 08:32
  • 2
    honestly, and I don't intend to be rude, but you are trying to write code that is way too advance for you, since you don't understand the basics of the language. This is about the static context: https://stackoverflow.com/questions/28044873/android-non-static-method-cannot-be-referenced-from-static-context-confused/28044935#28044935 https://www.w3schools.com/java/java_encapsulation.asp getters and setters – Stultuske Mar 09 '20 at 08:36
  • I know this code is way too advanced for me, I've been sitting on this project for almost 7 months now. I'm the only developer at the company I started working at and my "instructor" is a technician, not a developer.. I'm trying to teach myself but due to various reasons I have very little time. And I have to make progress obviously. Thanks for the links, I already have getters in my code for the webdriver, just didn't really know that it's like a full "thing" if you know what I mean. –  Mar 09 '20 at 09:13

2 Answers2

1

You can change the value of your label/textArea, through an instance of your UI class.

public class UI {

  // ...

  private TextArea consoleTextArea = new TextArea();
  //
  public void setTextInTextArea(String text) {
    this.consoleTextArea.setText(text);
  }
}

Having setters like this, you can manipulate your textArea from within a different class. This other class, however, must have access to the instance of UI, either instantiate it itself, or getting it passed as a parameter

public class OtherClass {
  UI ui = new UI();

  public void doSomething() {
    ui.setTextInTextArea("New text");
  }

}

or something like:

public class OtherClass {
  public void doSomething(UI ui) {
    ui.setTextInTextArea("New Text");
  }
}
Stultuske
  • 9,296
  • 1
  • 25
  • 37
0

Instead of making the field public (not static public) you should rather add a method like this to your class:

public void setConsoleTextArea(String value) { consoleTextArea.setText(value); }

Then you can do

UI.setConsoleTextArea("abc");

without having to mess with the consoleTextArea field that is automatically generated.

Daniel Junglas
  • 5,830
  • 1
  • 5
  • 22
  • your solution won't compile. You can't call that method in a static way, since it isn't a static method. If you do make it a static method, you'll end up with the same problem. There needs to be an instance of UI – Stultuske Mar 09 '20 at 11:25
  • I assumed that `UI` was an *instance* of a class. Otherwise things don't make a lot of sense to me. Building a user interface with `static` fields looks odd to me. But you could still easily adapt the code by making the proposed function `static`. – Daniel Junglas Mar 09 '20 at 13:19
  • UI is the class, not an instance. The problem is the OP doesn't know the difference between static and instance, and has no basis in Java or OOP. – Stultuske Mar 09 '20 at 13:28