English 中文(简体)
how to update a textbox from a static class?
原标题:

This is driving me nuts. I have a working text based application. It has many many variables which now need a GUI. I m starting with the basics. Whenever some data is sent to my log, I want it to display in my textbox.

Here is a unified entry point for data to pass where it can be manipulated.

public class Log {

    private static void consoleOut(String data) {
    System.out.println(data);
    OBD2nerConsole.update(data);
    }
      public static void level0(String data) {
    if (Status.ConsoleLevel >= 0) {
    consoleOut(data);

    }

This is my form and it has a text box and a few buttons.

public class OBD2nerConsole extends java.awt.Frame {

    public static void update(String data) {
        textField1.setText(textField1.getText() + data);
    }   

}

The prolem I am having is that I am working with a static and non-static I guess.. There is nothing displayed in the text box. I kept playing around and removed all of the errors but it is not working. I don t really know what to do. It seems that this is the best configuration, because there are no errors, but the text box is not doing anything.

I should probly add that this is my first form EVER!

最佳回答

If you give Log a static reference to an instance of OBD2nerConsole and remove static from update(String) you should be able to update the textField1.

Listing of modified Log.java:

public class Log {
    private static Updatable console = Updatables.getUpdatable();

    private static void consoleOut(String data) {
        System.out.println(data);
        console.update(data);
    }

    public static void level0(String data) {
        if (Status.ConsoleLevel >= 0) {
            consoleOut(data);
        }
    }
}

Listing of Updatable.java:

public interface Updatable {
    void update(String);
}

Listing of modified snippet of ODB2nerConsole.java:

public class OBD2nerConsole extends java.awt.Frame implements Updatable {
    @Override
    public void update(String data) {
        textField1.setText(textField1.getText() + data);
    }
}

Listing of Updatables.java:

public class Updatables {
    public Updatable getUpdatable() {
        return new ODB2nerConsole();
    }
}
问题回答

Assuming that textField1 is an attribute of the parent class, the update method should not be static. That of course means that you need to apply the method to an instance of the ODB2tunerConsole object.

The rule in Java is that a static method cannot access the non-static attributes and methods of its class with explicitly using a reference to an instance of the class.

This leads people who are new to object-oriented programming in Java to try to make everything static. But as you see, that leads to trouble. The correct solution is to limit your use of statics to cases where they are really needed. These are as follows:

  • Shared constants; e.g. public static final String FOO = "foo";
  • Helper methods that depend only on the state of their arguments.
  • Hidden references to global data structures, exposed (if necessary) using the "singleton" pattern.




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签