First I would like to say that I have only worked with java for 1 month now. This is probly a pretty simple question. I searched and the classic fruit example did not make any sense to me. I spent hours looking at it and trying to figure out how to apply this to make it work. It does not make any sense to me because everyone explains how to get and set a property with 2 lines of code and no structure statements. I would really appreciate a breakdown in how to talk to non-static from static.
I would like to setText in text box in my OBD2nerForm class from a separate and static class.
public class OBD2nerForm extends java.awt.Frame {
/** Creates new form OBD2nerForm */
public OBD2nerForm() {
initComponents();
} ....................................
public String setText(String text){
this.jFormattedTextField1.setText(text);
}
I think I have a static reference to this instance of the form defined here..
public class Status {
public static OBD2nerForm form = new OBD2nerForm();
it is called from my main like this
public class obd2ner {
public static void main(String[] args) throws IOException {
Status.form.main(args);
Then when I try to call it.. Status.form.getText gives me the initial values when the form is created. When I setText, it does not change the one on the screen.
I am just displaying this to keep it simple. There are many other parts going on. I have a static monitor on a serial port and I want it to grab the next data to be sent from the text box and then increment it.
I just don’t understand how to use a getter and a setter on a non-static. It’s not quite doing what I need it to do. It seems like I am seeing one instance on my screen and it is using a new instance to perform the getting and setting.
I tried this as per an answer I received, but it did not work…
public class OBD2nerForm extends java.awt.Frame {
String X = "";
//Trying out the runnable method of incrementing the code
public String getNewScannerValueRunnable(){
Runnable doWorkRunnable = new Runnable() {
@Override
public void run() {
Status.form.getNewRotatingValue()
;}
};
SwingUtilities.invokeAndWait(doWorkRunnable);
return X;
}
I could really use some other suggestions. i just don’t understand what has to happen here.
Your form is being created fine, and there’s just one reference to it, and it’s ending up in that static variable. All is well up to that point.There’s a ‘secret’ of Swing you need to be aware of: You cannot (visibly) change the properties of GUI objects from any thread other than the Swing thread, aka the Event Dispatching Thread.
The trick to doing it anyway is to pass the property-changing code as a
Runnableto either ofSwingUtilities.invokeAndWait()orSwingUtilities.invokeLater().EDIT:
OK, let’s back up. Your form is AWT based, not Swing based, so I’m afraid my advice on using
SwingUtilitieswould probably not have helped you, even if you had implemented it correctly. I’ll try to give more specific hints this time.You’ve created a class
OBD2nerFormthat’s an AWT form. That class has a constructor which callsinitComponentsto set up some GUI components on the screen.The class also has a method called
setTextthat will put its argument text into one of the fields on the form. That method is an instance method, i.e. it’s not “static”, as you’d call it.You have another class,
Statuswith a class fieldform. The initializer forformcalls the constructor forOBD2nerForm. That will create an instance of the form and store it inform; but I haven’t seen ashow()orsetVisible()call being made to the form to actually display it.Here are the first signs of trouble:
Class names (like
obd2ner) should start with capital letters; but that’s a matter of style and convention, it’s not what’s causing you problems. Following the conventions helps other people read and debug your code, though.The bigger problem is
obd2ner.main()calling your form’smain(). That could be made to work, but it’s usually a sign that you’re doing something wrong.While nothing stops you from coding static
mainmethods into as many of your classes as you want, only one of those main’s can be started from the outside, i.e. when you run your application. The firstmainis essentially the ‘boss’ method for your program.The “first main” usually instantiates and initializes a few objects. In a non-GUI application,
main()may then start up a loop or some other control structure, wherein it will then orchestrate the actions of the other objects. In a GUI application,main()will usually just instantiate and then show the GUI, and then end; once the GUI is visible, all further program activity is triggered by actions the user performs on the GUI.Without seeing your code, I’m guessing that
Obd2nerForm.main()also instantiates Obd2nerForm, and shows it. So you probably indeed have one instantiated but invisible form hanging offStatus.formand another one instantiated, visible and referenced from some variable inObd2nerForm. If you want to influence that GUI, you need to make a reference to that form accessible.Probably the simplest would be:
In
Obd2nerForm, declare apublic static Obd2nerForm form, and inObd2nerForm.main, right after you call the constructor, copy the reference to the form into that variable. From then on, you can access the form and its methods using e.g.Obd2nerForm.form.setText().A reminder, though: You seem to have two
main()s, and this needs fixing. All the stuff that should be done at the beginning of the app’s lifetime needs to be in one of thosemains, not several.Finally, look at this method call:
That’s the syntax for calling a method on a particular instance. But
Obd2nerForm.mainis a class method (what you call “static”), and so there isn’t “a particular one” to call, it’s always just the one that belongs to the class itself. That’s why the syntax to call a class method is something likeThe compiler lets you get away with the way you wrote it, but it’s not how it’s usually done, and indicates some confusion.
There… I hope that gets you a little further along. If you still have problems, please post a more complete code sample to PasteBin and I’ll take a look!