tags:

views:

44

answers:

1

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.

+2  A: 
Carl Smotricz
I tried this but it did not work... public String getNewScannerValueRunnable(){ Runnable doWorkRunnable = new Runnable() { @Override public void run() { Status.form.getNewRotatingValue();} }; SwingUtilities.invokeAndWait(doWorkRunnable); return X; } String X = "";Should I just make all of my formattedTextBoxes static?
Adam Outler
No, making everything static will only complicate your life. I've edited my answer to point out some of your problems, and hopefully that will help you straighten out your program. Alternatively, I'd strongly recommend learning GUI programming in better depth by closely following a tutorial or two - your main problem, as you're probably aware, is lack of general Java programming knowledge. But that's normal for just 1 month.
Carl Smotricz
Thank you. You helped me out... Apparently I was referencing form.main() which creates a new form. All I needed to do was form.setvisible. I was looking at a new form and an invisible form was running in the background.
Adam Outler
Thanks for your feedback! I put a fair bit of work into my analysis and I'm gratified that it has proven helpful. Happy hacking!
Carl Smotricz
Everything you said makes sense. I will be going back and taking al of your advice.
Adam Outler